在Mysql数据库中使用DATETIME类型来存储时间,使用JDBC中读取这个字段的时候,应该使用 ResultSet.getTimestamp(),这样会得到一个java.sql.Timestamp类型的数据。在这里既不能使用 ResultSet.getDate(),也不能使用ResultSet.getTime(),因为前者不包括time数据,后者不包括date数据。但是在使用ResultSet.getTimestamp()时也不是完全安全的,例如,当数据库中的TIMESTAMP类型的字段值为 '0000-00-00 00:00:00'时,使用此方法进行读取,会抛出异常:Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP,这是因为JDBC不能将'0000-00-00 00:00:00'转化为一个为一个java.sql.Timestamp,在Java中,想创建一个java.util.Date,使其值为 '0000-00-00'也是不可能的,最古老的日期应该是'0001-01-01 00:00:00'。那么在程序中该怎么办捏? 解决方案在这里:Datetimes with all-zero components (0000-00-00 ...) — These values can not be represented reliably in Java. Connector/J 3.0.x always converted them to NULL when being read from a ResultSet.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. This behavior can be modified using the zeroDateTimeBehavior configuration property. The allowable values are:exception (the default), which throws an SQLException with an SQLState of S1009.convertToNull, which returns NULL instead of the date.round, which rounds the date to the nearest closest value which is 0001-01-01.Starting with Connector/J 3.1.7, ResultSet.getString() can be decoupled from this behavior via noDatetimeStringSync=true (the default value is false) so that you can retrieve the unaltered all-zero value as a String. It should be noted that this also precludes using any time zone conversions, therefore the driver will not allow you to enable noDatetimeStringSync and useTimezone at the same time.所以,在JDBC URL中加入zeroDateTimeBehavior信息,既可以解决:String url = "jdbc:mysql://10.149.51.80:3306/test?relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull";
详细解决方案
java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 七 to TIMESTAMP
热度:248 发布时间:2016-05-05 14:15:43.0
相关解决方案
- java 乱码 汉字是 ? 如何转换成汉字
- java web 登录次数限制,该如何解决
- java 工商银行网银支付 B2B的 都亟需什么,需要注意什么
- Java Applet程序从JDK6升级到7时遇到的有关问题
- java web 视频相干
- java 系统单点登录解决方案
- java 获取客户端IP解决办法
- JAVA 后台怎么获得前台页面FCKedit编辑器中的内容以及内容的样式
- java 正则化匹配有关问题
- Expression dqgrade is undefined on line 20, column 30-freemarker报这样的错如何解决
- JAVA WEB导航条,该怎么处理
- java 中Node 有关问题
- 吐了,java Timer 终止不了。
- java web开发解决办法
- 关于 java cast 有关问题,你明白不
- java ftp 下传和上载乱码有关问题
- java web项目上的一些文件
- Hibernate操作数据库 报错java.lang.Integer cannot be cast to java.lang.String如何改
- java struts 文件上载
- java web baidu map开发
- JAVA WEB导航条解决办法
- java 编写ftp上载
- java web中的一个有关问题,困扰小弟我很久了
- java web 与tomcat解决办法
- java web基于j2ee的一些有关问题
- java 内网外网的一个需求,该怎么处理
- jsp 页面 安插ArrayList 报错。为什么,已经加了import="java.util."了呀!
- java dwg 怎样用java把dwg便是aoutcad的格式文件转换成gif,jpg等格式
- 求《Java Web 程序设计与项目实践》解决办法
- java webservice解决方法