Bug #5641 Connector/J throws ClassCastException
Submitted: 18 Sep 2004 10:50 Modified: 19 Sep 2004 21:40
Reporter: M Wensink Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / J Severity:S1 (Critical)
Version:Latest Snapshot OS:Windows (W2K)
Assigned to: CPU Architecture:Any

[18 Sep 2004 10:50] M Wensink
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);
}
[18 Sep 2004 12:50] Mark Matthews
Please don't open another bug report for the same bug. Ammend your test case to bug #5235.
[19 Sep 2004 21:23] M Wensink
I already added the testcase to bug#5235 on september the first but did not get any reaction.
[19 Sep 2004 21:40] Mark Matthews
Opening another bug report won't make things go faster...it just 'soaks up cycles' as we track down the duplicates.