我是靠谱客的博主 踏实抽屉,最近开发中收集的这篇文章主要介绍MySQL錯誤:Value '0000-00-00' can not be represented as java.sql.Date解決方法,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

原文地址:MySQL錯誤:Value '0000-00-00' can not be represented as java.sql.Date解決方法

 

jsp+Tomcat+Mysql開發中采用ResultSet取值時,不管是採用getString()還是採用getDate()時,均會拋出如題所述錯誤.查閱Mysql官方Bug資訊:

http://dev.mysql.com/doc/refman/5.1/en/connector-j-installing-upgrading.html

是因為日期型(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.

最后

以上就是踏实抽屉为你收集整理的MySQL錯誤:Value '0000-00-00' can not be represented as java.sql.Date解決方法的全部内容,希望文章能够帮你解决MySQL錯誤:Value '0000-00-00' can not be represented as java.sql.Date解決方法所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部