Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4d84b7a

Browse files
committed
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
1 parent 5b42666 commit 4d84b7a

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,13 @@ public java.sql.Date getDate(int columnIndex) throws SQLException
423423
String s = getString(columnIndex);
424424
if(s==null)
425425
return null;
426-
427-
return java.sql.Date.valueOf(s);
426+
// length == 10: SQL Date
427+
// length > 10: SQL Timestamp, assumes PGDATESTYLE=ISO
428+
try {
429+
return java.sql.Date.valueOf((s.length() == 10) ? s : s.substring(0,10));
430+
} catch (NumberFormatException e) {
431+
throw new PSQLException("postgresql.res.baddate", s);
432+
}
428433
}
429434

430435
/**
@@ -441,8 +446,13 @@ public Time getTime(int columnIndex) throws SQLException
441446

442447
if(s==null)
443448
return null; // SQL NULL
444-
445-
return java.sql.Time.valueOf(s);
449+
// length == 8: SQL Time
450+
// length > 8: SQL Timestamp
451+
try {
452+
return java.sql.Time.valueOf((s.length() == 8) ? s : s.substring(11,19));
453+
} catch (NumberFormatException e) {
454+
throw new PSQLException("postgresql.res.badtime",s);
455+
}
446456
}
447457

448458
/**

0 commit comments

Comments
 (0)