Solved

Prepared Statement -binding null values .

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

691 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