An exception is thrown when MySQL queries the default time.

Source: Internet
Author: User

An exception is thrown when MySQL queries the default time.

Symptom

A MySQL table is created with a field of the Date type. The default value is 0000-00-00 00:00:00. The PrepareStatement is used for query and the query result is ResultSet, the Date Field Retrieved From the result uses ResultSet. getDate ("XXXX"), the result throws an exception: java. SQL. SQLException: Value '2017-00-00 'can not be represented as java. SQL. date.

Cause

In MySQL, the default value of Date is "0000-00-00 00:00:00", but java. SQL. Date considers this as an invalid value, so the above exception is thrown.

Solution

Add the zeroDateTimeBehavior parameter to the URL of the MySQL connection. This parameter can be used to specify the reason why the default value is converted. There are two solutions, one is to convert to "0001-01-01 00:00:00", the other is to convert directly to Null. The corresponding configuration in sequence is as follows:

String url = "jdbc: mysql: // localhost: 3306/test? ZeroDateTimeBehavior = round "; // convert to" 0001-01-01 00:00:00"

String url = "jdbc: mysql: // localhost: 3306/test? ZeroDateTimeBehavior = convertToNull "; // convert to Null

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.