pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matthias Böhm <fliegenblues@gmx.net>
To: pgsql-jdbc@postgresql.org
Subject: Bug with PreparedStatements using EXTRACT function
Date: Fri, 16 Mar 2012 17:45:44 +0100
Message-ID: <000301cd0394$3f325a00$bd970e00$@net> (raw)

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 = conn.prepareStatement(
		"SELECT EXTRACT (YEAR FROM DATE ?)");
		
java.sql.Date date = new java.sql.Date(
		new GregorianCalendar(1990, 5, 7).getTime().getTime());
		
stmt2.setDate(1, date);

// here I get an SQL exception: 
res = stmt2.executeQuery();
***** END CODE *****

The exception thrown is the following (in English the German "Syntaxfehler
bei »$1«" is "syntax error at $1"): 

Exception in thread "main" org.postgresql.util.PSQLException: FEHLER:
Syntaxfehler bei »$1«
  Position: 31
	at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorI
mpl.java:2102)
	at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.ja
va:1835)
	at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
	at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.j
ava:500)
	at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2St
atement.java:388)
	at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statem
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 = conn.createStatement();
		
ResultSet res = 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. 

Regards, 
Matthias Boehm


***** CODE *****

package bug;

import java.sql.*;
import java.util.*;

public class Bug {

	public static void main(String[] args) throws SQLException {
		
		// create connection...
		Connection conn = ...
		
		// ---- This does work ----
		Statement stmt = conn.createStatement();
		
		ResultSet res = stmt.executeQuery(
				"SELECT EXTRACT (YEAR FROM DATE '1990-06-07
+02:00:00')");
		
		res.next();
		
		System.out.println("Year: " + res.getInt(1));
				
		// ---- This doesn't work ----
		PreparedStatement stmt2 = conn.prepareStatement(
				"SELECT EXTRACT (YEAR FROM DATE ?)");
		
		java.sql.Date date = new java.sql.Date(
				new GregorianCalendar(1990, 5,
7).getTime().getTime());
		
		stmt2.setDate(1, date);
		
		// It doesn't work although the compiled statement with
values inserted 
		// seems to be textually equal to the statement above:
		System.out.println(stmt2.toString());
		
		// here I get an SQL exception: 
		res = stmt2.executeQuery();
		
		res.next();
		
		System.out.println("Year: " + res.getInt(1));
		
		
		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 »$1«
  Position: 32
	at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorI
mpl.java:2102)
	at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.ja
va:1835)
	at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
	at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.j
ava:500)
	at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2St
atement.java:388)
	at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statem
ent.java:273)
	at Bug.main(Bug.java:41)
*****




view thread (6+ messages)  latest in thread

Message-ID: <000301cd0394$3f325a00$bd970e00$@net>
Permalink:  ../000301cd0394$3f325a00$bd970e00$@net/
Also on:    postgresql.org/message-id/000301cd0394$3f325a00$bd970e00$@net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: fliegenblues@gmx.net
  Subject: Re: Bug with PreparedStatements using EXTRACT function
  In-Reply-To: <000301cd0394$3f325a00$bd970e00$@net>
  References: <000301cd0394$3f325a00$bd970e00$@net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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