Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / July 2004

Tip: Looking for answers? Try searching our database.

ResultSet.getXYZ functions return primitive types? why?

Thread view: 
hilz - 23 Jul 2004 20:10 GMT
Hi all
I am sure this has been asked before, but i can't seem to find the answer.

the methods that get objects from java.sql.ResultSet  return primitive
types.
what if the value was null in the database?
rs.getDouble(columnName) will return 0

what is the correct way or getting values from the result set?

i wrote a utility function as shown below where i pass it the resultset and
the column name, and it will preserve the null value and return an object
Double for me.
is this the correct way of doing it?
i like to know what others do.
here is the utility function that i wrote which replaces the
rs.getDouble(String columnName)

private Double getResultSetDouble(java.sql.ResultSet rs, String columnName)
throws java.sql.SQLException{
       Object o = rs.getObject(columnName);
       if(null==o)
           return null;
       return new Double(rs.getDouble(columnName));
   }

thanks
hilz
David Harper - 23 Jul 2004 21:54 GMT
> the methods that get objects from java.sql.ResultSet  return primitive
> types.
> what if the value was null in the database?
> rs.getDouble(columnName) will return 0

If you suspect that you may have a null value, call ResultSet.wasNull
immediately after calling the getXXX function. For example:

  double d = rs.getDouble(columnName);

  if (rs.wasNull())
    System.err.println("Column " + columnName + " contained a null!");

David Harper
Cambridge, England
hilz - 24 Jul 2004 08:57 GMT
> If you suspect that you may have a null value, call ResultSet.wasNull
> immediately after calling the getXXX function. For example:
[quoted text clipped - 3 lines]
>    if (rs.wasNull())
>      System.err.println("Column " + columnName + " contained a null!");

Thanks David.
that was exactly what i needed.

thank you
hilz
Luke Webber - 24 Jul 2004 00:52 GMT
> Hi all
> I am sure this has been asked before, but i can't seem to find the answer.
[quoted text clipped - 19 lines]
>         if(null==o)
>             return null;

Just here, I would change from...

>         return new Double(rs.getDouble(columnName));

...to...

    return (Double) o;

Luke
Lee Fesperman - 24 Jul 2004 06:29 GMT
> > Hi all
> > I am sure this has been asked before, but i can't seem to find the answer.
[quoted text clipped - 27 lines]
>
>          return (Double) o;

How about going further and replacing the entire body of the method:

private Double getResultSetDouble(java.sql.ResultSet rs, String columnName)
throws java.sql.SQLException{
         return (Double) rs.getObject(columnName);
}

Signature

Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Murray - 24 Jul 2004 07:19 GMT
> > > Hi all
> > > I am sure this has been asked before, but i can't seem to find the answer.
[quoted text clipped - 34 lines]
>           return (Double) rs.getObject(columnName);
> }

I usually prefer:

private Double getResultSetDouble(java.sql.ResultSet rs, String columnName)
throws java.sql.SQLException{
         double d = rs.getDouble(columnName);
         if (rs.wasNull() {
             return null;
         else {
             return new Double(d);
         }
}

At least that way, if you get your datatypes mixed up you'll get an
SQLException telling you so, instead of a ClassCastException which can
sometimes be difficult to track down. Just a preference, the end result will
be the same ...
hilz - 24 Jul 2004 08:55 GMT
> I usually prefer:
>
> private Double getResultSetDouble(java.sql.ResultSet rs, String
columnName)
> throws java.sql.SQLException{
>           double d = rs.getDouble(columnName);
[quoted text clipped - 4 lines]
>           }
> }

Thanks Murray,
I liked this method.

thanks.
Luke Webber - 24 Jul 2004 23:49 GMT
> How about going further and replacing the entire body of the method:
>
> private Double getResultSetDouble(java.sql.ResultSet rs, String columnName)
> throws java.sql.SQLException{
>           return (Double) rs.getObject(columnName);
> }

D'oh! Wood, trees etc. And the next step is naturally to dispense with
this method entirely. <g>

Luke


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.