博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(转)can not be represented as java.sql.Timestamp
阅读量:4280 次
发布时间:2019-05-27

本文共 1441 字,大约阅读时间需要 4 分钟。

转载说明:做ETL工具从mysql导入pgsql的时候,mysql中datetime字段值'0000-00-00'时无法转换,参考blog方法,连接字符串添加后面字段,可以实现忽略这个值,将0转换为null。

 

使用hibernate开发程序的时候,有的时间字段没有必要填写,但是,以后hibernate查询的时候会报出

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

的错误, 这是因为hibernate认为这个不是一个有效的时间字串。

而有效的日期格式为“ 0001-01-01   00:00:00.0 ”

查看了mysql5的帮助文档对于datetime的解释如下

Datetimes with all-zero components (0000-00-00 ...) — These values can not be represented 关于所有Datetime类型由0组成的数据,这些值不能在java中被可靠的表示

reliably in Java. 
Connector/J 3.0.x always converted them to NULL when being read from a ResultSet. 
当这些值正在从ResultSet容器中读取时候,Connector/J 3.0.x 一直把他们转换为NULL值。

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.

依照JDBC和SQL的标准这些值碰到的最正确的处理方式就是在缺省情况下产生异常
This behavior can be modified using the zeroDateTimeBehavior configuration property. The allowable values are: 
JDBC允许用下列的值对zeroDateTimeBehavior 属性来设置这些处理方式,

exception (the default), which throws an SQLException with an SQLState of S1009. 

设置为exception 异常(缺省)用一个SQLState的s1009错误号来抛出一个异常
convertToNull, which returns NULL instead of the date. 
设置为convertToNull,用NULL值来代替这个日期类型
round, which rounds the date to the nearest closest value which is 0001-01-01. 
设置为round,则围绕这个日期最接近的值(0001-01-01)来代替

 

修改你的jdbc连接

jdbc:mysql://localhost/schoolmis?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull

你可能感兴趣的文章
CarPlay介绍
查看>>
CarPlay wireless(蓝牙+WiFi)连接方案(蓝牙部分)
查看>>
CarPlay wireless(蓝牙+WiFi)连接方案(Wi-Fi部分)
查看>>
CarPlay wired连接与wireless连接相互切换
查看>>
USB linux NCM usbnet驱动详解
查看>>
USB OTG规范的SRP和HNP协议
查看>>
usb驱动的层次结构简述
查看>>
控制Linux内核启动中的打印
查看>>
创建一个简单的debugfs文件系统节点
查看>>
创建一个procfs文件系统的节点
查看>>
高通平台手机开发之Sensor
查看>>
Android4.x 如何处理Power按键
查看>>
创建一个简单的device_create_file文件节点
查看>>
android linux 休眠 深度睡眠 查看 方法 调试
查看>>
ubuntu mount cifs
查看>>
一个简单的framebuffer的显示使用例子
查看>>
inux下串口编程简单实例
查看>>
SELinux app权限配置
查看>>
Linux下的串口编程
查看>>
pthread_create Linux函数 线程创建
查看>>