【发布时间】:2013-11-07 12:52:06
【问题描述】:
我的 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 为空的行时,结果集不包含任何行。这两种方法我都试过了:
select.setObject(1, null);
和
select.setNull(1, Types.INTEGER);
都不行!
发生了什么事?我知道检查 NULL 的正确 SQL 应该是 where value is null 而不是 where value=null 但 JDBC 肯定足够聪明,可以为我解决这个问题?
【问题讨论】:
-
不,JDBC 不够聪明。运气不好。
-
如果 JDBC 会这样做,它将违反 SQL 标准指定的有效行为
标签: java sql postgresql jdbc