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個成員
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()