MySQL 错误【四】Value ‘0000-00-00 00:00:00’ can not be represented as java.sql.Timestamp

1. 错误描述

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

2. 解决办法

给数据库的 jdbc.url 加上 zeroDateTimeBehavior 参数,如下:

jdbc.url=jdbc:mysql://127.0.0.1:3306/seckill?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=round

注意参数间不能有空格;否则报错 org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection

zeroDateTimeBehavior参数有两种配置:

  1. zeroDateTimeBehavior=round ,”0000-00-00“会默认转换为”0001-01-01 00:00:00”
  2. zeroDateTimeBehavior=convertToNull,“0000-00-00“会转换为null
posted @ 2022-03-04 23:03  涤心  阅读(270)  评论(0)    收藏  举报