`
chakey
  • 浏览: 363780 次
  • 性别: Icon_minigender_1
  • 来自: 水星
社区版块
存档分类
最新评论

MySQL 关于 Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestam

阅读更多

转载:

http://hi.baidu.com/koeiol/blog/item/b36bfe60452964e6f6365462.html

 

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

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

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

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

 

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

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

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

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

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.

 

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

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

原连接串: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 as java.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-16 09: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-16 09:30:21.0,status:0,totalprice:0.00,totalnum:0,lastdate:0001-01-01 00: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-16 09:30:21,status:0,totalprice:0.00,totalnum:0,lastdate:0000-00-00 00:00:00,lasttotal:0.00,]

 

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

分享到:
评论
1 楼 liguikai2008 2012-06-04  
谢谢,解决了

相关推荐

    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 - **...

    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 ...

    iOS Fuondation Framework Reference

    - `- (id)objectValue`: Returns the object value of the descriptor if it can be represented as an Objective-C object. **Constants**: Constants related to Apple Event types and formats. --- ##### ...

    微软内部资料-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 ...

    Introduction_to_Optimum_Design.pdf

    The chapter also includes several examples, such as the design of a can, insulated spherical tank, saw mill operation, two-bar bracket, cabinet, tubular column, cylindrical tank, coil springs, and a ...

    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 ...

    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 ...

    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 ...

    Model Driven Architecture.pdf

    - **CORBA 3.0**: This version of CORBA added component-model technology, enabling integration across diverse computing platforms such as the Internet, Java, Windows, and SQL backends. - **Specialized ...

    大学英语预备级精读课后答案与题干翻译.doc

    - 被动语态 (`is represented by`, `can be converted to`, `is decided by`) - 条件状语从句 (`He will be hired depending on the interview.`) - 定语从句 (`When asked where his mother was...`) 4. **句子...

    英语六级重点词汇表

    - She can hardly abide his presence. #### Abolish (v.) - **定义与用法**:意为废除某项制度、法律或习俗。 - **例句**:The government decided to abolish capital punishment. #### Absent (a.) - **定义与...

    C Bath University Course Material

    - **Images as Functions:** Images can be represented mathematically as functions that map spatial coordinates to intensity values. - **Images as Frequencies:** Fourier transforms can be used to ...

    wide terminal type flat chip resistors

    - Additional information about packaging options can be found in Appendix A. #### Resistance Values and Tolerances The WK73R series offers a range of resistance values with different tolerances: -...

    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 ...

    Rust - Rust Programming By Example - Guillaume Gomez

    - **Clone Types:** Types that implement the `Clone` trait can be cloned, creating a new owned value. - **Copy Types:** Types that implement the `Copy` trait can be copied directly into another ...

Global site tag (gtag.js) - Google Analytics