[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[转帖]Value '0000-00-00' can not be represented as java.sql.Timestamp

上一篇:[备忘]flash builder 4.7插件,只能安装在eclipse的4.2版本,即juno
下一篇:[备忘]画流程图,uml图的小软件:DiaPortable

添加日期:2015/7/2 12:59:05 快速返回   返回列表 阅读2333次
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
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved