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: