• Value '0000-00-00' can not be represented as java.sql.Date或是java.sql.Timestamp


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

    问题描述, 在java应用程序中,数据表中有记录的time字段(属性为timestamp)其值为:“0000-00-00 00:00:00”

    程序使用select 语句从中取数据时出现以下异常:

    java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date

    这是因为 “0000-00-00 00:00:00”在mysql中是作为一个特殊值存在的,但是在Java中, java.sql.Date 会被视为 不合法的值,被JVM认为格式不正确。

    解决办法:

    在jdbc的url加上   zeroDateTimeBehavior参数:

    datasource.url=jdbc:mysql://localhost:3306/pe?useUnicode=true&characterEncoding=gbk &zeroDateTimeBehavior=convertToNull

    对于值为0000-00-00   00:00:00(默认值)的纪录,根据不同的配置,会返回不同的结果:

    不配置:默认返回异常

    zeroDateTimeBehavior=round   0001-01-01   00:00:00.0

    zeroDateTimeBehavior=convertToNull   null

    这样有可能会报新的Exception:

    The reference to entity "characterEncoding" must end with the ';' delimiter   

    其原因可能是在Properties文件或者xml文件中忘记进行特殊符号的转译了,

    jdbc:mysql://192.168.1.155:3306/diandi?useUnicode=true&characterEncoding=UTF-8 & zeroDateTimeBehavior=convertToNull

    需要改为:

    jdbc:mysql://192.168.1.155:3306/diandi?useUnicode=true&characterEncoding=UTF-8 & zeroDateTimeBehavior=convertToNull 

    有以下几类字符要进行转义替换:

    <

    <

    小于号

    &gt;

    >

    大于号

    &amp;

    &

    &apos;

    '

    单引号

    &quot;

    "

    双引号

    记录一下:本人工程是这么写的:

    zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true
    zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;characterEncoding=UTF-8&allowMultiQueries=true

    以上两种都是可以的。

    但以下都是不可以的:(暂时还不知道为啥)

    characterEncoding=UTF-8&amp;allowMultiQueries=true&amp;zeroDateTimeBehavior=convertToNull&amp;useUnicode=true
    allowMultiQueries=true&amp;zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;characterEncoding=UTF-8

    转载至http://josh-persistence.iteye.com/blog/209841

  • 相关阅读:
    POJ 3080 Blue Jeans
    POJ 1961 Period
    POJ 2185 Milking Grid
    POJ 2752 Seek the Name, Seek the Fame
    斗地主算法的设计与实现--项目介绍&如何定义和构造一张牌
    MyEclipse 免安装版制作
    网络子系统48_ip协议数据帧的发送
    Oracle sql语句执行顺序
    当OOP语言RAII特性发展到functional形式的极致
    FusionCharts属性大全
  • 原文地址:https://www.cnblogs.com/ihanliu/p/4402905.html
Copyright © 2020-2023  润新知