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

Posted hunter_Cecil

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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。

 

以上是关于Mysql 时间格式默认空串 '0000-00-00 00:00:00' select抛出异常的解决方法的主要内容,如果未能解决你的问题,请参考以下文章