Description:
I already reported this bug about three weeks ago (nr. 5235) but I did not get any reaction on sending a test case. Because the bug still exists, I'll send it again.
If I use the value 'convertToNull' for the property 'setZeroDateTimeBehavior' Connector/J throws a ClassCastException when getObject() is used after using a PreparedStatement for selecting a Date-field with value "0000-00-00 00:00:00'. When a ordinary Statement is used there is no problem. See the tes case below.
How to repeat:
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.*;
public class Test {
private static final String server = "localhost";
private static final String database = "test";
private static final String username = "root";
private static final String password = "root";
private Statement stmt;
public static void main (String [ ] args)
{
try
{ System.err.println ("Initialize datasource");
MysqlDataSource ds = new MysqlDataSource();
ds.setServerName (server);
ds.setDatabaseName (database);
ds.setUser (username);
ds.setPassword (password);
System.err.println ("Setup database connection");
Connection conn = setupConnection (ds);
testBug5235 (conn);
}
catch (Exception e)
{ e.printStackTrace (System.err);
System.exit (1);
}
}
private static Connection setupConnection (DataSource ds)
throws SQLException
{
Connection conn = ds.getConnection();
// Set null date option:
com.mysql.jdbc.Connection myConn = (com.mysql.jdbc.Connection) conn;
myConn.setZeroDateTimeBehavior ("convertToNull");
return conn;
}
private static void testBug5235 (Connection conn) throws Exception
{
Statement stmt = conn.createStatement();
try {
stmt.executeUpdate ("DROP TABLE IF EXISTS testBug5235");
stmt.executeUpdate ("CREATE TABLE testBug5235(field1 DATE)");
stmt.executeUpdate("INSERT INTO testBug5235 (field1) VALUES ('0000-00-00')");
PreparedStatement ps = conn.prepareStatement ("SELECT field1 FROM testBug5235");
ResultSet rs = ps.executeQuery();
//ResultSet rs = conn.createStatement().executeQuery("SELECT field1 FROM testBug5235");
if (rs.next())
{ Date d = (Date) rs.getObject ("field1");
System.out.println ("date: " + d);
}
}
finally {
stmt.executeUpdate("DROP TABLE IF EXISTS testBug5235");
}
}
}
Suggested fix:
Add next if-clause to ReseltSet.getObject() immediately following the special case for type BIT:
if (field.getSQLType() == Types.DATE) {
return getDate(columnIndex);
}