Search code examples
arraysjdbcnullcallable-statement

How to pass an NULL (or empty) array to a JDBC callableStatement (that expects an Array)


I'm having trouble passing a NULL value to a JDBC stored function (using a callable statement) that expects an Array type. This is only a problem if I'm trying to set the IN parameter to NULL (eg., I can create and pass an empty array, but I shouldn't have to do that).

For example, I can do this as a work-around:

callableStatement.setObject(index, callableStatement.getConnection.createArrayOf("numeric", Array().asInstanceOf[Array[AnyRef]]))

But this bothers me. First of all, there is supposed to be an API for passing NULL arrays. Second of all, I'm creating an empty array for no good reason (and I'll have to create the correct array type so this is not a one-liner, I'll need to support several different types). It gets messy.

I should be able to do this, I think (or at least something pretty similar):

callableStatement.setNull(index, Types.ARRAY)

But it results in an exception:

com.edb.util.PSQLException: ERROR: function app_fetch_user_list_pkg.n_execute(character varying, character varying[], character varying, boolean, integer) does not exist

Any ideas? (We are working with Postgresql/EDB as well as Oracle... so far, I've been experimenting with the Postgresql instance).


Solution

  • Well, I'm using PreparedStatement.setNull(position, Types.ARRAY) with the 9.4-1202-jdbc41 JDBC driver and that is working as expected.