java 出现“无法将值 '0000-00-00 00:00:00' 从第 12 列转换为时间戳”之类的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3555017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:23:09  来源:igfitidea点击:

Getting the exception like "Cannot convert value '0000-00-00 00:00:00' from column 12 to TIMESTAMP"

javamysql

提问by Rakesh Sabbani

Previously the column Data type is Date now I am changed to Timestamp Now if I tried to run the program am getting them exception

以前列数据类型是日期现在我更改为时间戳现在如果我尝试运行该程序我得到它们异常

java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 12 to TIMESTAMP. at com.mysql.jdbc.ResultSetRow.getTimestampFast(ResultSetRow.java:1298) at com.mysql.jdbc.ByteArrayRow.getTimestampFast(ByteArrayRow.java:124) at com.mysql.jdbc.ResultSetImpl.getTimestampInternal(ResultSetImpl.java:6610) at com.mysql.jdbc.ResultSetImpl.getTimestamp(ResultSetImpl.java:5928) at com.mysql.jdbc.ResultSetImpl.getTimestamp(ResultSetImpl.java:5966) at org.hibernate.type.TimestampType.get(TimestampType.java:30) at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163) at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154) at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81) at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2096) at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380) at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308) at org.hibernate.loader.Loader.getRow(Loader.java:1206) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580) at org.hibernate.loader.Loader.doQuery(Loader.java:701) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236) at org.hibernate.loader.Loader.doList(Loader.java:2220) ... 40 more

java.sql.SQLException:无法将值“0000-00-00 00:00:00”从第 12 列转换为 TIMESTAMP。在 com.mysql.jdbc.ResultSetRow.getTimestampFast(ResultSetRow.java:1298) 在 com.mysql.jdbc.ByteArrayRow.getTimestampFast(ByteArrayRow.java:124) 在 com.mysql.jdbc.ResultSetImpl.getTimestampInternal(ResultSetImpl.java:6610) ) 在 com.mysql.jdbc.ResultSetImpl.getTimestamp(ResultSetImpl.java:5928) 在 com.mysql.jdbc.ResultSetImpl.getTimestamp(ResultSetImpl.java:5966) 在 org.hibernate.type.TimestampType.get(TimestampType.java: 30) 在 org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163) 在 org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154) 在 org.hibernate.type.AbstractType.hydr(AbstractType.java) :81) 在 org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2096) 在 org.

采纳答案by casablanca

0000-00-00 00:00:00is outside the range of a TIMESTAMPvalue (in fact, it won't work with a DATEfield either). From the MySQL manual:

0000-00-00 00:00:00超出TIMESTAMP值的范围(实际上,它也不适用于DATE字段)。从MySQL 手册

The TIMESTAMP data type has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

TIMESTAMP 数据类型的范围是 '1970-01-01 00:00:01' UTC 到 '2038-01-19 03:14:07' UTC。

回答by kitokid

You can just add zeroDateTimeBehavior=convertToNullto your connection jdbc:mysql://localhost/test?zeroDateTimeBehavior=convertToNull.

您可以将zeroDateTimeBehavior=convertToNull添加到您的连接中jdbc:mysql://localhost/test?zeroDateTimeBehavior=convertToNull

For me, it works perfectly. pls refer to this linkfor more detail.

对我来说,它完美地工作。请参阅此链接了解更多详情。

回答by Arun Kasyakar

I'm going to take a wild guess here that you're using MySQL :-) It uses "zero dates" as special placeholder - unfortunatelly, JDBC can not handle them by default.

我将在这里大胆猜测您正在使用 MySQL :-) 它使用“零日期”作为特殊占位符 - 不幸的是,JDBC 默认无法处理它们。

The solution is to specify "zeroDateTimeBehavior=convertToNull" as parameter to your MySQL connection (either in datasource URL or as an additional property), e.g.:

解决方案是将“zeroDateTimeBehavior=convertToNull”指定为 MySQL 连接的参数(在数据源 URL 中或作为附加属性),例如:

jdbc:mysql://localhost/myDatabase?zeroDateTimeBehavior=convertToNull

This will cause all such values to be retrieved as NULLs.

这将导致所有此类值都作为 NULL 检索。

回答by shamittomar

You can Use UNIX_TIMESTAMP(date)function to explicitly convert the value to TIMESTAMP.

您可以使用UNIX_TIMESTAMP(date)函数将值显式转换为 TIMESTAMP。

回答by Thierry-Dimitri Roy

Make sure that in you java code the field type is java.sql.Timestamp

确保在您的 java 代码中,字段类型是 java.sql.Timestamp