java - 无法将日期从 mysql 检索到 JDBC

标签 java mysql jdbc

我目前遇到的问题是我无法从我的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

有谁知道如何解决这个问题。

我已经尝试过thisthis但我仍然遇到同样的两个错误,这两个错误都是由日期引起的

最佳答案

除了其他人的回答(包括存储无效日期不是一个好主意的非常好建议)之外,您还可以指示 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/

相关文章:

java - 处理动态数据库连接

java - 为什么我的应用程序在手机重启后尝试打开文件时崩溃?

java - 将数字和小数四舍五入到最接近的百位

php - 使用正则表达式返回 mysql 字符串

mysql - 两个表的sql命令怎么写?

java - 如何将总和放入字符串中并打印它

java - IPBoard 密码哈希问题,与算法不匹配

java - Android Sqlite 选择查询值是否为零或 0

java - 将 RGB 转换为 HSV

Java:无法使用 JDBC ODBC 更新 Excel