Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id A0D97633C78 for ; Fri, 23 Apr 2010 10:13:10 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 70133-04-5 for ; Fri, 23 Apr 2010 13:12:59 +0000 (UTC) X-Greylist: delayed 00:25:34.560234 by SQLgrey-1.7.6 Received: from office2.smilehouse.cxm (80.205.94.193.static.smilehouse.com [193.94.205.80]) by mail.postgresql.org (Postfix) with ESMTP id 5E322633B63 for ; Fri, 23 Apr 2010 10:12:21 -0300 (ADT) Received: from dhcp-208.smilehouse.cxm (dhcp-208.smilehouse.cxm [192.168.192.208]) by office2.smilehouse.cxm (Postfix) with ESMTP id E6A7E4BB62 for ; Fri, 23 Apr 2010 15:46:44 +0300 (EEST) Message-ID: <4BD196B4.3040607@smilehouse.com> Date: Fri, 23 Apr 2010 15:46:44 +0300 From: Heikki Hiltunen User-Agent: Thunderbird 2.0.0.24 (Macintosh/20100228) MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Problem with prepareStatement and Statement.RETURN_GENERATED_KEYS in PostgreSQL JDBC driver 8.4 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201004/61 X-Sequence-Number: 20472 When using 8.4 JDBC drivers, calling prepareStatement(String sql, int autoGeneratedKeys) with Statement.RETURN_GENERATED_KEYS seems to add "RETURNING *" to the end of the SQL even with select statements. According to Javadoc for prepareStatement(String sql, int autoGeneratedKeys) in java.sql.Connection: "The given constant tells the driver whether it should make auto-generated keys available for retrieval. This parameter is ignored if the SQL statement is not an INSERT statement, or an SQL statement able to return auto-generated keys (the list of such statements is vendor-specific). " Here's a simple test class to demonstrate this problem: import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; public class PostgrePrepareStatementTest { public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException { Class.forName("org.postgresql.Driver").newInstance(); String url = "jdbc:postgresql://localhost:5432/testdb"; Connection conn = DriverManager.getConnection(url, "user", "password"); PreparedStatement pStmt = conn.prepareStatement("select * from test_table", Statement.RETURN_GENERATED_KEYS); System.out.println(pStmt.toString()); pStmt.execute(); } } When run this prints "select * from test_table RETURNING *" - which is invalid - and an exception is thrown: Exception in thread "main" org.postgresql.util.PSQLException: ERROR: syntax error at or near "RETURNING" Position: 26 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1795) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:367) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:360) at com.smilehouse.PostgrePrepareStatementTest.main(PostgrePrepareStatementTest.java:17) I have tested this with driver version 8.4-701 (both JDBC3 and JDBC4). The PostgreSQL version was 8.4. -Heikki Hiltunen