Value '0000-00-00' can not be represented as java.sql.Date
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Value '0000-00-00' can not be represented as java.sql.Date,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1582字,纯文字阅读大概需要3分钟。
内容图文
![Value '0000-00-00' can not be represented as java.sql.Date](/upload/InfoBanner/zyjiaocheng/462/5b1df1fae4d54902ae27ad47c788ad13.jpg)
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
有以下几类字符要进行转义替换:
< |
< |
小于号 |
> |
> |
大于号 |
& |
& |
和 |
' |
‘ |
单引号 |
" |
" |
双引号 |
Value '0000-00-00' can not be represented as java.sql.Date
标签:
本文系统来源:http://www.cnblogs.com/yanduanduan/p/5369344.html
内容总结
以上是互联网集市为您收集整理的Value '0000-00-00' can not be represented as java.sql.Date全部内容,希望文章能够帮你解决Value '0000-00-00' can not be represented as java.sql.Date所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。