Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Urgent Help needed with nulls from ResultSet

Status
Not open for further replies.

stressball

Programmer
Mar 14, 2001
68
AU
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.
 
Tricky things about resultsets is that you have to get the object before you can tell if it is null. Example:
Code:
Hashtable finalContentHash = new Hashtable();
P_Content_Read contentRead = new P_Content_Read();
ResultSet rs = contentRead.getData(content_id);
String handleNull = "";

Object o;
o = rs.getObject(DEFAULT_FILE);
if (rs.wasNull) 
  finalContentHash.put(DEFAULT_FILE_TITLE,handleNull);
else
  finalContentHash.put(DEFAULT_FILE_TITLE,o);

Here is the API for wasNull() FYI:
Code:
wasNull

public boolean wasNull()
                throws SQLException


Reports whether the last column read had a value of SQL NULL. Note that you must first call one of the getXXX methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL.

Returns:

true if the last column value read was SQL NULL and false otherwise

Throws:

SQLException - if a database access error occurs
Wushutwist
 
Thank you Wushutwist BUT...
I need a generic way of dealing with the nulls in my ResultSet.
There are 7 columns that may be null. I do not want to keep 7 If statements that do the the same thing for each column. I want a method that gets the ResultSet, searches for nulls and replaces the nulls with an empty String.

Help please, I can't seem to find any documentation on this and my attempts are failing miserably!
 
Hi, I think this would help:-

...
Hashtable finalContentHash = new Hashtable();
P_Content_Read contentRead = new P_Content_Read();
ResultSet rs = contentRead.getData(content_id);
ArrayList rsList = convertData(rs);
// loop through rsList to retrieve the data just like looping through resultset
...

public ArrayList convertData(ResultSet rs)
{
// get number of columns
int col = rs.getMetaData().getColumnCount();
ArrayList rsList = new ArrayList();

// transfer data to arraylist
while (rs.next())
{
String temp = "";
for (int i=0;i<col;i++)
{
String s = (String)rs.getObject(i);
if (s != null)
temp = temp + s + &quot; &quot;;
else
temp = temp + &quot; &quot;;
}
rsList.add(temp);
}
return rsList;
}


It should be something like this. I have not tested it out so I am not sure if it would work or not but the idea should be there.

Regards,
Leon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top