Sunday, September 27, 2009

MySQL Connector/J exception on timestamp

If you are using MySQL JDBC driver in your Java program and the version is MySQL Connector/J 3.1, you might see this exception sometimes:

    Exception: Cannot convert value '0000-00-00 00:00:00' from column ... to TIMESTAMP.

The exception is thrown from mysql.jdbc.ResultSet and is caused by the datetime field with an all zero value '0000-00-00 00:00:00'.

Sometimes, you don't want to manually update all those datetime fields to actual time values. You just want your program to continue. The trick would be to set the zeroDateTimeBehavior property to round, which rounds the value to "0001-01-01 00:00:00":

    jdbc:mysql://localhost:3306/mydb?zeroDateTimeBehavior=round

Reference: http://dev.mysql.com/doc/refman/5.0/en/connector-j-installing-upgrading.html (search section Datetimes)

No comments:

Post a Comment