Solution for java.sql.SQLException: Zero date value prohibited exception

I encountered this exception when using MyBatis to query data. The solution to the answer on stackoverflow was to add the zeroDateTimeBehavior parameter to the jdbc link.

The actual error should also have:
java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date

jdbc:mysql://yourserver:3306/yourdatabase?zeroDateTimeBehavior=convertToNull

Then I consulted the official manual, and the explanation given is as follows.

zeroDateTimeBehavior What should happen when the driver encounters
DATETIME values that are composed entirely of zeros (used by MySQL to
represent invalid dates)? Valid values are “exception”, “round” and
“convertToNull”. Default: exception Since version: 3.1.4
http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-installing-upgrading-3-0-to-3-1.html

Set the zeroDateTimeBehavior property, when encountering a DATETIME value consisting entirely of zeros, the final valid value can be set to, exception (exception) , an approximate value (round) , or convert this value to null (convertToNull) .

Then check the manual further, and the detailed explanation of the three parameters is as follows.

exception (the default), which throws an SQLException with an SQLState
of S1009.
convertToNull, which returns NULL instead of the date.
round, which rounds the date to the nearest closest value which is
0001-01-01.
http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-installing-upgrading-3-0-to-3-1.html

The default is exception. Setting this property will throw a SQLException with the SQLSate code of S1009. This status code can also be used when writing stored procedures to handle exceptions.

convertToNull returns null to replace dates like 0000-00-00.

round, converts the date to 0001-01-01.

Therefore, the occurrence of 0000-00-00 belongs to an invalid date, and the convertToNull attribute can be used.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324775372&siteId=291194637