当前位置: 代码迷 >> 综合 >> java.sql.SQLException: Value '0000-00-00 ' can not be represented as java.sql.Timest
  详细解决方案

java.sql.SQLException: Value '0000-00-00 ' can not be represented as java.sql.Timest

热度:53   发布时间:2024-01-12 09:36:22.0

在使用MySql 时, 数据库中的字段类型是timestamp的,默认为0000-00-00, 会发生异常:java.sql.SQLException:   Value   '0000-00-00 '   can   not   be   represented   as   java.sql.Timestamp。

原因:“0000-00-00 00:00:00”在mysql中是作为一个特殊值存在的,而 java.sql.Date 将其视为 不合法的值 格式不正确因此产生异常

解决办法:

jdbc的url加上zeroDateTimeBehavior参数:

如:jdbc.jdbcUrl=jdbc\:mysql\://127.0.0.1\:3306/hysdebugdb?useSSL\=false&zeroDateTimeBehavior=convertToNull

对于值为0000-00-00   00:00:00(默认值)的纪录,如下两种配置,会返回不同的结果:

(1)zeroDateTimeBehavior=round      0001-01-01   00:00:00.0

(2)zeroDateTimeBehavior=convertToNull      null