Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JDBC and the fly likes Displaying null using rs.getFloat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Displaying null using rs.getFloat" Watch "Displaying null using rs.getFloat" New topic
Author

Displaying null using rs.getFloat

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi all,
I'm trying to display the output of a resultset in a tabular manner on the screen .However , I have a problem where , a value that is null is displayed as 0.0 or 0 as the resultset methods(getFloat and getInteger) are of return type float or int and not Float or Integer .
To provide an example

Is there any other method I should be invoking in order to display the null as blank rather than a default value.?
Thank you .

Learning and Learning!-- Java all the way!
Sudheer Bhat
Ranch Hand

Joined: Feb 22, 2011
Posts: 75
With getInt() or getFloat its not possible. These methods return 0 if the database value is NULL (its documented in the Resultset interface). If you want blank to be displayed in these columns, then you probably need to put a blank string in there.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Sudheer Bhat wrote:With getInt() or getFloat its not possible. These methods return 0 if the database value is NULL (its documented in the Resultset interface).

It is very much possible. See the ResultSet.wasNull() method. All what is needed here is to call the wasNull() method after getInt()/getFloat() and re-set obtained value to null if wasNull() returns true.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks Martin!
That worked for me!
 
Consider Paul's rocket mass heater.
 
subject: Displaying null using rs.getFloat