Uploaded image for project: 'MariaDB Connector/J'
  1. MariaDB Connector/J
  2. CONJ-57

When calling setObject(new Date(), Types.TimeStamp) from a Preparedstatement the field is filled with a zero date String.

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Minor
    • Resolution: Fixed
    • 1.1.3
    • None
    • None
    • None

    Description

      When calling setObject(new Date(), Types.TimeStamp) from a Preparedstatement the field is filled with a zero date String.
      When you wrap the Date in a Timestamp Object it's working as expected.
      Steps to reproduce:

      • Execute the follwing testcase:

      import org.junit.After;
      import org.junit.Before;
      import org.junit.Test;
       
      import java.sql.*;
      import java.util.Calendar;
      import java.util.Date;
      import java.util.TimeZone;
       
      import static org.junit.Assert.assertEquals;
       
      public class MariaDBDateTest {
       
          private static final String url =
                  "jdbc:mysql://" +
                          // "jdbc:mariadb://" +
                          System.getProperty("hostname", "localhost") + ":" +
                          System.getProperty("port", "3306") + "/" +
                          System.getProperty("dbname", "test");
          private static final String user = "root";
          private static final String passwd = "";
       
          private Connection con;
          private Date date;
       
          @Before
          public void setUp() throws Exception {
              // Load in the DriverManager Class
              Class.forName("org.mariadb.jdbc.Driver");
      //        Class.forName("com.mysql.jdbc.Driver");
       
              con = DriverManager.getConnection(url, user, passwd);
       
              date = Calendar.getInstance(TimeZone.getTimeZone()).getTime();
      //                new Date();
      //                Calendar.getInstance().getTime();
       
              createDB();
              System.out.println("Setup Complete.");
              System.out.println("**********");
          }
       
          @After
          public void tearDown() throws Exception {
              con.close();
              System.out.println("Teardown Complete.");
              System.out.println("**********");
          }
       
          private void createDB() throws Exception {
              con.setAutoCommit(true);
       
              PreparedStatement pstmt_drop = con.prepareStatement("DROP TABLE IF EXISTS test_table");
              try {
                  pstmt_drop.executeUpdate();
              } catch (Throwable t) {
              }
       
              PreparedStatement pstmt_create = con.prepareStatement("CREATE TABLE test_table " +
                      "(" +
                      "id INT PRIMARY KEY, " +
                      "someDate1 DATETIME(3)," +
                      "someDate2 DATETIME(3)" +
                      ")");
              pstmt_create.executeUpdate();
       
       
          }
       
          @Test
          public void testDateTime() throws Exception {
       
              PreparedStatement pstmt_add = con.prepareStatement("INSERT INTO test_table (id, someDate1, someDate2) VALUES (?, ?, ?)");
       
       
              pstmt_add.setInt(1, 1);
              pstmt_add.setObject(2, new Timestamp(date.getTime()), Types.TIMESTAMP);
              pstmt_add.setObject(3, date, Types.TIMESTAMP);
              pstmt_add.executeUpdate();
       
       
              String sql = "SELECT ID, someDate1, someDate2 FROM test_table t0 WHERE ID = ?";
              PreparedStatement pstmt = con.prepareStatement(sql);
              pstmt.setInt(1, 1);
       
              ResultSet rs = pstmt.executeQuery();
              while (rs.next()) {
                  assertEquals(1, rs.getInt(1));
                  assertEquals(date, rs.getObject(2));
      //            assertEquals(date,  rs.getObject(3));
              }
          }
       
      }

      • Then execute mariadb client

        MariaDB [test]> select * from test_table;
        +----+-------------------------+-------------------------+
        | id | someDate1               | someDate2               |
        +----+-------------------------+-------------------------+
        |  1 | 2013-07-19 07:00:17.452 | 0000-00-00 00:00:00.000 |
        +----+-------------------------+-------------------------+
        1 row in set (0.00 sec)
         
        MariaDB [test]> 

      Attachments

        Activity

          People

            wlad Vladislav Vaintroub
            framas Frank S
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.