在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";

Mysql-提示java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 7 to TIMESTAMP.的更多相关文章

  1. nested exception is java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 14 to TIMESTAMP.

    无法将"0000-00-00 00:00:00"转换为TIMESTAMP 2017-05-08 00:56:59 [ERROR] - cn.kee.core.dao.impl.Ge ...

  2. mysql插入报错:java.sql.SQLException: Incorrect string value: '\xE6\x9D\xAD\xE5\xB7\x9E...' for column 'address' at row 1

    界面报错: 日志报错: java.sql.SQLException: Incorrect at com.mysql.cj.jdbc.exceptions.SQLError.createSQLExcep ...

  3. mysql报错:java.sql.SQLException: Incorrect string value: '\xE4\xB8\x80\xE6\xAC\xA1...' for column 'excelName' at row 1

    一.问题 用Eclipse做项目时候报错 java.sql.SQLException: Incorrect string value: '\xE4\xB8\x80\xE6\xAC\xA1...' fo ...

  4. java.sql.SQLException: Incorrect string value: '\xF0\x9F\x9A\x80\xF0\x9F...' for column 'name' at row 1

    1.异常提示: 12:59:10.000 [http-nio-8080-exec-40] DEBUG o.s.j.s.SQLStateSQLExceptionTranslator - Extracte ...

  5. nested exception is java.sql.SQLException: Incorrect string value: '\xE7\x99\xBB\xE9\x99\x86...' for column 'image' at row 1

    HTTP Status 500 - Hibernate operation: could not insert: [cn.itcast.shop.product.vo.Product]; uncate ...

  6. java.sql.SQLException: Incorrect string value: '\xF0\x9F\x91\x88\xE6\x88...' for column 'content' at row 1

    往MySQL插入数据时,报错如下 java.sql.SQLException: Incorrect at com.mysql.cj.jdbc.exceptions.SQLError.createSQL ...

  7. java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'

    java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName' ...

  8. 【MySQL】java.sql.SQLException: The server time zone value

    错误:Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: The se ...

  9. 更新MySQL数据库( java.sql.SQLException: No value specified for parameter 1) 异常 解决方法

    package com.swift; import java.io.File; import java.sql.Connection; import java.sql.PreparedStatemen ...

  10. 解决java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'

    今天使用mysql,用java程序往mysql中写数据,出现如下错误: Caused by: java.sql.SQLException: Incorrect string value: '\xD3\ ...

随机推荐

  1. HDU 5029 Relief grain --树链剖分第一题

    题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...

  2. 每日一语:What is he getting at?

    What is he getting at? 他讲这话是什么意思? 2015-1-12

  3. Android SQLite (二) 基本用法

    在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的. 一 ...

  4. VS2013无法启动 IIS Express Web解决办法

    不要勾选[覆盖应用程序根URL(U)],或让[覆盖应用程序根URL(U)]下面的输入框和上面的输入框的地址一样! 使用VS2013有一段时间了,因前期都是编写C/S程序,没有使用到B/S调试器.前几日 ...

  5. window7 右键菜单显示-》在此处打开命令窗口

    window7 右键菜单显示->在此处打开命令窗口: 注册表中: HKEY_CLASSES_ROOT\Directory\Background\shell\cmd下将[Extended]重命名或 ...

  6. php 上传大文件主要涉及配置upload_max_filesize和post_max_size两个选项

    php 上传大文件主要涉及配置 upload_max_filesize 和post_max_size两个选项   今天在做上传的时候出现一个非常怪的问题,有时候表单提交可以获取到值,有时候就获取不到了 ...

  7. 关于Java多态

    什么是多态 同一个实现接口,使用不同的实例而执行不同的操作 子类转换成父类的规则: *将一个父类的引用指向一个子类对象时,称为上转型,自动进行类型转换 *此时通过父类引用变量调用的方法是子类覆盖或继承 ...

  8. 21SpringMvc_异步发送表单数据到Bean,并响应JSON文本返回(这篇可能是最重要的一篇了)

    这篇文章实现三个功能:1.在jsp页面点击一个按钮,然后跳转到Action,在Action中把Emp(int id ,String salary,Data data)这个实体变成JSON格式返回到页面 ...

  9. 集成架构:对比 Web API 与面向服务的架构和企业应用程序集成(转)

    http://kb.cnblogs.com/page/521644/ 摘要:总体上讲,SOA 和 Web API 似乎解决的是同一个问题:以实时的.可重用的方式公开业务功能.本教程将分析这些举措有何不 ...

  10. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...