Value '0000-00-00' can not be represented as java.sql.Date

原文  http://josh-persistence.iteye.com/blog/2098415


java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp


问题描述, 在java应用程序中,数据表中有记录的time字段(属性为timestamp)其值为:“0000-00-00 00:00:00”


程序使用select 语句从中取数据时出现以下异常:


java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date


这是因为 “0000-00-00 00:00:00”在mysql中是作为一个特殊值存在的,但是在Java中, java.sql.Date 会被视为 不合法的值,被JVM认为格式不正确。


解决办法:


在jdbc的url加上   zeroDateTimeBehavior参数:


datasource.url=jdbc:mysql://localhost:3306/pe?useUnicode=true&characterEncoding=gbk &zeroDateTimeBehavior=convertToNull


对于值为0000-00-00   00:00:00(默认值)的纪录,根据不同的配置,会返回不同的结果:


不配置:默认返回异常


zeroDateTimeBehavior=round   0001-01-01   00:00:00.0


zeroDateTimeBehavior=convertToNull   null


但 这样有可能会报新的Exception:


The reference to entity "characterEncoding" must end with the ';' delimiter   


其原因可能是在Properties文件或者xml文件中忘记进行特殊符号的转译了,


jdbc:mysql://192.168.1.155:3306/diandi?useUnicode=true&characterEncoding=UTF-8 & zeroDateTimeBehavior=convertToNull


需要改为:


jdbc:mysql://192.168.1.155:3306/diandi?useUnicode=true&characterEncoding=UTF-8 & zeroDateTimeBehavior=convertToNull 


有以下几类字符要进行转义替换:


&lt; < 小于号


&gt; > 大于号


&amp; &


&apos; ' 单引号


&quot; " 双引号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值