Mysql-提示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-提示java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 7 to TIMESTAMP.

时间: 2024-11-06 23:18:21

Mysql-提示java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 7 to TIMESTAMP.的相关文章

连接mysql时提示java.sql.SQLException: Access denied for user 'root'@'DESKTOP-N2B2D9A' (using password: YES)

用root连接mysql时提示:访问被拒绝 检查一下mysql server是否开启,发现后台在运行着..  然后查了一下mysql的用户表,发现root只能运行使用本地ip(localhost或者127.0.0.1)进行连接 解决方法:新开一个权限GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'jeffw' WITH GRANT OPTION; 然后查询一下:select * from user; 新开的root的已经开启权限,

mysql 保存emoji时报,数据库报错:Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x82\xF0\x9F...' for column 'review' at row 1

错误原因:我们可以看到错误提示中的字符0xF0 0x9F 0x98 0x84 ,这对应UTF-8编码格式中的4字节编码(UTF-8编码规范).正常的汉字一般不会超过3个字节,为什么为出现4个字节呢?实际上是它对应的是智能手机输入法中的表情.那为什么会报错呢?因为mysql中的utf-8并不是真正意义上的utf-8,它只能存储1~3个字节长度的utf-8编码,如果想存储4个字节的必须用utf8mb4类型.不而要使用utf8mb4类型,首先要保证Mysql版本要不低于 MySQL 5.5.3. 常用

Error getting nested result map values for 'user_inf'. Cause: java.sql.SQLException: Cannot convert

问题?Error getting nested result map values for 'user_inf'.  Cause: java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 8 to TIMESTAMP. 错误:org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.  Cause:

mysql插入报错:java.sql.SQLException: Incorrect string value: '\xE6\x9D\xAD\xE5\xB7\x9E...' for column 'address' at row 1

界面报错: 日志报错: java.sql.SQLException: Incorrect string value: '\xE6\x9D\xAD\xE5\xB7\x9E...' for column 'address' at row 1 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.17.jar:8.0.17] at com.mys

mysql: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x90</...'

插入数据出现问题,因为包含了特殊字符. 现象: 插入的数据中如果含有某些特殊字符,会导致插入数据失败,例如字符串”测试**插入数据...“,在console中insert是正常的,但是使用java代码insert的时候报错. 原因: mysql utf-8编码最多只支持三个字节,而有的unicode字符转成utf8编码之后有4个字节,所以插入失败. 解决方案: 修改Content字段为MEDIUMBLOB(原来是varchar),并且把SELECT语句修改成 SELECT CAST(Conten

mysql报错:java.sql.SQLException: Incorrect string value: ‘\xE4\xB8\x80\xE6\xAC\xA1...‘ for column ‘excelName‘ at row 1

一.问题 用Eclipse做项目时候报错 java.sql.SQLException: Incorrect string value: '\xE4\xB8\x80\xE6\xAC\xA1...' for column 'excelName' at row 1.而且就只有这个表的插入有问题. 二.解决 通过对比其它可以正常插入的表,发现正常的表的字符编码格式和有问题的这张表的不一样.利用Navicat工具修改字段的字符编码,问题成功解决.     原文地址:https://www.cnblogs.

java.sql.SQLException: Incorrect string value: '\xF0\x9F\x9A\x80\xF0\x9F...' for column 'name' at row 1

1.异常提示: 12:59:10.000 [http-nio-8080-exec-40] DEBUG o.s.j.s.SQLStateSQLExceptionTranslator - Extracted SQL state class 'HY' from value 'HY000' 12:59:10.000 [http-nio-8080-exec-40] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional Sq

nested exception is java.sql.SQLException: Incorrect string value: '\xE7\x99\xBB\xE9\x99\x86...' for column 'image' at row 1

HTTP Status 500 - Hibernate operation: could not insert: [cn.itcast.shop.product.vo.Product]; uncategorized SQLException for SQL [insert into product (pname, market_price, shop_price, image, pdesc, is_hot, pdate, csid) values (?, ?, ?, ?, ?, ?, ?, ?)

解决java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'

今天使用mysql,用java程序往mysql中写数据,出现如下错误: Caused by: java.sql.SQLException: Incorrect string value: '\xD3\xCD\xBB\xAD' for column 'type' at row 1 解法: 创建每张表的时候都必须设置数据编码方式: drop table if exists we_artwork_desc; CREATE TABLE we_artwork_desc ( id VARCHAR(200),