我目前遇到的问题是我无法从我的mysql数据库检索日期对象,到目前为止,无论我尝试什么,我要么得到
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
或
java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date
有谁知道如何解决这个问题。
最佳答案
除了其他人的回答(包括存储无效日期不是一个好主意的非常好建议)之外,您还可以指示 JDBC 驱动程序以不同的方式处理这些无效日期:
来自:http://dev.mysql.com/doc/refman/5.5/en/connector-j-installing-upgrading.html
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 permissible 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.
我建议使用convertToNull
参见 here有关如何指定配置属性的说明。
关于java - 无法将日期从 mysql 检索到 JDBC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12606799/