Skip site navigation (1) Skip section navigation (2)

JDBC and getTimestamp() exception

From: idealab(at)sirius(dot)pisa(dot)it
To: <pgsql-bugs(at)postgresql(dot)org>
Subject: JDBC and getTimestamp() exception
Date: 2001-03-21 13:41:20
Message-ID: 000101c0b20c$d7ad89c0$ca0afac3@techserver (view raw or flat)
Thread:
Lists: pgsql-bugs
We are running a PostgreSQL server on a Debian 2.2 and we have problems with
the
mothod getTimestamp() of the ResultSet class;
we have find that if we insert a timestamp on a table, for example
'2000-10-09 12:21:34.321+01'
when we try to read that tuples using the JDBC driver we have an exception
on position 19.
The exception is caused by the milliseconds part of the timestamp!

We are using the following version of the data base
PostgreSQL 7.0.0 on i686-pc-linux-gnu, compiled by gcc 2.95.2
and we have tried to use the JDBC driver of the 7.1 release (downloading it
from CVS)

If we change the getTimestamp() methods of the
jdbc.postgresql.jdbc2.ResultSet

// Original code
public Timestamp getTimestamp(int columnIndex) throws SQLException
{
 String s = getString(columnIndex);
     if(s==null)
  return null;

     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzz");

     try {
  return new Timestamp(df.parse(s).getTime());
     } catch(ParseException e) {
  throw new PSQLException("postgresql.res.badtimestamp",new
Integer(e.getErrorOffset()),s);
     }
}

with the following code

// New code
public Timestamp getTimestamp(int columnIndex) throws SQLException
{
 String s = rs.getString(columnIndex);
 if (s==null) return;

 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSzzz");

 try {
  return new Timestamp(df.parse(s).getTime());
 } catch(ParseException e1) {

  df=new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzz");
  try {
   return new Timestamp(df.parse(s).getTime());
  } catch(ParseException e2) {
   throw new PSQLException("postgresql.res.badtimestamp",new
Integer(e2.getErrorOffset()),s);
  }

 }
}

we have no exceptions from the JDBC driver!
(but why is a new SimpleDateFormat created every time something call this
methods? if we
create this object when the ResultSet is created we can avoid to create a
new
SimpleDateFormat every time the getTimestamp methods is called)

-Massimiliano Giorgi

Excuse me for my bad English.




Responses

pgsql-bugs by date

Next:From: Tatsuo IshiiDate: 2001-03-21 14:14:12
Subject: Re: Encoding problem
Previous:From: Krzysztof CzumaDate: 2001-03-21 12:00:55
Subject: Re: psql crashes postmaster

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group