mysql数据库中日期类型为datetime,而用mybatis映射失败,报java.sql.SQLException: Cannot convert value '0000-00-00 00:00:
来源:互联网 发布:瘦脸针 皮肤松弛 知乎 编辑:程序博客网 时间:2024/05/24 03:14
环境:mysql数据库+mybatis
mysql的某个字段为日期,它的类型用的是datetime。然后框架用mybatis,然后查询的时候进行映射操作是报
java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 7 to TIMESTAMP. 解决有关资料:在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";
- mysql数据库中日期类型为datetime,而用mybatis映射失败,报java.sql.SQLException: Cannot convert value '0000-00-00 00:00:
- mysql数据库中日期类型为datetime,而用mybatis映射失败,报java.sql.SQLException: Cannot convert value '0000-00-00 00:00:
- mysql 数据库字段为datetime类型时默认值为:0000-00-00 00:00:00 查询时异常 java.sql.SQLException:Value '0000-00-00' can not be represented as java.sq
- java的JDBC连接mysql数据库时对于Datetime类型出现Cannot convert value '0000-00-00 00:00:00' from column 9 to TIMESTAMP.
- java的JDBC连接mysql数据库时对于Datetime类型出现Cannot convert value '0000-00-00 00:00:00' from column 9 to TIMESTAMP
- java java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 5 to TIMESTAMP.
- java java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 5 to TIMESTAMP.
- java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 7 to TIMESTAMP
- Mysql 报错java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timest
- 解决mysql 数据库中日期类型00:00:00 的问题 设置xml数据类型:java.util.Date
- java.sql.SQLException: Value'0000-00-00'异常解决
- java.sql.SQLException: Value'0000-00-00'异常解决办法
- java.sql.SQLException: Value'0000-00-00'错误解决方法
- java.sql.SQLException: Value'0000-00-00'异常解决办法
- java.sql.SQLException: Value'0000-00-00'异常解决办法[转]
- java.sql.SQLException: Value'0000-00-00'异常解决办法
- java.sql.SQLException: Value'0000-00-00'异常解决办法及mysql的url设置
- mysql时间格式默认为null抛出java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java
- 数据库中varchar
- RTP/RTCP的实现
- 计算距离国庆多少天
- mysql下载、安装、启动、使用、卸载、常见错误
- 日志记录过滤器
- mysql数据库中日期类型为datetime,而用mybatis映射失败,报java.sql.SQLException: Cannot convert value '0000-00-00 00:00:
- quickSort
- Lua的数据结构
- Filter(过滤器)简介和工作原理
- JSP pageEncoding charset 区别
- ERROR 1005 (HY000): Can't create table 'XXXXX' (errno: 150)
- NSBundle介绍
- C++ typedef用法小结
- 目标检测的图像特征提取之(一)HOG特征——http://blog.csdn.net/zouxy09/article/details/7929348