我想删除列为null的DB并使用preparedstatement 这是我的代码
String deletesql = "delete from user where e-mail = ?";
PreparedStatement prestmt = conn.prepareStatement(deletesql);
prestmt.setNull(1,Types.VARCHAR);
prestmt.executeUpdate();
但这些代码不起作用
答案 0 :(得分:2)
请尝试
String deletesql = "delete from user where e-mail is null";
如果某些内容未知(NULL),则不等于其他内容(=
)
答案 1 :(得分:2)
在SQL something = null
中未知,您需要使用something is null
,或者在参数something is ?
的情况下使用setNull(1, Types.NULL)
。
如果你想同时拥有null和非null值,你需要让它更复杂一些:
(something is ? or something = ?)
或甚至(以前可能不适用于所有数据库):
(? is null and something is null or something = ?)
然后使用:
if (something == null) {
stmt.setNull(1, Types.NULL);
// NOTE: check behavior of your db, some might require a non-null value here
stmt.setXxxx(2, null);
} else {
// Setting a null-type parameter to a non-null value should mark it as not null
stmt.setObject(1, "not null", Types.NULL);
// Use appropriate setter
stmt.setXxxx(2, something);
}
答案 2 :(得分:0)
看这里https://stackoverflow.com/a/19836582 您必须使用“DELETE WHERE e-mail IS NULL”之类的语句,并省略“setNull”调用。您可能希望将其放入单独的预准备语句中,因为它只适用于NULL列。