Search code examples
javaresultset

What is causing Java program to treat the two ResultSet.getString() methods differently?


If "FirstName" and index 1 refer to the same column in a query, and rs is a ResultSet, why would

if(rs.getString("FirstName")!=null) fullName = rs.getString("FirstName");

and

if(rs.getString(1)!=null) fullName = rs.getString(1);

behave differently?

According to the Java docs, using either the columnIndex or the columnLabel both return the value of the designated column.

I'm running into a weird problem when I use the columnIndex instead of the columnLabel, for some reason, after the jsp has been running for a few hours/days then if(rs.getString(1)!=null) evaluates to true even when the String is null. When I use the columnLabel instead, the problem doesn't happen. Can anyone tell me why this might be happening?

Extra background info

The program runs in Tomcat. We updated Tomcat, the JVM, and our database driver on the same day, and this started happening. If we reload the app in Tomcat, it starts working correctly, at least for a few hours. We switched back to the old database driver and the problem continues, so we have ruled out the database driver.


Solution

  • We have solved it. We decided to try a different SQL driver (we were using a Microsoft driver before, we switched to JTDS) and all problems have gone away.

    So I'm guessing the Microsoft driver was returning the string "null" when it should have been returning null.

    So to answer the question, the ResultSet.getString() method was probably behaving correctly all along, but the SQL driver was returning bad data.