使用hibernate開發程序的時候,有的時間字段沒有必要填寫,但是,以后hibernate查詢的時候會報出
“java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Timestamp”
的錯誤, 這是因為hibernate認為這個不是一個有效的時間字串。
而有效的日期格式為“ 0001-01-01 00:00:00.0 ”
查看了mysql5的幫助文檔對于datetime的解釋如下
Datetimes with all-zero components (0000-00-00 ...) — These values can not be represented 關于所有Datetime類型由0組成的數據,這些值不能在java中被可靠的表示
reliably in Java.
Connector/J 3.0.x always converted them to NULL when being read from a ResultSet.
當這些值正在從ResultSet容器中讀取時候,Connector/J 3.0.x 一直把他們轉換為NULL值。
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.
依照JDBC和SQL的標準這些值碰到的最正確的處理方式就是在缺省情況下產生異常
This behavior can be modified using the zeroDateTimeBehavior configuration property. The allowable values are:
JDBC允許用下列的值對zeroDateTimeBehavior 屬性來設置這些處理方式,
exception (the default), which throws an SQLException with an SQLState of S1009.
設置為exception 異常(缺省)用一個SQLState的s1009錯誤號來拋出一個異常
convertToNull, which returns NULL instead of the date.
設置為convertToNull,用NULL值來代替這個日期類型
round, which rounds the date to the nearest closest value which is 0001-01-01.
設置為round,則圍繞這個日期最接近的值(0001-01-01)來代替
修改你的jdbc連接
jdbc:mysql://localhost/schoolmis?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
posted on 2009-06-20 15:20
ゞ沉默是金ゞ 閱讀(5949)
評論(0) 編輯 收藏 所屬分類:
DB