Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What happens with setString and null values?

843854May 20 2002 — edited May 20 2002
Hi,

I seem to be experiencing some strange behaviour with PreparedStatements and setString that I can't quite explain and would appreciate if somebody could help.

I am running JDK1.3.1 on Windows NT 4 using JDBC-ODBC to interact with our SQL Server 7 database.

I have a PreparedStatement which is expecting a string e.g.

String var;
...
PreparedStatement ps = cn.prepareStatement("SELECT X FROM Y WHERE W = ?");
ps.setString(1, var);

If var has a valid value then eveything works fine, however sometimes var can be null. In this case, I would have expected this statement to either
a) return nothing (what the code currently assumes) or
b) throw an error since the proper syntax should be SELECT X FROM Y WHERE W IS NULL.

The strange thing is sometimes this statement works when var is null returing all rows where w is null. This doesn't happen all the time, just sometimes.

My question is, what should I expect if var is null?

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 17 2002
Added on May 20 2002
3 comments
2,310 views