简体   繁体   中英

Data truncation: Incorrect datetime value: 'null' in Java

I have a following query which insert data into mysql using java.

It gives me this error

Data truncation: Incorrect datetime value: 'null' for column 'lastEventTime' at row 1

The lastEventTime is set to be a nullable datetime type with default as NULL . Unfortunately when I run the same query in phpmyadmin it accept and data get inserted into the table. What is the solution for java then?

My of part codes is as below.

Single quotes ( ' ) denote string literals. Here, you didn't mean you insert the string 'null' , but an actual null value - just drop the quotes:

INSERT INTO tblDetails 
SET    eID=1010,entID=2,tID=65,eDateTime='2014-12-04 14:34:44',lastEventTime=null

Moreover, unless lsatEventTime has an undesirable default value, you could just drop the reference to it altogether and let the database use the default ( null ):

INSERT INTO tblDetails 
SET    eID=1010,entID=2,tID=65,eDateTime='2014-12-04 14:34:44'

EDIT:
To answer the question asked in the comments, this can be done even when dynamically constructing the insert statement. Just take the following line:

"',lastEventTime='"+rs10d.getString("lastEventTime")+"'";

... and add some logic to check for null s:

"',lastEventTime=" + (rs10d.getString("lastEventTime") == null ? 
                      "null" : 
                      "'"+rs10d.getString("lastEventTime")+"'");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM