解决 java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
最近在做dbunit测试的时候把mysql数据库的数据导出到xml中遇到了如下错误:
Exception in thread "main" org.dbunit.dataset.DataSetException: java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
at org.dbunit.database.ForwardOnlyResultSetTable.getValue(ForwardOnlyResultSetTable.java:108)
at org.dbunit.dataset.DefaultTable.addTableRows(DefaultTable.java:139)
at org.dbunit.database.CachedResultSetTable.<init>(CachedResultSetTable.java:69)
at org.dbunit.database.CachedResultSetTableFactory.createTable(CachedResultSetTableFactory.java:52)
at org.dbunit.database.QueryTableIterator.getTable(QueryTableIterator.java:151)
at org.dbunit.dataset.stream.DataSetProducerAdapter.produce(DataSetProducerAdapter.java:83)
at org.dbunit.dataset.xml.FlatXmlWriter.write(FlatXmlWriter.java:124)
at org.dbunit.dataset.xml.FlatXmlDataSet.write(FlatXmlDataSet.java:378)
at dbunit.Test.main(Test.java:27)
Caused by: java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.ResultSetRow.getTimestampFast(ResultSetRow.java:1102)
at com.mysql.jdbc.ByteArrayRow.getTimestampFast(ByteArrayRow.java:127)
at com.mysql.jdbc.ResultSetImpl.getTimestampInternal(ResultSetImpl.java:6592)
at com.mysql.jdbc.ResultSetImpl.getTimestamp(ResultSetImpl.java:6192)
at org.dbunit.dataset.datatype.TimestampDataType.getSqlValue(TimestampDataType.java:161)
at org.dbunit.database.ForwardOnlyResultSetTable.getValue(ForwardOnlyResultSetTable.java:104)
... 8 more
java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp是因为时间字段里有'0000-00-00 00:00:00‘这种格式的日期导致的
解决方法,把这种日期换成标准的日期格式,年月日不能用0表示
或者在mysql连接的时候添加如下字符串
jdbc:mysql://localhost/mydb?zeroDateTimeBehavior=convertToNull
该字段的值将表示为空
顶
踩
相关文章: