• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Handle Null Values

 
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 didn't say it. I'm just telling you what this tiny ad said.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic