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 / August 2006

Tip: Looking for answers? Try searching our database.

Testing for empty columns in resultset

Thread view: 
glen@selfs.net - 16 Aug 2006 14:30 GMT
This seems too trivial.
After populating a resultset with a query, what is the best way to test
if a column is null to avoid the dreaded null pointer exception when
attempting a tostring() call.
thanks
Thomas Kellerer - 16 Aug 2006 14:36 GMT
> This seems too trivial.
> After populating a resultset with a query, what is the best way to test
> if a column is null to avoid the dreaded null pointer exception when
> attempting a tostring() call.
> thanks

ResultSet.wasNull() (very important if you use getInt() or other get
methods that return primitive data types)

As an alternative you can always test your return value (if it's an
Object) agains null ;), e.g:

String value = rs.getString(1);
if (value != null) ....

Thomas

Signature

It's not a RootKit - it's a Sony

gself - 16 Aug 2006 14:50 GMT
> > This seems too trivial.
> > After populating a resultset with a query, what is the best way to test
[quoted text clipped - 15 lines]
> --
> It's not a RootKit - it's a Sony

I was hoping for something like an isnull test.
String value = rs.getString(1); requires an extra creation of a String
var
wasNull requires the getString first to set it's status anyway
Oh well, I'm justing longing for the good old days of 'C' where if you
could think of it you could do it. :-)
Thanks
Thomas Kellerer - 16 Aug 2006 15:02 GMT
> I was hoping for something like an isnull test.
> String value = rs.getString(1); requires an extra creation of a String
> var
> wasNull requires the getString first to set it's status anyway
> Oh well, I'm justing longing for the good old days of 'C' where if you
> could think of it you could do it. :-)

How should the driver decide that without fetching the value from the
server? There is no way around the getXXXX()

Btw: I doubt that rs.getString(1) will create a *new* String (in the
sense that additional memory is allocated to hold the character data)

If the driver simply does a "return value" where value is the String
that it created, no additional memory is required (except for your local
reference).
Even a "return new String(value)" does not copy the character data. The
new String merely references the character array of the source (this is
possible as Strings are imutable)

Thomas

Signature

It's not a RootKit - it's a Sony

Dyreatnews@sun.com - 16 Aug 2006 19:09 GMT
> This seems too trivial.
> After populating a resultset with a query, what is the best way to test
> if a column is null to avoid the dreaded null pointer exception when
> attempting a tostring() call.

If all you are going to do with your ResultSet is to print it, I would
do

System.out.println(rs.getObject(x)+" somestring"+...

which would produce
null somestring

I've tried this with integer, varchar and double columns, but I don't
know if it will work for all types (I guess that depends on how the
getObject() method is implemented in the driver)

Signature

dt



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.