wood burning stoves 2.0*
The moose likes JDBC and the fly likes SQL NULL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL NULL" Watch "SQL NULL" New topic


Arun Thakur

Joined: Mar 15, 2006
Posts: 17
Suppose we have sql statement:

Select Column1 From Table1 Where arg= ? ;

and some where in method I call that statement and that method passes
arg = null:


public ResultSet abc(String arg)
conection .....bla bla.

statement.setString(1, arg);
rs = statement.exceuteQuery;

return rs;

Now question is whether is rs would be null or not???
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Did you try it?

In SQL null never equals anything, even another null. I haven't used prepared statements enough to know whether it would throw an exception for an invalid condition, convert the syntax to "is null", or just return, um, what did you get back?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Arun Thakur

Joined: Mar 15, 2006
Posts: 17
That I have to verify whether rs is null or not. I am using JTEST. And JTEST is passing null as argumnet to the method and asking me assertNotNull (the return)???
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Did you write the test? If so you can change it to test for another return value if the null turns out to not work. If you can't change it (maybe it's an existing API or a class assignment?) you'll probably have to do something to make it pass.

Have you run the code snippet you showed? What actually came back?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Moving to JDBC.

[Jess in Action][AskingGoodQuestions]
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

Originally posted by Arun Thakur:
That I have to verify whether rs is null or not.
Why? The documentation for Statement's executeQuery method specifically says that it will never return null. And what does that test have to do with whether a bind variable in a PreparedStatement being null?
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30370

Paul is correct that rs will never be null.

It sounds like JTest is passing null to the method, not the result set. This is to get you think about what should happen if null is passed. Should the code throw an IllegalArgumentException, pass silently, etc. Or if it could never happen, you can supress the test.

I would throw an exception rather than call setString with a null through - for the reasons others have described above.

For those following this, JTest is a commericial tool that checks coding conventions and tries to generate tests that will break your code.

If this thread moves back to the topic of testing, we can move it back to the testing forum for you. If it stays related to JDBC, it can stay here.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
wood burning stoves
subject: SQL NULL