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"}

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


When using MySql, the field type in the database is timestamp, the default is 0000-00-00, an exception will occur: java.sql.SQLException: Value '0000-00-00 ' can not be represented as java.sql.Timestamp

Solution:

Add the zeroDateTimeBehavior parameter to the jdbc url:

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

zeroDateTimeBehavior=round is to specify how the default value of the DateTime field in MySql is queried; the default is to throw an exception,

For a record with a value of 0000-00-00 00:00:00 (the default value), the following two configurations will return different results:

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.

Just a guess since you didn't show anything but the stack trace, but I would say that you are creating a PreparedStatement with the query select id from employeeerole where username = kiran and pin is NULL and later you will call setString on the PreparedStatement( 1, someString) (via StatementCreatorUtils#setParameterValue()) To use setString with PreparedStatement, you have to query with parameters(?). In that query, you don't have any parameters, but you also set a value for parameter #1. For further reference to PreparedStatements, I would recommend reading at least this tutorial: Using Prepared Statements Just need to add that if the username column is a character type column, you must quote the string kiran in the sql query. Note that PreparedStatements are handled for you when using parameterized queries. Apart from this advantage, it is the most basic protection against SQL injection as it also escapes parameters as needed.

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

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

When I read the date type from sql, the data is 0000-00-00, and the above error is reported. This is because "0000-00-00" exists as a special value in mysql, but in Java, java.sql.Date will be regarded as an illegal value, and the JVM considers it to be malformed.

Solution:

Add the zeroDateTimeBehavior parameter to the url of jdbc: Reference: http://zhaohe162.blog.163.com/blog/static/3821679720110261248540/

datasource.url=jdbc:mysql://localhost:3306/pe?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull

For records whose value is 0000-00-00 00:00:00 (the default value), different results will be returned depending on the configuration:

Do not configure: return an exception by default

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

zeroDateTimeBehavior = convertToNull null

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325010691&siteId=291194637