This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Handle Null Values" Watch "Handle Null Values" New topic
Author

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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?

 
 
subject: Handle Null Values
 
Similar Threads
Issue with sorting date column with null values in a dataTable
MySQL JDBC Driver and Null Dates
SQL & Java Data Mapping using EJB
generic method for similar classes
selectItem null