Java JDBC-选择列为NULL的位置

Java JDBC-选择列为NULL的位置,java,sql,postgresql,jdbc,Java,Sql,Postgresql,Jdbc,我的Postgres 9.0数据库中有一个简单的表: create table test (id int not null, value int); 我用几行填充了它: insert into test values (1, 1); insert into test values (2, null); insert into test values (3, null); insert into test values (4, 1); 现在我试着用JDBC阅读它。当我通过value列中的非空值

我的Postgres 9.0数据库中有一个简单的表:

create table test (id int not null, value int);
我用几行填充了它:

insert into test values (1, 1);
insert into test values (2, null);
insert into test values (3, null);
insert into test values (4, 1);
现在我试着用JDBC阅读它。当我通过
value
列中的非空值进行选择时,一切正常:

PreparedStatement select = c.prepareStatement("select * from test where value=?");
select.setInt(1, 1);
return select.executeQuery();
但是当我想选择
value
为null的行时,结果集不包含任何行。我尝试了这两种方法:

select.setObject(1, null);

都不行


发生什么事了?我知道检查null的正确SQL应该是
其中value为null
,而不是
其中value=null
,但肯定JDBC足够聪明,可以帮我解决这个问题吗?

没有任何东西是
=null
。如果在交互式查询计算器中键入
select*fromtest where value=NULL
,则不会得到任何结果。JDBC不会重写表达式,它只是替换值

您必须使用
is
运算符来代替查询:

PreparedStatement select = c.prepareStatement("select * from test where value is NULL");
return select.executeQuery();

您说过您希望JDBC足够“聪明”来为您做到这一点,但这将严重违反关注点分离。您可能希望在查询中使用
=
设置一个参数
NULL
,因为您知道该关系永远不会计算为true(很可能是更大条件集的一部分)。

不,JDBC不够聪明。运气不好。如果JDBC这样做,它将违反SQL标准指定的有效行为谢谢-最后我需要编写一个比较函数,该函数包含2个参数,如果它们相等或都为null,则返回true。
PreparedStatement select = c.prepareStatement("select * from test where value is NULL");
return select.executeQuery();