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

异常:
{org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [略。。。]; Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp; nested exception is java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp"}

------------------------------------------------------------------------------------------------------------


在使用MySql 时, 数据库中的字段类型是timestamp的,默认为0000-00-00, 会发生异常:java.sql.SQLException: Value ‘0000-00-00 ’ can not be represented as java.sql.Timestamp

解决办法:

给jdbc url加上 zeroDateTimeBehavior参数:

datasource.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true

zeroDateTimeBehavior=round是为了指定MySql中的DateTime字段默认值查询时的处理方式;默认是抛出异常,

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

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

zeroDateTimeBehavior=convertToNull null

----------
https://stackoverflow.com/questions/7484559/org-springframework-dao-transientdataaccessresourceexception-preparedstatementc

Just guessing, because you haven't shown anything but a stack trace, but I'd say that you're creating a PreparedStatement with the query

select id from employeerole where username = kiran and pin is NULL 
and you're later invoking setString(1, someString) on the PreparedStatement (by means of StatementCreatorUtils#setParameterValue())

To use setString with a PreparedStatement you'd have to have a query with parameters (? ) on it. In that query you don't have any parameters but you're also setting a value for parameter #1.

For further reference on PreparedStatements, I'd recommend taking a read, at least, at this tutorial: Using Prepared Statements

Just to add, if the username column is a character-typed column, you'll have to quote the String kiran in your sql query.

Note that PreparedStatements handle that for you when using a parameterized query. Aside from that advantage it's the most basic protection against SQL Injection, because it also escapes parameters as needed.

只是猜测,因为你没有显示任何东西,但堆栈跟踪,但我会说你正在创建一个PreparedStatement与查询 从employeerole中选择id,其中username = kiran且pin为NULL 并且稍后您将调用PreparedStatement上的setString(1,someString)(通过StatementCreatorUtils#setParameterValue()) 要将setString与PreparedStatement一起使用,您必须使用参数(?)进行查询。在该查询中,您没有任何参数,但是您还为参数#1设置了一个值。 有关PreparedStatements的进一步参考,我建议至少在本教程中进行阅读:使用Prepared Statements 只需要添加一下,如果用户名列是一个字符类型的列,则必须在sql查询中引用字符串kiran。 请注意,PreparedStatements在使用参数化查询时为您处理。除了这个优点以外,它是针对SQL注入的最基本的保护,因为它也根据需要转义参数。

-----------------
https://www.cnblogs.com/youxin/p/5322808.html

JDBC报错:Value '0000-00-00' can not be represented as java.sql.Date

当我从sql读出date类型时,有数据为0000-00-00,报上面的错误。这是因为 “0000-00-00”在mysql中是作为一个特殊值存在的,但是在Java中, java.sql.Date 会被视为 不合法的值,被JVM认为格式不正确。

解决办法:

在jdbc的url加上   zeroDateTimeBehavior参数:参考:http://zhaohe162.blog.163.com/blog/static/3821679720110261248540/

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

猜你喜欢

转载自my.oschina.net/klog/blog/1802145