Re: Prepared statement parameter and timestamp

From: "Xavier Poinsard" <xpoinsard(at)free(dot)fr>
To: "ste(dot)buffaishere(at)tin(dot)it" <ste(dot)buffaishere(at)tin(dot)it>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Prepared statement parameter and timestamp
Date: 2007-04-11 13:21:29
Message-ID: 461CE0D9.1090609@free.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Kris Jurka a écrit :
> On Tue, 3 Apr 2007, ste(dot)buffaishere(at)tin(dot)it wrote:
>
>> PreparedStatement ps = conn.prepareStatement(
>> "SELECT date_trunc('week', {ts ?})");
>> ps.setTimestamp(1, new java.sql.
>> Timestamp(
>> new java.util.Date().getTime()));
>>
>> fails miserably with
>> org.postgresql.util.PSQLException: ERROR: syntax
>> error at or near "$1"

The purpose of the {ts 'XXX'} JDBC escape syntax is to have a standard
syntax to write timestamp literal values. But if you are using a
PreparedStatement you should directly write : SELECT date_trunc('week',?).

>
>
> The JDBC driver translates the escape sequence to:
>
> SELECT date_trunc('week', timestamp ?)
>
> but the server can't handle the prefix timestamp cast for anything other
> than plain literals, so it doesn't work for the parameter.
>
> I suppose the driver should instead try to rewrite this as:
>
> SELECT date_trunc('week', ?::timestamp)
>
> I'm not sure how much work that would be to do.

This is a one line change.

>
> Kris Jurka
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message ismo.tuononen 2007-04-11 13:45:52 7.4 driver vs 8.2 driver
Previous Message Kris Jurka 2007-04-11 11:10:24 Re: Small addition to PGInterval