Solved

Prepared Statement -binding null values .

Posted on 2013-11-26
2
336 Views
Last Modified: 2014-01-21
Hi

I am trying to bind a lot of variable to prepared statements and everytime i have to check for null and then introduce setNull instead of setInt or setLong .  Is there anything else i can do to make the code look more clean and remove the redundant code ?

currently its like - if(value != null)
                             ps.setInt (1,value)
                             else
                             ps.setNull(1,Type)
0
Comment
Question by:FranklinRaj22
2 Comments
 
LVL 26

Expert Comment

by:dpearson
ID: 39678294
You should consider switching to a library like QueryRunner from Apache:
http://commons.apache.org/proper/commons-dbutils/examples.html

It allows you to write much simpler client code like:

Object[] result = run.query("SELECT * FROM Person WHERE name=?", h, "John Doe");

where the "query" method handles all of the encoding as a prepared statement (including calls to setNull etc.) for you.

The even more important functionality (and the real win for QueryRunner) is that it releases you from creating and *correctly closing* database connections.  That's all handled by the QueryRunner class.

It's well worth checking out,

Doug
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39679457
You can use the setObject() method instead to clean up that code. You haven't stated what Database you are using, so as per the Javadoc for this method, you may or may not need the third parameter, but try it either way and see what works for you.
Integer intValue = 5;
ps.setObject(1, intValue, Types.INTEGER);

Long longValue = 123L;
ps.setObject(2, longValue, Types.BIGINT);

Open in new window

Note if you DO require to keep the 3rd parameter and you are interested in what all the mappings are between Java and JDBC/SQL, then check out this page...

http://docs.oracle.com/javase/6/docs/technotes/guides/jdbc/getstart/mapping.html
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now