When accessing ResultSets in JDBC, is there an elegant way to distinguish between nulls and actual z

Posted by Uri on Stack Overflow See other posts from Stack Overflow or by Uri
Published on 2010-05-05T22:30:04Z Indexed on 2010/05/05 22:48 UTC
Read the original article Hit count: 173

Filed under:
|
|

When using JDBC and accessing primitive types via a result set, is there a more elegant way to deal with the null/0 than the following:

int myInt = rs.getInt(columnNumber)
if(rs.wasNull())?
{
 // Treat as null
} else
{
 // Treat as 0
}

I personally cringe whenever I see this sort of code. I fail to see why ResultSet was not defined to return the boxed integer types (except, perhaps, performance) or at least provide both. Bonus points if anyone can convince me that the current API design is great :)

My solution was to write a wrapper that returns an Integer (I care more about elegance of client code than performance), but I'm wondering if I'm missing a better way to do this.

© Stack Overflow or respective owner

Related posts about java

Related posts about jdbc