mssql-异常value '0000-00-00' can not be represented as java.sql.Date

本文解决在使用Mysql的ResultSet获取日期型数据时遇到的'000-00-00'异常问题,通过修改JDBC连接字符串参数zeroDateTimeBehavior为convertToNull,避免异常并正确读取日期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mysql开发中采用ResultSet取值時,不管是才用getString()还是用getDate(),或者getObject,均会拋出如题所述异常.查阅Mysql官方Bug咨询:

 

是因为日期型(Date或DateTime类型)字串为'000-00-00'时,MySQL预设处理方式是拋出异常(exception).只要将jdbc数据库连接字串作如下修改即可:


修改前jdbc连接字符串为:  

jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&characterSetResults=UTF-8

 


修改後jdbc连接字符串为 : 

 jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull

 


原文如下:

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.

 

转载于:https://www.cnblogs.com/hwaggLee/p/5230429.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值