解决 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

该字段的值将表示为空

来源:网络//作者:admin/更新时间:2013-12-30
相关文章
评论:
验证码:
匿名评论:

最新文章

新热推荐

文章排行