Wednesday, July 1, 2009

JDBC to MySQL Datetime (Time truncated)

Be careful when accessing Datetime fields in a MySQL database through JDBC. You might think that the java.sql.Date would work, but that actually truncates the Date back to midnight. In order to access the actual time, use getTimestamp() instead. Here is the code:

Result rs = ...
while (rs.next()) {
return rs.getTimestamp(1).getTime();
}

No comments: