Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Prepared Statement -binding null values .

Posted on 2013-11-26
2
Medium Priority
?
381 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

618 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