Urgent: How to handle null from ResultSet

I am getting data from a ResultSet and putting it into a Hashtable. My problem is that some of the fields I am getting back are null, but I still need them. I would like to change them to an empty String, currently I am doing this but I would like to create a method to test if there is a null field in the ResultSet and set it to an empty String. I need to do this without specifying a column name as there are many columns and many could be null. Here is an example of one of the many if statements I currently have:

Hashtable finalContentHash = new Hashtable();

P_Content_Read contentRead = new P_Content_Read();

ResultSet rs = contentRead.getData(content_id);

String handleNull = "";

if (rs.getObject(DEFAULT_FILE)!=null) {

finalContentHash.put(DEFAULT_FILE_TITLE,rs.getObject(DEFAULT_FILE)); } else

finalContentHash.put(DEFAULT_FILE_TITLE,handleNull);

Can somebody help me?! I would like to use column numbering eg

loop through resultset checking for nulls and replacing them with "" but I can't seem to get it working.

Your help will be most appreciated.

[1136 byte] By [stressball] at [2007-9-26 3:35:08]
# 1

I just send the results of getString() to a method:

public String nullStringToEmpty( String stringObject )

{

if (((Object) stringObject) == null)

{

return "";

}

return stringObject.toString();

}

so in my processing code I call it like this:

[code]

while (rs.next())

{

String val1 = nullStringToEmpty(rs.getString(1));

String val2 =nullStringToEmpty(rs.getString(2));

}

Hope this helps

Jamie

jlrober at 2007-6-29 12:05:10 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
sorry there is a typo.my method's last return value needs only to be return stringObject; Jamie
jlrober at 2007-6-29 12:05:10 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> I would like to change them to an empty String

That might not be a good idea.

Doing that means that when you put data back into the database you must trim every value and then check to see if it is an empty string. If it is empty then you have to set the field to null.

jschell at 2007-6-29 12:05:10 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
Yes I see what you mean, but I cannot read a null from the database, if I do then I get a NullPointerException
stressball at 2007-6-29 12:05:10 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

> Yes I see what you mean, but I cannot read a null from the database,

> if I do then I get a NullPointerException

That doesn't have anything to do with nulls. Rather it has to do with your usage of Hashtable.

Have you considered just not putting an entry into the hash table if the result is null? And of course you can use an empty string, just as long as you consistently validate the field by triming spaces.

jschell at 2007-6-29 12:05:10 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
Another way to do it is to use "new Object()" instead of an empty string. You can use that as a marker to indicate null, still insert it into the hash table, and then you don't have to worry if a string just has spaces in it.
jschell at 2007-6-29 12:05:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

> Another way to do it is to use "new Object()" instead

> of an empty string. You can use that as a marker to

> indicate null, still insert it into the hash table,

> and then you don't have to worry if a string just has

> spaces in it.

Thanks that sounds like a good solution, I'll try that when I get to work tomorrow, the reason I need an entry in the hashtable for a null item is the hashtable is used to display the data to the user, everything needs to be displayed, and the user may decide to make an entry into a previously empty textbox.

stressball at 2007-6-29 12:05:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

> , the reason I need an entry in the hashtable for a null item is the

> hashtable is used to display the data to the user, everything needs to be displayed,

Of course. But by doing what you are doing in the database layer, you are changing your business logic to meets the needs of the display layer. Which is okay if the application is small and will have a low long term maintenance schedule. But it should not be done in general.

jschell at 2007-6-29 12:05:11 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...