Re: [JDBC] Re: Trouble with JDBC2 ResultSet.getDate()

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Palle Girgensohn <girgen(at)partitur(dot)se>
Cc: Juhan-Peep Ernits <juhan(at)cc(dot)ioc(dot)ee>, pgsql-interfaces(at)postgresql(dot)org, pgsql-jdbc(at)postgresql(dot)org, peter(at)retep(dot)org(dot)uk
Subject: Re: [JDBC] Re: Trouble with JDBC2 ResultSet.getDate()
Date: 2001-05-30 16:34:53
Message-ID: 200105301634.f4UGYrV08160@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces pgsql-jdbc


Patch applied. Standards require it. If Peter adds casting later, this
can be removed. We are clearly seeing problem reports about this.

Thanks.

> Hi!
>
> I just got bitten by this too. I use type timestamp in the
> database, and often need the latest timestamp, but want to
> format it as a date. With 7.0.x, I just
>
> select ts from foo order by ts desc limit 1
>
> and in java: d = res.getDate(1);
>
> but this fails everywhere in my code now :(
>
> The problem is this optimization in
> src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java,
> introduced in 1.21:
>
> @@ -418,12 +418,8 @@
> String s = getString(columnIndex);
> if(s==null)
> return null;
> - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
> - try {
> - return new java.sql.Date(df.parse(s).getTime());
> - } catch (ParseException e) {
> - throw new PSQLException("postgresql.res.baddate",new
> Integer(e.getErrorOffset()),s);
> - }
> +
> + return java.sql.Date.valueOf(s);
> }
>
> /**
>
>
> Log string is:
>
> - Removed need for SimpleDateFormat in ResultSet.getDate()
> improving performance.
>
> I cannot find any reference to whether it is bad or not to let
> getDate return a date when the string from postgres is a
> timestamp or datetime. It appears to me as a good feature, and
> since it has been working for quite som time, and the postgres
> documenation recommends timestamp as the best time & date type,
> is it really necessary to break functionality for many users
> this way? The performance fix is good, but I think code shall
> be added to make it backward compatible.
>
> My query will need to be rewritten like this:
>
> select ts::date as d, ts from foo order by ts desc limit 1
>
> and I have hundreds of them :(
>
> Maybe we can introduce a try-catch clause to handle the case
> where the string is really a timestamp and not a pure date, but
> this would give users the false impression that everything is
> OK, and exceptions are really a performance hog... Maybe just
> check if the string size is > 10, and then use the old code?
> Agree, this would make it complicated.
>
> /Palle
>
>
> Juhan-Peep Ernits wrote:
> >
> > System is Debian "woody"
> > java is IBM SDK1.3
> > Source is CVS from March 20, 2001.
> >
> > Trouble is the following, that
> >
> > org.postgresql.jdbc2.ResultSet.getDate(int)
> >
> > Started to generate errors
> >
> > java.lang.NumberFormatException: 15 14:25:17+02
> > at java.lang.Integer.parseInt(Integer.java:415)
> > at java.lang.Integer.parseInt(Integer.java:455)
> > at java.sql.Date.valueOf(Date.java:97)
> > at org.postgresql.jdbc2.ResultSet.getDate(ResultSet.java:427)
> > at org.postgresql.jdbc2.ResultSet.getDate(ResultSet.java:665)
> > ...
> >
> > when fetching dates from fields of timestamp
> > type. It seems that the fixes provided in CVS version 1.18 from Jan 24
> > 23:41:04 2001 of ResultSet.java regarding getDate() method broke it for
> > our application. Now I went back to 1.17 and copied the
> >
> > SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
> >
> > try {
> > return new java.sql.Date(df.parse(s).getTime());
> > } catch (ParseException e) {
> > throw new PSQLException("postgresql.res.baddate",new
> > Integer(e.getErrorOffset()),s);
> > }
> >
> > part to replace the new code:
> >
> > return java.sql.Date.valueOf(s);
> >
> > and it works fine but I have not had time to debug this any further. May
> > be it would be nice to have that part of the old code included in the 7.1
> > release driver?
> >
> > Regards,
> >
> > Juhan Ernits
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 6: Have you searched our list archives?
> >
> > http://www.postgresql.org/search.mpl
>
> --
> Partitur Informationsteknik AB
> Wenner-Gren Center +46 8 566 280 02
> 113 46 Stockholm +46 70 785 86 02
> Sweden girgen(at)partitur(dot)se
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026

Attachment Content-Type Size
unknown_filename text/plain 1007 bytes

In response to

Browse pgsql-interfaces by date

  From Date Subject
Next Message Joe Conway 2001-05-31 04:59:52 remote database queries
Previous Message Manika dey 2001-05-30 15:52:35 Re: Using BLOBs with several DBMS

Browse pgsql-jdbc by date

  From Date Subject
Next Message Bruce Momjian 2001-05-30 16:35:41 Re: getDate() and getTime() fails with columns of type 'TimeStamp'
Previous Message Tony Grant 2001-05-30 16:20:29 Re: Fw: Druid problems