Value '0000-00-00' can not be represented as java.sql.Date

后端 未结 6 1204
名媛妹妹
名媛妹妹 2020-12-05 08:43

I\'m working on some project which needs to extract data from DB and I use Spring MVC to build model from DB for selecting data.

Here is the problem with my

相关标签:
6条回答
  • 2020-12-05 09:01

    For me simply the following worked. Thanks to @Yogesh H Bhosale and @fthiella answer in this same Q-A thread I found the hint, and improved it.

    Code Snippet:
    Connection String:

    private static String CONNECTION_STR = "jdbc:mysql://localhost:3306/water_billing?zeroDateTimeBehavior=convertToNull";
    

    SQL-DB implementation:

    And to prevent from Null Pointer Exception at the time of data(date field) extraction from the returned data set I add the following code to the Prepared Statement.

    Connection conn = DriverManager.getConnection(CONNECTION_STR,USERNAME,PASSWORD);   //   Get connection to the DB to execute the query. You will have to handle exceptions here too..
    String SQL = "SELECT dateField FROM my_payments_tbl";
    ResultSet rs = conn.createStatement().executeQuery(SQL);
    LocalDate prvPaymentDate = null;
    
    while (rs.next())
    {
         // Following is the next Important line.
         prvPaymentDate = (rs.getDate(1) != null) ? rs.getDate(1).toLocalDate() : null;    // Checks if it comes as a Null or a Real java.sql.Date object.
    
         // Depending on the value of the column in the current row, prvPaymentDate could be null.
         // Specially as we instructed via the Connection String, 0000-00-00 00:00:00 will come now as SQL NULL objects.
         // So we filter out them in the above line and assigns null, instead of LocalDate objects.
         if (prvPaymentDate != null)
            System.out.println("date "+prvPaymentDate.toString());
    }
    

    Note:
    Please read all the comments within the answer for good understanding.

    0 讨论(0)
  • 2020-12-05 09:02

    It is NOT always the NULL issue. Your data set may contain dates in the format 05/23/2005 and running the loader will cause it to insert 0000-00-00 into your DATE type column. Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00') (from MySQL manual)

    While retrieving using a SELECT, MySQL retrieves the value in YYYY-MM-DD' format in the range of '1000-01-01' to '9999-12-31'. So all your 0000-00-00 starts screwing up.

    So watch out not just for NULLs but also for date formats not supported by MySQL.

    There is RELAXED mode supported where any character can be used as delimiters like / in 05/23/2005 - you may want to try that as well.

    0 讨论(0)
  • 2020-12-05 09:10

    My wild guess is that you're using MySQL, your date columns are not mandatory and hence can contain NULLs, but MySQL does idiotic things and will return 0000-00-00 instead of NULL unless you fix the MySQL server settings.

    0 讨论(0)
  • 2020-12-05 09:11

    Append the following statement with the JDBC-mysql protocol:

    "?zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=UTF-8&characterSetResults=UTF-8"
    

    for Example:

    jdbc:mysql://localhost/infra?zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=UTF-8&characterSetResults=UTF-8
    
    0 讨论(0)
  • 2020-12-05 09:11

    Add zeroDateTimeBehavior=convertToNull to the jdbc connection URL.

    String jdbcUrl="jdbc:mysql://localhost:3306/master_schema?zeroDateTimeBehavior=convertToNull"
    
    0 讨论(0)
  • 2020-12-05 09:23

    In MySql '0000-00-00' is considered a valid date, but it can't be repesented as java.sql.Date.

    You could use a query that returns NULL in case date is '0000-00-00', or the actual value otherwise:

    SELECT
      CASE WHEN `date`!='0000-00-00' THEN `date` END new_date
    FROM
      yourtable
    

    or you can add to your datasource connection string this:

    zeroDateTimeBehavior=convertToNull
    

    and dates as '0000-00-00' will be automatically converted to NULL.

    0 讨论(0)
提交回复
热议问题