Saturday, October 31, 2009

mysql-java-connector: ResultSet getObject failure on Timestamp column if null

I had strange situation working with ResultSet when fetching Timestamp column from MyTable.

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

Class.forname("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:mysql://host:port/database",
"username", "password");

Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT timestamp_column FROM mytable");

while(rs.next()) {
rs.getObject("timestamp_column");
}


The cause of the failure was due to null value in timestamp_column !

The solution suggested about (zeroDateTimeBehavior=convertToNull) in the article, helped me overcome the failure.

All you need to do is:
Connection connection = DriverManager.getConnection(
"jdbc:mysql://host:port/database?zeroDateTimeBehavior=convertToNull",
"username", "password");

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?