setObject(...) with native Java arrays like String[] ?

From: Craig Ringer <ringerc(at)ringerc(dot)id(dot)au>
To: PostgreSQL JDBC <pgsql-jdbc(at)postgresql(dot)org>
Subject: setObject(...) with native Java arrays like String[] ?
Date: 2012-08-22 03:09:12
Message-ID: 50344D58.9010401@ringerc.id.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hi all

I was recently surprised to find that PgJDBC doesn't accept Java arrays
as parameters to prepared statements.

For example:

PreparedStatement.setObject(1, new String[]{"a"}).

will fail with:

[ERROR] Internal Exception: org.postgresql.util.PSQLException: Can't
infer a SQL
type to use for an instance of [Ljava.lang.String;.
Use setObject () with an explicit Types value to specify the type to use.

... which I've verified with the 902 driver in git.

After reading the spec I can't see anywhere the JDBC driver is required
to accept native Java arrays, but at least EclipseLink seems to want to
do it anyway. Is this reasonable to support? There's one par in the JDBC
spec that suggests it might be supposed to work, but I'm not convinced
it's more than bad phrasing. In §16.5.4 the JDBC4.2 draft spec reads:

A Java array may be passed as an input parameter by calling the method
PreparedSatement.setObject.

Thoughts? Opinions? Any idea what other DBs drivers do when they get
passed a raw Java array to setObject() ?

Source:

http://stackoverflow.com/questions/12042181/how-can-i-set-a-string-parameter-to-a-native-query/12065537#12065537

--
Craig Ringer

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Craig Ringer 2012-08-22 03:31:11 Re: setObject(...) with native Java arrays like String[] ?
Previous Message Scott Harrington 2012-08-21 23:08:05 Re: send/receive buffer size