如何在mysql中插入NULL,尤其是INT dataType [英] How to insert NULL in mysql especially INT dataType
本文介绍了如何在mysql中插入NULL,尤其是INT dataType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有80000个需要插入数据库的重新编码,特别是在表中:temp(ts,temp)
temp是INT。
I have 80000 recodes that are need to insert into database especially in table: temp(ts, temp) temp is INT.
问题是几乎20000个重新编码为空,所以我想知道当dataType为INT时如何在DB中插入NULL。
The problem is almost 20000 recodes are null, so I am wondering how to insert NULL into DB when dataType is INT.
我试过这个:
String val = null;
//insert(ts, val) into temp
String sql = "INSERT INTO temp" + "(val)" + " VALUES" + "('" + val + "')";
Statement st = (Statement) conn.createStatement();
count = st.executeUpdate(sql);
遗憾的是插入失败。打印出异常消息:
unfortunately insert is failure. Print out the exception message:
Incorrect integer value: 'null' for column 'val' at row 1"
希望有人可以帮助我。谢谢。
Wish someone can help me with it. Thank you.
推荐答案
您应该使用 PreparedStatement
并使用 setNull(int,int)
:
You should use a PreparedStatement
and use setNull(int, int)
:
String sql = "INSERT INTO temp(val) VALUES (?)";
PreparedStatement st = con.prepareStatement(sql);
if (/* int value is not null */) {
st.setInt(1, value);
} else {
set.setNull(1, Types.INTEGER);
}
count = st.executeUpdate();
这篇关于如何在mysql中插入NULL,尤其是INT dataType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文