|
在Mysql数据库中使用DATETIME类型来存储时间,使用JDBC中读取这个字段的时候,应该使用 ResultSet.getTimestamp(),这样会得到一个java.sql.Timestamp类型的数据。在这里既不能使用 ResultSet.getDate(),也不能使用ResultSet.getTime(),因为前者不包括time数据,后者不包括date数据。 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.
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信息,既可以解决: 当然,也可以使用另外一个策略:round |
Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP
最新推荐文章于 2024-10-26 23:30:00 发布
本文介绍在MySQL数据库中使用DATETIME类型存储时间,并通过JDBC读取时遇到的问题及解决方案。当遇到'0000-00-0000:00:00'这种无效日期时,可以通过配置zeroDateTimeBehavior属性来避免异常,如设置为convertToNull返回NULL值。

5856

被折叠的 条评论
为什么被折叠?



