Search
Tweets
Recent Changes
« MySQL Query Cache | Main | More S40 Fun »
Wednesday
Nov172004

JDBC Annoyance

I like JDBC more than even DBI, which has always been my favorite database abstraction doodad. But this irritates the crap out of me. setLong()! Consider the following:

Connection conn = ...
PreparedStatement ps = conn.prepareStatement("SELECT name FROM customers WHERE customerid=?");
ps.setLong(1, someVar);

That works great, as long as someVar is a long and not a Long. If someVar is a Long you have to call longValue() on your Long since setLong()'s signature is (int, long). But what if someVar is null? NPE!

So you could fix that with:

if(someVar != null) {
ps.setLong(1, someVar);
} else {
ps.setNull(2, Types.INTEGER);
}

But that's so wordy! As I wrote this I decided that maybe this code would work:

ps.setObject(1, someVar, Types.INTEGER);

It should handle the null. Let's hope so!

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>