`
bugyun
  • 浏览: 556021 次
社区版块
存档分类
最新评论

Value '0000-00-00' can not be represented as java.sql.Date(转)

 
阅读更多

转自:http://josh-persistence.iteye.com/blog/2098415?utm_source=tuicool&utm_medium=referral

 

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;

 

"

 

双引号

 

 

分享到:
评论

相关推荐

    mysql datetime查询异常问题解决

    异常:Value ‘0000-00-00 00:00:00’ can not be represented as java.sql.Timestamp (2011-05-25 11:38:40) 描述:非空无默认值的Datetime类型字段,查询时程序报以下错误: Value ‘0000-00-00 00:00:00’ can ...

    Senfore_DragDrop_v4.1

    Earlier versions of Delphi and C++ Builder will not be supported. If you need Delphi 3 or C++ Builder 3 support you will have to revert to version 3.7 of the Drag and Drop Component Suite. The ...

    微软内部资料-SQL性能优化2

    Contents Module Overview 1 Lesson 1: Memory 3 Lesson 2: I/O 73 Lesson 3: CPU 111 ... It should be pointed out that SQL Server was linked using the /LAREGEADDRESSAWARE flag and can leverage ...

    AnsoftQ3D_v7-full_book.pdf

    To create complex objects, primitives can be used as “tools” to cut holes, carve away, or join. The operations that are performed with these “tools” are often referred to as Boolean operations. ...

    Speech Encoding - Frequency Analysis MATLAB.zip_As One_HMM word_

    The speech signal for the particular isolated word can be viewed as the one generated using the sequential generating probabilistic model known as hidden Markov model (HMM). Consider there are n ...

    Image super-resolution

    The mapping is represented as a deep convolutional neural network (CNN) [15] that takes the low- resolution image as the input and outputs the high-resolution one. We further show that traditional ...

    json.js_json2.js

    value represented by the name/value pair that should be serialized, or undefined if nothing should be serialized. The toJSON method will be passed the key associated with the value, and this will ...

    Raize.Components-v6.1.12 FullSource(2009-XE8) Part2/2

    not be displayed correctly when using a custom VCL Style in XE6 or XE7. * Added LightenUnselectedColoredTabs property to TRzPageControl and TRzTabControl. When this property is True, a non-active ...

    OTA COM Type Library

    Other assets can be represented as user assets or resources: A user asset is an entity that is conceptually part of another entity and so owned by that entity. It is therefore automatically copied ...

    Raize.Components-v6.1.12 FullSource(2009-XE8) Part1/2

    not be displayed correctly when using a custom VCL Style in XE6 or XE7. * Added LightenUnselectedColoredTabs property to TRzPageControl and TRzTabControl. When this property is True, a non-active ...

    DBGridPro new

    Multilevel layout of column titles - fields of dataset record can be represented as several rows in the grid to enhance the display of information to end-users at runtime. Column layout can be easily...

    kgb档案压缩console版+源码

    the leading digits can be output as they become known. For decompression, as the digits of x are read, the set of possible y satisfying (1) is restricted to an increasingly narrow lexicographical ...

    ISO14229-1.pdf

    established has the right to be represented on that committee. International organizations, governmental and non-governmental, in liaison with ISO, also take part in the work. ISO collaborates closely...

    Cours-VB-NET-2010-Developpez-com

    Objects are real-world entities that can be represented in programming. For example, a car can be considered an object with properties (color, make, model) and behaviors (drive, stop). ##### 2. In ...

    SNMP_Tag_Library-Suggested.pdf

    Setting this value to invalid (4) shall cause this software to stop running and to be unloaded. - **PC1.hrStorageDescr01** - **Type**: String - **ObjectID**: .1.3.6.1.2.1.25.2.3.1.3.1 - **...

    a project model for the FreeBSD Project.7z

    This project model is not meant to be a tool to justify creating impositions for developers, but as a tool to facilitate coordination. It is meant as a description of the project, with an overview of...

    EMD.rar_As One_EMD

    This is an implementation of the Earth Movers Distance, as described in [1]. The EMD computes the distance between two distributions, which are represented by signatures. The signatures are sets of ...

    android dex format

    ### Android DEX (Dalvik Executable) 格式详解 #### 概述 DEX(Dalvik Executable)格式是Android平台特有的程序文件格式,用于存储一系列类定义及其相关的辅助数据。这种格式专为Android设备上的Dalvik虚拟机...

    计算机网络英文题库(附答案)chapter5.pdf

    error checking method, with a four-bit generator G = 1011 and data D = 10101010, the value of R (the remainder) is calculated by XORing the data with the polynomial represented by G. The calculation ...

Global site tag (gtag.js) - Google Analytics