I have a simple table in my Postgres 9.0 database:
create table test (id int not null, value int);
I have populated it with a few rows:
insert into test values (1, 1);
insert into test values (2, null);
insert into test values (3, null);
insert into test values (4, 1);
Now I'm trying to read it with JDBC. When I select by the non-null values in the value
column, everything is fine:
PreparedStatement select = c.prepareStatement("select * from test where value=?");
select.setInt(1, 1);
return select.executeQuery();
But when I want to select rows where value
is null, the Result Set contains no rows. I have tried both of these approaches:
select.setObject(1, null);
and
select.setNull(1, Types.INTEGER);
Neither work!
What's going on? I know that the correct SQL to check for NULLs would be where value is null
instead of where value=null
but surely JDBC is clever enough to sort that out for me?