Solved

Prepared Statement -binding null values .

Posted on 2013-11-26
2
345 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

896 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

13 Experts available now in Live!

Get 1:1 Help Now