mysql错误:Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp;的解决方法
程序员文章站
2022-04-21 15:37:15
...
java连接MySQL数据库,字段类型timestamp,数据库值为0000-00-00 00:00:00时,解析数据时会默认抛出一个异常:java.sql.SQLException: Cannot convert value ‘0000-00-00 00:00:00’ from column XX to TIMESTAMP
举个实际的例子,你的方法中要传入来自页面的日期参数值,
按照正常的做法,比如日期值为2016-10-11,但是由于误操作,传入了0000-00-00,并没有设置正确的数据,那么这时默认抛出java.sql.SQLException异常
(如果设定这一项 zero datetime behavior(英文字面意思为“0datetime反应”)=convert to null(英文字面意思为“转化为null”)
,把日期转换为null代替异常处理):
即这类操作情况的处理策略,有3种
1.exception(不指定,则默认)---->默认抛出异常,
2.convertToNull------->转化为null
3.round------->替换成最近的日期即XXXX-01-01
修改连接为:
jdbc.url=jdbc:mysql://localhost:3406/dbname?zeroDateTimeBehavior=convertToNull
上一篇: 产业链:Intel加速推进10nm独显 明年年中发布
下一篇: 好似又学到一招
推荐阅读
-
解决:java.sql.SQLException:Value ‘0000-00-00‘ can not be represented as java.sql.Date
-
java.sql.SQLException: Value ‘0000-00-00 00:00:00‘ can not be represented as java.sql.Timestamp异常
-
mysql的timestamp类型字段为'0000-00-00 00:00:00'导致mybatis映射时报错解决方法
-
FAQ(60):java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date
-
java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Tim 异常解决办法
-
mysql错误:Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp;的解决方法
-
FAQ(64):java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date
-
java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date(已解决)
-
java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp at
-
java.sql.SQLException: Value ‘0000-00-00 00:00:00’ can not be represented as java.sql.Timestamp