Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

sql

null

mysql

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.

like image 440
user4126382 Avatar asked Mar 07 '26 09:03

user4126382


1 Answers

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 nulls:

"',lastEventTime=" + (rs10d.getString("lastEventTime") == null ? 
                      "null" : 
                      "'"+rs10d.getString("lastEventTime")+"'");
like image 150
Mureinik Avatar answered Mar 09 '26 21:03

Mureinik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!