This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Handle Null Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Handle Null Values" Watch "Handle Null Values" New topic

Handle Null Values

Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
I have a table of Date and String value. Each date column and string values represents specific logic such as unfinished transactions so each must be represented by a null value. (Not an empty string)

Now I am having a problem mapping everything in my JDBC.

Timestamp dteEndTrans = rs.getTimestamp("dteEndTrans");

if (rs.wasnull()){
//set DTO object to empty string("")

Is there an elegant way of handling such things rather than calling wasnull in every column?

I have another option which is in the Database Query itself wherein it will return an empty string for every null value, but I wanted the application to handle this.

Any advice please?

Sean Clark ---> I love this place!!!
Me ------> I definitely love this place!!!
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

If you're getting objects (e.g. dates and strings) then the "more elegant" way would be to just compare said objects to null directly. You only need the "wasNull" method when you're getting primitives from the database.
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi Paul,

Thanks for pointing that out. I thought they only work on Object type since that was I have read from the API Docs.

Going back to your suggestion.

So does it mean that for every columns, I'll have to add bunch of if statements to test if it is null?

I agree. Here's the link:
subject: Handle Null Values
It's not a secret anymore!