`
chakey
  • 浏览: 359774 次
  • 性别: 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  
谢谢,解决了

相关推荐

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

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

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

    56991795TIMIT-speech-database.rar

    3- All the dialect(方言的) regions should be represented in both subsets, with at least 1 male and 1 female speaker from each dialect. 4- The amount of overlap of text material in the two subsets...

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

    speex回声消除算法

    The echo canceller is based on the MDF algorithm described in: J. S. Soo, K.... can be seen as applying a gradient descent on a "soft constraint" instead of having a hard constraint.

    Circuitscape-4.0.5-x64-setup.rar

    Effective resistances, current flow, and voltages calculated across the landscapes can then be related to ecological processes, such as individual movement and gene flow. More detail about the ...

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

    EN 60601-1:2006+A11

    A list of organizations represented on this subcommittee can be obtained on request to its secretary. This publication does not purport to include all the necessary provisions of a contract. Users are...

    Foursquare数据集

    Foursquare数据集 Abstract: Foursquare is a location-based social networking website, software for mobile devices....-. Basic statistics Number of Nodes: 106,218 Number of Edges: 3,473,834

    i-vector的工具箱

    In case Matlab is not installed or Matlab license is not available (for instance on a computer cluster), we provide standalone executables that can be used in conjunction with the Matlab Compiler ...

    Python_Real-World+Data+Science_A+Course+in+Four+Modules-2016.pdf

    be seen as methods of the objects that represents me. So, now that you know what objects are and that they expose methods that you can run and properties that you can inspect, you're ready to start ...

    Data Cube: A Relational Aggregation Operator

    The SQL aggregate functions and the GROUP BY operator produce zero-dimensional or one-dimensional answers. Applications need the N-dimensional generalization of these operators. This paper defines ...

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

    华南理工大学计算机全英班算法设计实验

    2)Dynamic Programming is an algorithm design method that can be used when the solution to a problem may be viewed as the result of a sequence of decisions and this algorithm is a very useful technique...

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

    EhLib-delphi

     can send SQL expressions on the server through the corresponding access engine.  It is TBDEDataDriverEh, TIBXDataDriverEh, TDBXDataDriverEh and TADODataDriverEh  component.  There are a ...

Global site tag (gtag.js) - Google Analytics