When Java JDBC is connected to the MySQL database, cannot convert value '2017-00-00 00:00:00 'from column appears for the datetime type.

Source: Internet
Author: User

Use Hibernate to connect to the database of mysql5. Since the table uses a datetime time period, the following

Java. SQL. SQLException: Cannot convert value '2017-00-00 00:00:00 'from column 9 to TIMESTAMP.
At com. mysql. jdbc. ResultSet. getTimestampFromBytes (ResultSet. java: 6864)
At com. mysql. jdbc. ResultSet. getTimestampInternal (ResultSet. java: 6899)
At com. mysql. jdbc. ResultSet. getTimestamp (ResultSet. java: 6218)
At com. mysql. jdbc. ResultSet. getTimestamp (ResultSet. java: 6256)
At org. hibernate. type. TimestampType. get (TimestampType. java: 30)
At org. hibernate. type. NullableType. nullSafeGet (NullableType. java: 113)
At org. hibernate. type. NullableType. nullSafeGet (NullableType. java: 102)

The help document of mysql5 explains datetime as follows:

Datetimes with all-zero components (0000-00-00 ...) -These values can not be represented: for all data of the Datetime type consisting of 0, These values cannot be reliably expressed in java.
Reliably in Java.
Connector/J 3.0.x always converted them to null when being read from a resultset.
When these values are being read from the resultset container, connector/J 3.0.x always converts them to null values.

Connector/J 3.1 throws an exception by default when these values are encountered as this is the most correct behavior according to the JDBC and SQL standards.
According to JDBC and SQL standards, the most correct way to handle these values is to generate exceptions by default.
This behavior can be modified using the zerodatetimebehavior configuration property. The allowable values are:
JDBC allows the following values to be used to set these processing methods for the zerodatetimebehavior attribute,

Exception (the default), which throws an sqlexception with an sqlstate of s1009.
An exception (default) is thrown by an error number of the sqlstate, which is the same as the error number of the sqlstate.
Converttonull, which returns NULL instead of the date.
Set to converttonull. Use null to replace this date type.
Round, which rounds the date to the nearest closest value which is 0001-01-01.
If it is set to round, it is replaced by the closest value of this date (0001-01-01 ).

You can modify your JDBC connection

JDBC: mysql: // localhost/schoolmis? Useunicode = true & amp; characterencoding = utf8 & amp; zerodatetimebehavior = converttonull

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.