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 :(

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
REL7_2_STABLE
Bruce Momjian 24 years ago
parent 5b42666fd9
commit 4d84b7a10f
  1. 18
      src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java

@ -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);
}
}
/**

Loading…
Cancel
Save