异常:
{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"}字段类型 timestamp , 默认值 0000-00-00 00:00:00
----------------------------------
(mysql 5.5 可以存,mysql 5.7报错)
timestamp类型取值范围:1970-01-01 00:00:00 到 2037-12-31 23:59:59,
( 官网给出timestamp类型默认值default范围是 '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC,在中国,由于时区问题为1970-01-01 08:00:01 to 2038-01-19 11:14:07。)
初始值调整为 1970-01-02 00:00:00 就可以了
因MySQL的时间类型datetime范围是1000-01-01 00:00:00 到 9999-12-31 23:59:59,所以报错,
将日期改为正常日期即可。
------------------------------------------------------------------------------------------------------------
在使用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-preparedstatementcJust 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.htmlJDBC报错: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