pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: Aleksey Demakov <avd@gcom.ru>
To: pgsql-interfaces@postgreSQL.org
To: peter@retep.org.uk
Subject: Re: [INTERFACES] JDBC date problem
Date: Sun, 18 Oct 1998 12:02:42 +0200
Message-ID: <l03110702b24f6b7e6731@[147.233.159.109]> (raw)
In-Reply-To: <87u315m3ip.fsf@avd.garsib.ru>

At 8:20 +0200 on 16/10/98, Aleksey Demakov wrote:


>
> Can you believe me?
>
> I have a table with a column which is to store the dates each row was
> created. I fill it with the line:
>
>     insert_stmt.setDate(2, new Date (System.currentTimeMillis ()));
>
> And every day I insert a new row I can see that I did it tomorrow!
>
> I have found that in the method PreparedStatement.setDate() the date
> argument is intentionally incremented by one. The comments there say
> that it's to compensate broken SimpleDateFormat. Apparently, my
> SimpleDateFormat is ok (JDK 1.1.7 for win95/nt and jdk1.1.5 for freebsd).

For some, it works like that, and for some it doesn't. I'll leave it to the
author of the JDBC driver to explain why this is done. The question I ask
is: why do you use the Java current date instead of doing this within the
INSERT itself?

That is, if the column to contain the row creation date is of type
DATETIME, just use now() instead of a ? and a setDate.

INSERT INTO my_table
  (creation, other_field1, other_field2, other_field3)
VALUES
  ('now', ?, ?, ?);

Personally, I do this by defining the creation column as a NOT NULL and
giving it a default (There's a bit of a trick here, because you have to use
a function, or 'now' will be interpreted as the time of the creation of the
table, so I define an SQL function which returns 'now'::DATETIME). This
enables me to use a statement like

INSERT INTO my_table
  (other_field1, other_field2, other_field3)
VALUES
  (?,?,?);

And not bother myself about the creation dates (which are automatic).

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





view thread (12+ messages)  latest in thread

Message-ID: <l03110702b24f6b7e6731@[147.233.159.109]>
Permalink:  ../l03110702b24f6b7e6731@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03110702b24f6b7e6731@[147.233.159.109]

 · 

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-interfaces@postgresql.org
  Cc: herouth@oumail.openu.ac.il, avd@gcom.ru, peter@retep.org.uk
  Subject: Re: [INTERFACES] JDBC date problem
  In-Reply-To: <l03110702b24f6b7e6731@[147.233.159.109]>
  References: <87u315m3ip.fsf@avd.garsib.ru> <l03110702b24f6b7e6731@[147.233.159.109]>

* 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