处理Sql查询遇到这样一个问题(数据库是MySQL),有个字段定义类型是datetime,且非空无默认值,

通过PHPMyAdmin界面填充测试数据的时候没有理会这个字段,看到个小警告,插入成功,

但是在取数据的时候出现这样一个错误:

Value '0000-00-00 00:00:00' can not be represented asjava.sql.Timestamp

因为数据库访问层是自己封装的架构,所以第一个想法是:“哇,遇到一个新情况”,

于是去看基类的反射处理部分,好像没啥问题,小小的失望了一下,

后来把整个错误扔到网上搜索了一下,嗬,还真不少,中文的英文的××文的都有,

捡个权威的吧(MySQL官方http://dev.mysql.com/doc/refman/5.1/en/connector-j-installing-upgrading.html):

Datetimes withall-zero components (0000-00-00 ...) — These values can not berepresented reliably in Java. Connector/J 3.0.x always convertedthem to NULL when being read from a ResultSet.

Connector/J 3.1 throws an exception by default when thesevalues are encountered as this is the most correct behavioraccording to the JDBC and SQL standards. This behavior can bemodified using the zeroDateTimeBehavior configurationproperty. The allowable values are:

exception (thedefault), which throws an SQLException with an SQLState ofS1009.

convertToNull,which returns NULL instead of the date.

round,which rounds the date to the nearest closest value which is0001-01-01.

Starting with Connector/J 3.1.7, ResultSet.getString() can bedecoupled from this behavior via noDatetimeStringSync=true (thedefault value is false) so that you can retrieve the unalteredall-zero value as a String. It should be noted that this alsoprecludes using any time zone conversions, therefore the driverwill not allow you to enable noDatetimeStringSync and useTimezone atthe same time.

两种解决办法(高亮颜色我加的),两种方法都是在数据库连接串处追加设置,

现举例说下两种情况的结果:

原连接串:driver-url=jdbc:mysql://127.0.0.1/test

使用参数zeroDateTimeBehavior:

取值exception:

driver-url=jdbc:mysql://127.0.0.1/test?zeroDateTimeBehavior=exception

结果:

java.sql.SQLException:Value '0000-00-00 00:00:00' can not be represented asjava.sql.Timestamp

取值convertToNull:

driver-url=jdbc:mysql://127.0.0.1/test?zeroDateTimeBehavior=convertToNull

结果:

ClientDto[id:1,email:test@hotmail.com,civilite:测试用户,prenom:贾,nom:某某,birthday:1992-03-18,telephone:12312345678,address:测试用户的住址,regionid:1,postcode:111000,city:大连,country:france,batiment:110#,escalier:2,code:151515,interphone:050505,etage:东侧走廊,appartment:205,createdate:2010-03-1609:30:21.0,status:0,totalprice:0.00,totalnum:0,lastdate:null,lasttotal:0.00,]

取值round:

driver-url=jdbc:mysql://127.0.0.1/test?zeroDateTimeBehavior=round

结果:

ClientDto[id:1,email:test@hotmail.com,civilite:测试用户,prenom:贾,nom:某某,birthday:1992-03-18,telephone:12312345678,address:测试用户的住址,regionid:1,postcode:111000,city:大连,country:france,batiment:110#,escalier:2,code:151515,interphone:050505,etage:东侧走廊,appartment:205,createdate:2010-03-1609:30:21.0,status:0,totalprice:0.00,totalnum:0,lastdate:0001-01-0100:00:00.0,lasttotal:0.00,]

另一参数noDatetimeStringSync:

driver-url=jdbc:mysql://127.0.0.1/test?noDatetimeStringSync=true

结果:

ClientDto[id:1,email:test@hotmail.com,civilite:测试用户,prenom:贾,nom:某某,birthday:1992-03-18,telephone:12312345678,address:测试用户的住址,regionid:1,postcode:111000,city:大连,country:france,batiment:110#,escalier:2,code:151515,interphone:050505,etage:东侧走廊,appartment:205,createdate:2010-03-1609:30:21,status:0,totalprice:0.00,totalnum:0,lastdate:0000-00-0000:00:00,lasttotal:0.00,]

个人感觉按通用性来说结果是Null可能好些,但是结果是一串零的更能贴近空时间的概念,所以具体选择看各自项目的需求选择使用吧。

原文转自:http://blog.sina.com.cn/s/blog_5fbb09180100u6xb.html,所有权力归原作者所有。

问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date相关推荐

  1. ERROR manager.SqlManager: Error executing statement: java.sql.SQLException: Access denied for user

    使用sqoop从MySQL数据库导出数据时: [root@node3 bin]# ./sqoop import --connect jdbc:mysql://192.168.0.109:3306/fa ...

  2. nested exception is java.sql.SQLException: HOUR_OF_DAY: 0 -> 1

    Cause: java.sql.SQLException: HOUR_OF_DAY: 0 -> 1 HOUR_OF_DAY: 0 -> 1; nested exception is jav ...

  3. java中isclosed_java.sql.SQLException: Conntion is closed.解决方法

    最近在一个java调用oracle存储过程的程序中,经行压力测试老报java.sql.SQLException: Conntion is closed,间歇中冶也有java.sql.SQLExcept ...

  4. java.sql.SQLException: validateConnection false

    错误信息 详细信息 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.my ...

  5. java.sql.SQLException: Protocol violation 解决方法

    本篇承接这一篇 :java.sql.SQLException: Protocol violation 问题解析 问题描述 应用可以正常启动,但是运行一段时间时候偶尔会出现这个错误. java.sql. ...

  6. Druid java.sql.SQLException: 违反协议 / Oracle连接报错生违反协议

    1. 报错背景 tomcat + Oracle + Druid连接池后台报错( java.sql.SQLException: 违反协议),很奇怪的是只有某种特殊情况下才报错,项目其他功能都运转正常,报 ...

  7. java jdbc 无效的列名_java.sql.SQLException列名无效

    java.sql.SQLException列名无效 mip版  关注:114  答案:3  悬赏:50 解决时间 2021-01-26 23:14 已解决 2021-01-26 11:39 下面是提示 ...

  8. oracle java.sql.SQLException: 列名无效

    执行一段sql, String sql="select e.*,nvl(p.userid,0) as proid from (select * from (select ROWNUM as ...

  9. java.sql.SQLException: Field ‘habit_id‘ doesn‘t have a default value

    bug 记录,今天执行下面这段代码时报:java.sql.SQLException: Field 'habit_id' doesn't have a default value异常 String sq ...

  10. java sqlexception_java.sql.SQLException

    java.sql.SQLException: ResultSet is closed是什么错误 java.sql.SQLException:ResultSetisclosed是什么错误 java.sq ...

最新文章

  1. 2021年3月9日 北京快手Java开发–用户增长方向 实习面经(一面)
  2. matplotlib画图绘制辅助线
  3. 在sdk中添加源文件_实用干货 | 一步一步教你在SpringBoot中集成微信刷卡支付
  4. 泛洪攻击(Flood)与TCP代理(TCP proxy)
  5. 解决selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missin
  6. 阿里女员工遭遇「杀猪盘」背后的思考
  7. codeforces 580D Kefa and Dishes
  8. photo-sphere-viewer.js使用时图片拉伸的问题
  9. 文本文件上传漏洞[任意.绕过.解析]
  10. Intellij IDEA 2016 使用
  11. Could not find an NgModule. Use the skip-import option to skip importing in NgModule.
  12. java 排序,选择排序、插入排序、冒泡排序
  13. Win10如何使用命令释放IP和重新获取新IP
  14. pe服务器制作教程,WINPE系统制作原理教程介绍
  15. 解决VMware虚拟机中鼠标闪烁问题
  16. 【毕业设计】flink大数据淘宝用户行为数据实时分析与可视化
  17. VS Code 安装和配置 ESLint
  18. Win10内存占用率过高问题解决
  19. Chia官方:有关Chia官方矿池的重要解读,费率更加透明,爆块更稳定
  20. 为什么复数域中使用共轭转置而不是转置

热门文章

  1. CodeForces-748D 贪心
  2. 读《大道至简》第三章 有感
  3. 有关锁和内存使用的DMV
  4. sybase数据库配置经验交流
  5. 重构图书馆惊魂夜(理解模型,关注设计)
  6. linux打印JAVA日志命令_Linux下查看日志用到的常用命令
  7. RTT的IPC机制篇——信号
  8. 怎么实现抢票软件_怎么样在windows上实现文件预览功能?一个软件搞定,提高效率...
  9. php工具箱mysql停止进程_PHP进程卡死和MySQL超时时间的设置方法
  10. lua运行外部程序_Lua 协同程序(coroutine)