在postgres下将PreparedStatement参数设置为null的正确方法

rad*_*dai 2 postgresql jdbc

我使用 JDBC4(通过 JDK6)使用最新的 JDBC4 驱动程序连接到 postgresql 9.0。

conn = dpaDs.getConnection();
PreparedStatement stmt = conn.prepareStatement("delete from fss where f_id=? and f_lastmodified=?");
UUID target = UUID.fromString("8c5c5ac2-3a2a-49f3-ae48-29226d6ea3e7");
stmt.setObject(1, target, 1111);
//non of these work
//stmt.setNull(2,93);
//stmt.setObject(2,null, 93);
//stmt.setObject(2,null, java.sql.Types.NULL);
//stmt.setNull(2, java.sql.Types.NULL);
int rowCount = stmt.executeUpdate();
int g = 8;
Run Code Online (Sandbox Code Playgroud)

将第二个参数设置为 null 的正确方法是什么?我知道我可以使用“where ... f_lastmodified is null”,但是没有办法解决这个问题吗?

小智 5

那是行不通的。

不能使用运算符进行 NULL 比较=。如果你想测试 NULL,你必须使用and f_lastmodified IS NULL.

您不能在准备好的语句中使用 IS NULL 条件的参数。