简体   繁体   English

数据截断:日期时间值不正确:Java中的“null”

[英]Data truncation: Incorrect datetime value: 'null' in Java

I have a following query which insert data into mysql using java. 我有一个以下查询,使用java将数据插入到mysql中。

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 . lastEventTime设置为可为空的datetime类型,默认为NULL Unfortunately when I run the same query in phpmyadmin it accept and data get inserted into the table. 不幸的是,当我在phpmyadmin中运行相同的查询时,它接受并将数据插入到表中。 What is the solution for java then? 那么java的解决方案是什么?

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: 在这里,您并不是说您插入字符串'null' ,而是实际的null值 - 只需删除引号:

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 ): 此外,除非lsatEventTime具有不合需要的默认值,否则您可以完全删除对它的引用,并让数据库使用默认值( 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. 要回答注释中提出的问题,即使在动态构造insert语句时也可以这样做。 Just take the following line: 只需采取以下行:

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

... and add some logic to check for null s: ...并添加一些逻辑来检查null s:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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