java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 7 to TIMESTAMP

在Mysql数据库中使用DATETIME类型来存储时间,使用JDBC中读取这个字段的时候,应该使用 ResultSet.getTimestamp(),这样会得到一个java.sql.Timestamp类型的数据。在这里既不能使用 ResultSet.getDate(),也不能使用ResultSet.getTime(),因为前者不包括time数据,后者不包括date数据。

但是在使用ResultSet.getTimestamp()时也不是完全安全的,例如,当数据库中的TIMESTAMP类型的字段值为 '0000-00-00 00:00:00'时,使用此方法进行读取,会抛出异常:Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP,这是因为JDBC不能将'0000-00-00 00:00:00'转化为一个为一个java.sql.Timestamp,在Java中,想创建一个java.util.Date,使其值为 '0000-00-00'也是不可能的,最古老的日期应该是'0001-01-01 00:00:00'。

那么在程序中该怎么办捏? 解决方案在这里:

Datetimes with all-zero components (0000-00-00 ...) — These values can not be represented reliably in Java. Connector/J 3.0.x always converted them to NULL when being read from a ResultSet.

Connector/J 3.1 throws an exception by default when these values are encountered as this is the most correct behavior according to the JDBC and SQL standards. This behavior can be modified using the zeroDateTimeBehavior configuration property. The allowable values are:

exception (the default), which throws an SQLException with an SQLState of S1009.
convertToNull, which returns NULL instead of the date.
round, which rounds the date to the nearest closest value which is 0001-01-01.
Starting with Connector/J 3.1.7, ResultSet.getString() can be decoupled from this behavior via noDatetimeStringSync=true (the default value is false) so that you can retrieve the unaltered all-zero value as a String. It should be noted that this also precludes using any time zone conversions, therefore the driver will not allow you to enable noDatetimeStringSync and useTimezone at the same time.

所以,在JDBC URL中加入zeroDateTimeBehavior信息,既可以解决:
String url = "jdbc:mysql://10.149.51.80:3306/test?relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:问题?Error getting nested result map values for 'user_inf'. Cause: java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 8 to TIMESTAMP. 引用\[2\]:今天在写完sql语句,运行时报如下错误: java.sql.SQLException: 无法转换为内部表示; 第一次遇到这个问题,反复查看sql,在pl/sql里运行sql语句都是正常的;查阅资料,才发现是自己将查询结果映射成vo对象时,数据类型不一致造成的。 引用\[3\]:当看到这个报错的时候不要慌,上网查是为啥报错,网上的方法解决可能会管用,但是你看的懂吗?反正我是看不懂,还不一定是时间类型不对,或者时间转换不对,是的,有可能是因为你的数据库中时间字段写了别的字符,不是时间类型,就会报这个错。 问题:Cause: java.sql.SQLException: Cannot convert string '\xAC\xED\x00\x05ur...' from binary to utf8mb4 回答: 这个错误是由于数据库中的数据类型与程序中的数据类型不一致导致的。在引用\[2\]中提到,可能是将查询结果映射成vo对象时,数据类型不一致所致。另外,在引用\[3\]中也提到,这个错误可能是由于数据库中的时间字段写了别的字符,不是时间类型所导致的。总之,这个错误的原因是数据类型不一致。解决这个问题的方法可以参考网上的方法,但需要注意确保理解这些方法的含义和适用情况。 #### 引用[.reference_title] - *1* [Error getting nested result map values for 'user_inf'. Cause: java.sql.SQLException: Cannot convert](https://blog.csdn.net/Mr_li13/article/details/51346308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [java.sql.SQLException: 无法转换为内部表示原因及解决办法](https://blog.csdn.net/csdn15556927540/article/details/82900024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [### Cause: java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 13 to TIMES](https://blog.csdn.net/qq_38337245/article/details/85065853)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值