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

来源:互联网 发布:sql标识符的命名规则 编辑:程序博客网 时间:2024/04/29 07:00

问题描述:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date

问题描述:

数据表中有记录的time字段(属性为timestamp)其值为:“0000-00-00 00:00:00”

程序使用select 语句从中取数据时出现以下异常:

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

 

后查资料发现 “0000-00-00 00:00:00”在mysql中是作为一个特殊值存在的

但 java.sql.Date 将其视为 不合法的值 格式不正确

 

解决办法:

 

给jdbc   url加上   zeroDateTimeBehavior参数:

datasource.url=jdbc:mysql://localhost:3306/pe?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull


zeroDateTimeBehavior=round是为了指定MySql中的DateTime字段默认值查询时的处理方式;默认是抛出异常,

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

zeroDateTimeBehavior=round

zeroDateTimeBehavior=convertToNull   null