pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeffrey Leegon <nogeel@acm.org>
To: pgsql-jdbc@postgresql.org
Subject: JDBC, PrepareStatement and TimeStamp problem
Date: Tue, 10 Jan 2006 20:17:04 +0000
Message-ID: <43C41640.8010000@acm.org> (raw)


I get an error with the timestamps when running the following code:
(Varibbles currentTrainStart and stop are GregorianCalendars)
String tableName = "\"AdultData\"";


  String tempSql = "SELECT 
\"Age\",\"Gender\",\"PrimaryLanguage\",\"TimeOfDay\",\"DayOfWeek\",\"ChiefComplaint\",\"BodyFluidIso\",\"RespIso\",\"Acuity\",\"Consult\",\"Labs\",\"Radiology\",";
             tempSql+= "\"EKG\",\"MOA\",\"Admitted\" FROM ? ";
             tempSql +="WHERE \"DATE_ADMITTED\">= ? AND 
\"DATE_ADMITTED\"< ? ;";
             query = conn.prepareStatement(tempSql);


        query.setString(1, tableName);
        Timestamp startTrain = new 
Timestamp(currentTrainStart.getTimeInMillis());
        query.setTimestamp(2,startTrain);
        Timestamp stopTrain = new Timestamp(stop.getTimeInMillis());
        query.setTimestamp(3, stopTrain);

        System.out.println(query.toString());


        query.executeQuery();
        // query.clearParameters();

When I execute the following code I get the error:

java.sql.SQLException: ERROR: syntax error at or near "$1"
	at 
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1482)
	at 
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1267)
	at 
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:186)
	at 
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:392)
	at 
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:330)
	at 
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:240)
	at TemporalDataSetCreator.getNextDataSet(TemporalDataSetCreator.java:180)
	at SequentialBNTest.main(SequentialBNTest.java:31)

After initiating the places holders in the prepared statement and call 
toString() I get the following line going to the DB:

SELECT 
"Age","Gender","PrimaryLanguage","TimeOfDay","DayOfWeek","ChiefComplaint","BodyFluidIso","RespIso","Acuity","Consult","Labs","Radiology","EKG","MOA","Admitted" 
FROM "AdultData" WHERE "DATE_ADMITTED">= 2004-05-10 00:00:00.000000+0100 
AND "DATE_ADMITTED"< 2004-05-31 00:00:00.000000+0100

Which if I put in pgAdmin III and add single quotes around the 
Timestamps if runs fine. If I add \' around the ? in the prepared 
statement it gives the ? to the DB and throws and error. What am I doing 
wrong?

Thanks,
Jeff



view thread (2+ messages)  latest in thread

Message-ID: <43C41640.8010000@acm.org>
Permalink:  ../43C41640.8010000@acm.org/
Also on:    postgresql.org/message-id/43C41640.8010000@acm.org

 · 

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: nogeel@acm.org
  Subject: Re: JDBC, PrepareStatement and TimeStamp problem
  In-Reply-To: <43C41640.8010000@acm.org>
  References: <43C41640.8010000@acm.org>

* 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