pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedResultSet problem in JDBC
2+ messages / 2 participants
[nested] [flat]
* ResultSet problem in JDBC
@ 2010-12-02 05:14 Zhipan Wang <wzhipan@soe.ucsc.edu>
2010-12-02 06:44 ` Re: [JDBC] ResultSet problem in JDBC Craig Ringer <craig@postnewspapers.com.au>
0 siblings, 1 reply; 2+ messages in thread
From: Zhipan Wang @ 2010-12-02 05:14 UTC (permalink / raw)
To: pgsql-jdbc; +Cc: pgsql-general@postgresql.org
Hi all,
I am writing a Java program using postgres. When I run my program, I got the following error message:
Exception in thread "main" org.postgresql.util.PSQLException: Can't infer the SQL type to use for
an instance of org.postgresql.jdbc4.Jdbc4ResultSet. Use setObject() with an explicit Types value to specify the type to use.
at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1772)
at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:37)
at org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:46)
at Cracking.testMethod(Cracking.java:619)
at GetConnection.main(GetConnection.java:97)
My code that caused this problem is:
Statement st = c.createStatement();
ResultSet r = st.executeQuery("select unnest(col0) from relation2_col1 where keyValue<500");
PreparedStatement sql = c.prepareStatement( "SELECT col1 FROM relation2 WHERE col0 IN ?");
sql.setObject(1, r);
ResultSet result = sql.executeQuery();
I am using Eclipse, and there is no warning for this code.
Can't a result set be used in another query? How do I correct it?
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: [JDBC] ResultSet problem in JDBC
2010-12-02 05:14 ResultSet problem in JDBC Zhipan Wang <wzhipan@soe.ucsc.edu>
@ 2010-12-02 06:44 ` Craig Ringer <craig@postnewspapers.com.au>
0 siblings, 0 replies; 2+ messages in thread
From: Craig Ringer @ 2010-12-02 06:44 UTC (permalink / raw)
To: Zhipan Wang <wzhipan@soe.ucsc.edu>; +Cc: pgsql-jdbc; pgsql-general@postgresql.org
On 02/12/10 13:14, Zhipan Wang wrote:
> Statement st = c.createStatement();
> ResultSet r = st.executeQuery("select unnest(col0) from relation2_col1
> where keyValue<500");
> PreparedStatement sql = c.prepareStatement( "SELECT col1 FROM relation2
> WHERE col0 IN ?");
> sql.setObject(1, r);
> ResultSet result = sql.executeQuery();
You appear to be trying to implement a subquery client-side. Why not
just use:
PreparedStatement sql = c.prepareStatement(
"SELECT col1 FROM relation2 WHERE col0 IN "+
"(SELECT unnest(col0) from relation2_col1 where keyValue<500"
);
? It's hard to know whether the above is correct because you've failed
to provide your schema, but something along those lines seems to fit
your intent from the code above.
> I am using Eclipse, and there is no warning for this code.
No, Eclipse can't know the details of how every JDBC driver works.
> Can't a result set be used in another query? How do I correct it?
Either build an array with the contents of the resultset and pass that,
or avoid the weirdness by using a subquery directly.
--
System & Network Administrator
POST Newspapers
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2010-12-02 06:44 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-12-02 05:14 ResultSet problem in JDBC Zhipan Wang <wzhipan@soe.ucsc.edu>
2010-12-02 06:44 ` Craig Ringer <craig@postnewspapers.com.au>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox