OSDN Git Service

I just got bitten by this too. I use type timestamp in the
authorBruce Momjian <bruce@momjian.us>
Wed, 30 May 2001 16:34:49 +0000 (16:34 +0000)
committerBruce Momjian <bruce@momjian.us>
Wed, 30 May 2001 16:34:49 +0000 (16:34 +0000)
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 :(

http://java.sun.com/j2se/1.3/docs/guide/jdbc/spec/jdbc-spec.frame7.html

says

  The ResultSet.getXXX methods will attempt to
  convert whatever SQL type was returned by the
  database to whatever Java type is returned by
  the getXXX method.

Palle Girgensohn

src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java

index 2928ab9..81605de 100644 (file)
@@ -423,8 +423,13 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
     String s = getString(columnIndex);
     if(s==null)
       return null;
-
-    return java.sql.Date.valueOf(s);
+    // length == 10: SQL Date
+    // length >  10: SQL Timestamp, assumes PGDATESTYLE=ISO
+    try {
+      return java.sql.Date.valueOf((s.length() == 10) ? s : s.substring(0,10));
+    } catch (NumberFormatException e) {
+      throw new PSQLException("postgresql.res.baddate", s);
+    }
   }
 
   /**
@@ -441,8 +446,13 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
 
     if(s==null)
       return null; // SQL NULL
-
-    return java.sql.Time.valueOf(s);
+    // length == 8: SQL Time
+    // length >  8: SQL Timestamp
+    try {
+      return java.sql.Time.valueOf((s.length() == 8) ? s : s.substring(11,19));
+    } catch (NumberFormatException e) {
+      throw new PSQLException("postgresql.res.badtime",s);
+    }
   }
 
   /**