?
Solved

Prepared Statement -binding null values .

Posted on 2013-11-26
2
Medium Priority
?
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

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 36

Accepted Solution

by:
mccarl earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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