handling DATETIME values 0000-00-00 00:00:00 in JDBC

前端 未结 10 1645
一向
一向 2020-11-27 11:01

I get an exception (see below) if I try to do

resultset.getString(\"add_date\");

for a JDBC connection to a MySQL database containing a DA

相关标签:
10条回答
  • 2020-11-27 11:20

    I suggest you use null to represent a null value.

    What is the exception you get?

    BTW:

    There is no year called 0 or 0000. (Though some dates allow this year)

    And there is no 0 month of the year or 0 day of the month. (Which may be the cause of your problem)

    0 讨论(0)
  • 2020-11-27 11:24

    I stumbled across this attempting to solve the same issue. The installation I am working with uses JBOSS and Hibernate, so I had to do this a different way. For the basic case, you should be able to add zeroDateTimeBehavior=convertToNull to your connection URI as per this configuration properties page.

    I found other suggestions across the land referring to putting that parameter in your hibernate config:

    In hibernate.cfg.xml:

    <property name="hibernate.connection.zeroDateTimeBehavior">convertToNull</property>
    

    In hibernate.properties:

    hibernate.connection.zeroDateTimeBehavior=convertToNull
    

    But I had to put it in my mysql-ds.xml file for JBOSS as:

    <connection-property name="zeroDateTimeBehavior">convertToNull</connection-property>
    

    Hope this helps someone. :)

    0 讨论(0)
  • 2020-11-27 11:24
    DATE_FORMAT(column name, '%Y-%m-%d %T') as dtime
    

    Use this to avoid the error. It return the date in string format and then you can get it as a string.

    resultset.getString("dtime");
    

    This actually does NOT work. Even though you call getString. Internally mysql still tries to convert it to date first.

    at com.mysql.jdbc.ResultSetImpl.getDateFromString(ResultSetImpl.java:2270)

    ~[mysql-connector-java-5.1.15.jar:na] at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5743)

    ~[mysql-connector-java-5.1.15.jar:na] at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576)

    ~[mysql-connector-java-5.1.15.jar:na]

    0 讨论(0)
  • 2020-11-27 11:26

    If, after adding lines:

    <property
    name="hibernate.connection.zeroDateTimeBehavior">convertToNull</property>
    
    hibernate.connection.zeroDateTimeBehavior=convertToNull
    
    <connection-property
    name="zeroDateTimeBehavior">convertToNull</connection-property>
    

    continues to be an error:

    Illegal DATETIME, DATE, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00 00:00:00' or '0000-00-00').
    

    find lines:

    1) resultSet.getTime("time"); // time = 00:00:00
    2) resultSet.getTimestamp("timestamp"); // timestamp = 00000000000000
    3) resultSet.getDate("date"); // date = 0000-00-00 00:00:00
    

    replace with the following lines, respectively:

    1) Time.valueOf(resultSet.getString("time"));
    2) Timestamp.valueOf(resultSet.getString("timestamp"));
    3) Date.valueOf(resultSet.getString("date"));
    
    0 讨论(0)
提交回复
热议问题