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。