handling DATETIME values 0000-00-00 00:00:00 in JDBC

Posted by Jason S on Stack Overflow See other posts from Stack Overflow or by Jason S
Published on 2009-04-23T17:45:16Z Indexed on 2010/04/22 16:33 UTC
Read the original article Hit count: 366

Filed under:
|
|
|

I get an exception (see below) if I try to do

resultset.getString("add_date");

for a JDBC connection to a MySQL database containing a DATETIME value of 0000-00-00 00:00:00 (the quasi-null value for DATETIME), even though I'm just trying to get the value as string, not as an object.

I got around this by doing

SELECT CAST(add_date AS CHAR) as add_date

which works, but seems silly... is there a better way to do this?

My point is that I just want the raw DATETIME string, so I can parse it myself as is.

note: here's where the 0000 comes in: (from http://dev.mysql.com/doc/refman/5.0/en/datetime.html)

Illegal DATETIME, DATE, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00 00:00:00' or '0000-00-00').

The specific exception is this one:

SQLException: Cannot convert value '0000-00-00 00:00:00' from column 5 to TIMESTAMP.
SQLState: S1009
VendorError: 0
java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 5 to TIMESTAMP.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.ResultSetImpl.getTimestampFromString(ResultSetImpl.java:6343)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5670)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5491)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5531)

© Stack Overflow or respective owner

Related posts about jdbc

Related posts about java