[MYSQL]Cannot convert value '0000-00-00 00:00:00' from column 6 to TIMESTAMP / Value '[B@XXXXXXX' can not be represented as java.sql.Timestamp 解法方法
Cannot convert value '0000-00-00 00:00:00' from column 6 to TIMESTAMP /Value '[B@XXXXXXX' can not be represented as java.sql.Timestamp 解法方法
1) 在URI后加上: &zeroDateTimeBehavior=convertToNul
这样一个参考的URI如下:
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNul&transformedBitIsBoolean=true
2) 查看对应表记录中DATETIME取值,看是否存在一些非法取值,因为MYSQL DATETIME取值范围为:1000-01-01 00:00:00 - 9999-12-31 23:59:59
3) 查看记录中DATETIME值中是否存在这种情况,年、月、日、时、分、秒是否为0的情况,如,1990-01-01 00:00:00 及 1990-01-01 01:00:00都可能引起转换出错问题
具体的一些原因请详读下面的内容:
一、DATETIME转换注意的一些细节 Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP(http://hi.baidu.com/jjpro/blog/item/4dfc8900dae09581e950cd84.html)
在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";
当然,也可以使用另外一个策略:round。
二、MySQL中各种字段的取值范围(http://www.54chen.com/c/22)
TINYINT
-128 - 127
TINYINT UNSIGNED
0 - 255
SMALLINT
-32768 - 32767
SMALLINT UNSIGNED
0 - 65535
MEDIUMINT
-8388608 - 8388607
MEDIUMINT UNSIGNED
0 - 16777215
INT 或 INTEGER
-2147483648 - 2147483647
INT UNSIGNED 或 INTEGER UNSIGNED
0 - 4294967295
BIGINT
-9223372036854775808 - 9223372036854775807
BIGINT UNSIGNED
0 - 18446744073709551615
FLOAT
-3.402823466E+38 - -1.175494351E-38
0
1.175494351E-38 - 3.402823466E+38
DOUBLE 或 DOUBLE PRECISION 或 REAL
-1.7976931348623157E+308 - -2.2250738585072014E-308
0
2.2250738585072014E-308 - 1.7976931348623157E+308
DECIMAL[(M,[D])] 或 NUMERIC(M,D)
由M(整个数字的长度,包括小数点,小数点左边的位数,小数点右边的位数,但不包括负号)和D(小数点右边的位数)来决定,M缺省为10,D缺省为0
DATE
1000-01-01 - 9999-12-31
DATETIME
1000-01-01 00:00:00 - 9999-12-31 23:59:59
TIMESTAMP
1970-01-01 00:00:00 - 2037年的某天(具体是哪天我也不知道,呵呵)
TIME
-838:59:59′ to 838:59:59
YEAR[(2|4)]
缺省为4位格式,4位格式取值范围为1901 - 2155,0000,2位格式取值范围为70-69(1970-2069)
CHAR(M) [BINARY] 或 NCHAR(M) [BINARY]
M的范围为1 - 255,如果没有BINARY项,则不分大小写,NCHAR表示使用缺省的字符集.在数据库中以空格补足,但在取出来时末尾的空格将自动去掉.
[NATIONAL] VARCHAR(M) [BINARY]
M的范围为1 - 255.在数据库中末尾的空格将自动去掉.
TINYBLOB 或 TINYTEXT
255(2^8-1)个字符
BLOB 或 TEXT
65535(2^16-1)个字符
MEDIUMBLOB 或 MEDIUMTEXT
16777215 (2^24-1)个字符
LONGBLOB 或 LONGTEXT
4294967295 (2^32-1)个字符
ENUM(’value1′,’value2′,…)
可以总共有65535个不同的值
SET(’value1′,’value2′,…)
最多有64个成员