KEEP ON CODING......

Mysql 时间格式默认空串 '0000-00-00 00:00:00' select抛出异常的解决方法

Mysql 时间格式默认插入值为空时,会以'0000-00-00 00:00:00'填充,这时如果select时会抛出SQLExecption如下:

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

 

解决方法如下:

方法一:jdbc的url加入zeroDateTimeBehavior参数: 

datasource.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true 

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

(摘自:http://www.blogjava.net/hilor/articles/164814.html)

 

方法二:select 语句中做如下处理:

SELECT ID, IF(createDate='0000-00-00 00:00:00','null',createDate)createDate FROM T_DateTest;

这里将createDate格式转换为”null“展示,不再抛出SQLException。

 

posted @ 2016-05-30 19:54  Cecil2020  阅读(13917)  评论(0编辑  收藏  举报