Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Testing for empty columns in resultset

 
   Database Forums (Home) -> Java RSS
Next:  Access Database update with pstmt.executeUpdate()..  
Author Message
glen

External


Since: Aug 16, 2006
Posts: 1



(Msg. 1) Posted: Wed Aug 16, 2006 6:30 am
Post subject: Testing for empty columns in resultset
Archived from groups: comp>lang>java>databases (more info?)

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

 >> Stay informed about: Testing for empty columns in resultset 
Back to top
Login to vote
gself

External


Since: Aug 16, 2006
Posts: 1



(Msg. 2) Posted: Wed Aug 16, 2006 6:50 am
Post subject: Re: Testing for empty columns in resultset [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thomas Kellerer wrote:
> On 16.08.2006 15:30 glen RemoveThis @selfs.net wrote:
> > 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 Wink, e.g:
>
> String value = rs.getString(1);
> if (value != null) ....
>
>
> Thomas
>
>
> --
> 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. Smile
Thanks

 >> Stay informed about: Testing for empty columns in resultset 
Back to top
Login to vote
Thomas Kellerer

External


Since: Aug 06, 2006
Posts: 4



(Msg. 3) Posted: Wed Aug 16, 2006 3:36 pm
Post subject: Re: Testing for empty columns in resultset [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 16.08.2006 15:30 glen RemoveThis @selfs.net wrote:
> 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 Wink, e.g:

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


Thomas


--
It's not a RootKit - it's a Sony
 >> Stay informed about: Testing for empty columns in resultset 
Back to top
Login to vote
Thomas Kellerer

External


Since: Aug 06, 2006
Posts: 4



(Msg. 4) Posted: Wed Aug 16, 2006 4:02 pm
Post subject: Re: Testing for empty columns in resultset [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 16.08.2006 15:50 gself wrote:
> 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. Smile

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


--
It's not a RootKit - it's a Sony
 >> Stay informed about: Testing for empty columns in resultset 
Back to top
Login to vote
Dyreatnews

External


Since: Jun 08, 2006
Posts: 23



(Msg. 5) Posted: Wed Aug 16, 2006 8:09 pm
Post subject: Re: Testing for empty columns in resultset [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

glen.TakeThisOut@selfs.net writes:

> 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)

--
dt
 >> Stay informed about: Testing for empty columns in resultset 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
error in resultset - public static UserDTO getUserDTO(String userName) { UserDTO userDTO=null; // a simple bean with username and password with get-set methods try { if(isConnected) //flag indicating database connectivity {..

Using ResultSet in a TableModel? - in all the example programs i've looked at and reading i've done on extending AbstractTableModel for use in a JTable... the data returned in a ResultSet using JDBC with an SQL query is read and placed in a container object; such as Vector or the newer..

Intercepting the ResultSet.next() function - Hello! I want to build a customized ResultSet such that when the resultset.next() is invoked by a JDBC client, the resultset sends a copy of the fetched row to another program through RMI. I am modifying mysql-connector-java-3.0.17, but I found that th...

MySQL ResultSet - count rows? - Hello, I am writing the data of a ResultSet object to an array. To declare the array length, I need to know the number of rows before copying them - is there a way to read the row count instantly, without having to iterate anything? PHP has..

How to handle OutOfMemory Exception for large resultset - hi, "Mytable" contains more than 79,000 records my query is"select * from Mytable"...while processing Resultset I got OutOfMemory exception. I tried to solve it using cursors as follows..... PreparedStatement ps = con.prepa...
   Database Forums (Home) -> Java All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]