Value '0000-00-00' can not be represented as java.sql.Date
2016-04-08 18:33
696 查看
Value '0000-00-00' can not be represented as java.sql.Date
时间 2014-07-30 09:00:50 ITeye-博客原文 http://josh-persistence.iteye.com/blog/2098415
主题 Java SQL
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
有以下几类字符要进行转义替换:
< | < | 小于号 |
> | > | 大于号 |
& | & | 和 |
' | ' | 单引号 |
" | " | 双引号 |
相关文章推荐
- HDU Rescue
- Quartz 2D中UIImage的分类
- Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (144900 > 1024)
- 关于java中几个CP的一点说明--stringBuilder和stringBuffer与hashTable和hashMap
- String、StringBuffer、StringBuilder简单理解
- Qt 示例学习--3 gallery quick controls
- UEditor1.4.3关于上传文件绝对路径的配置
- easyui 添加不重复的tab选项卡
- Virtual machine steal BlueTooth from Host(Mac)
- HDU:1242 Rescue(BFS+优先队列)
- 实际用户和有效用户的
- UIButton的基本用法和事件
- easyui验证
- iOS _监听UITextField输入状态
- 2786: [JSOI]Word Query电子字典
- 1007. Maximum Subsequence Sum
- iOS开发学习之 - 深入学习UIButton(1)
- iOS UI绘制和布局基础解析
- JAVA---Unique Characters
- 模板模式 和 策略模式的区别, builder pattern构造者模式,BeanDefinitionBuilder