Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL NULL

 
Arun Thakur
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

METHOD

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

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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Arun Thakur
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to JDBC.
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun,
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic