Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 18E6D131F6D5 for ; Fri, 16 Mar 2012 13:46:00 -0300 (ADT) Received: from mailout-de.gmx.net ([213.165.64.23]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1S8aHt-0001hv-7t for pgsql-jdbc@postgresql.org; Fri, 16 Mar 2012 16:45:59 +0000 Received: (qmail invoked by alias); 16 Mar 2012 16:45:43 -0000 Received: from i577A745E.versanet.de (EHLO MatthiasLaptop) [87.122.116.94] by mail.gmx.net (mp033) with SMTP; 16 Mar 2012 17:45:43 +0100 X-Authenticated: #41751617 X-Provags-ID: V01U2FsdGVkX18c/kJnwngcHpI6oEW7JpPIPJoYnCQGqXA8gJ/h9G bCjbCNkNBfZTnJ From: =?iso-8859-1?Q?Matthias_B=F6hm?= To: Subject: Bug with PreparedStatements using EXTRACT function Date: Fri, 16 Mar 2012 17:45:44 +0100 Message-ID: <000301cd0394$3f325a00$bd970e00$@net> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook 12.0 Content-Language: de Thread-Index: Ac0DlCplXbUyASQ0Q2S9fK1qIHLAMw== X-Y-GMX-Trusted: 0 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201203/31 X-Sequence-Number: 22770 Hi! I found a bug in the postgresql jdbc driver (I'm using version 9.1 build 901). Constructing a prepared statement that uses the EXTRACT function = in the following way results in an PSQLException when trying to execute the prepared statement: ***** CODE ***** PreparedStatement stmt2 =3D conn.prepareStatement( "SELECT EXTRACT (YEAR FROM DATE ?)"); =09 java.sql.Date date =3D new java.sql.Date( new GregorianCalendar(1990, 5, 7).getTime().getTime()); =09 stmt2.setDate(1, date); // here I get an SQL exception:=20 res =3D stmt2.executeQuery(); ***** END CODE ***** The exception thrown is the following (in English the German = "Syntaxfehler bei =BB$1=AB" is "syntax error at $1"):=20 Exception in thread "main" org.postgresql.util.PSQLException: FEHLER: Syntaxfehler bei =BB$1=AB Position: 31 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecut= orI mpl.java:2102) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl= .ja va:1835) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:2= 57) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statemen= t.j ava:500) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc= 2St atement.java:388) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Sta= tem ent.java:273) at pitas.shipdbs.storage.Bug.main(Bug.java:42) Executing a simple unprepared statement with an equal, unprepared query doesn't result in a syntax error: ***** CODE **** Statement stmt =3D conn.createStatement(); =09 ResultSet res =3D stmt.executeQuery( "SELECT EXTRACT (YEAR FROM DATE '1990-06-07 +02:00:00')"); res.next(); System.out.println("Year: " + res.getInt(1)); ***** END CODE **** A complete example that shows this bug is added at the end of the mail.=20 Regards,=20 Matthias Boehm ***** CODE ***** package bug; import java.sql.*; import java.util.*; public class Bug { public static void main(String[] args) throws SQLException { =09 // create connection... Connection conn =3D ... =09 // ---- This does work ---- Statement stmt =3D conn.createStatement(); =09 ResultSet res =3D stmt.executeQuery( "SELECT EXTRACT (YEAR FROM DATE '1990-06-07 +02:00:00')"); =09 res.next(); =09 System.out.println("Year: " + res.getInt(1)); =09 // ---- This doesn't work ---- PreparedStatement stmt2 =3D conn.prepareStatement( "SELECT EXTRACT (YEAR FROM DATE ?)"); =09 java.sql.Date date =3D new java.sql.Date( new GregorianCalendar(1990, 5, 7).getTime().getTime()); =09 stmt2.setDate(1, date); =09 // It doesn't work although the compiled statement with values inserted=20 // seems to be textually equal to the statement above: System.out.println(stmt2.toString()); =09 // here I get an SQL exception:=20 res =3D stmt2.executeQuery(); =09 res.next(); =09 System.out.println("Year: " + res.getInt(1)); =09 =09 conn.close(); } } ***** END CODE ***** Output of the program: ***** Year: 1990 SELECT EXTRACT (YEAR FROM DATE '1990-06-07 +02:00:00') Exception in thread "main" org.postgresql.util.PSQLException: FEHLER: Syntaxfehler bei =BB$1=AB Position: 32 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecut= orI mpl.java:2102) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl= .ja va:1835) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:2= 57) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statemen= t.j ava:500) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc= 2St atement.java:388) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Sta= tem ent.java:273) at Bug.main(Bug.java:41) *****