Re: JDBC - Prepared statements and PostgreSql Time/Date operations

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Jan Bilek" <bilekj(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: JDBC - Prepared statements and PostgreSql Time/Date operations
Date: 2007-05-18 21:26:45
Message-ID: 3020.1179523605@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Jan Bilek" <bilekj(at)gmail(dot)com> writes:
> I would like to use this query in java PreparedStatement, where age of a =
> row would be one of its parameters:

> PreparedStatement could look like this:
> select * from mytable where creation_time > (CURRENT_TIMESTAMP - ?)

> But nothing works with PreparedStatements!
> I tried to set whole interval strings "interval '7 days'" as a parameter
> I tried to set time strings '7 days' as a parameter
> I tried to use BigDecimal as a parameter...

I'm not sure that Java has a type corresponding to "interval". Instead
use number-times-interval multiplication:

... creation_time > (CURRENT_TIMESTAMP - ? * interval '1 day')

The parameter is now of plain integer or float type.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jan Bilek 2007-05-18 22:15:49 Re: JDBC - Prepared statements and PostgreSql Time/Date operations
Previous Message Reece Hart 2007-05-18 21:16:13 Re: Random Sample