当前位置: 代码迷 >> SQL >> 有关问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql
  详细解决方案

有关问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql

热度:256   发布时间:2016-05-05 13:29:03.0
问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql
问题描述:

数据表中有记录的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   0001-01-01   00:00:00.0

zeroDateTimeBehavior=convertToNull   null

转自 http://blog.csdn.net/ja_ii_ck/archive/2009/02/18/3905120.aspx
  相关解决方案