Boden
Boden

Reputation: 4167

Java ResultSet getString weirdness?

This one has me stumped.

I've got a java.sql.ResultSet and I'm pulling string values out of it like so:

address.setAddressLine1(rs.getString("AddressLine1"));
address.setAddressLine2(rs.getString("AddressLine2"));

When I debug and inspect rs.getString("AddressLine1"), the debugger says I've got a 30 character string: "ATTN: ACCOUNTS PAYABLE" (trailing spaces removed). However, when I inspect my address object immediately after this line, it's reporting that addressLine1 is a 30 character string of spaces or some other whitespace.

When I debug and inspect rs.getString("AddressLine2"), the debugger says that I've got a 23 character string: "10 Something Street" (trailing spaces removed). When I inspect my address object immediately after this line, it's reporting addressLine2 is null.

What's really driving me nuts is that this isn't happening with every value, only a few.

My address class is storing plain old strings with totally dumb getters and setters. Really, I promise! This is cut & paste with other dumb properties removed:

public class Address implements java.io.Serializable {

 private static final long serialVersionUID = 1L;
 private String addressLine1; 
 private String addressLine2; 

 public String getAddressLine1() {
  return addressLine1;
 }

 public void setAddressLine1(String addressLine1) {
  this.addressLine1 = addressLine1;
 }

 public String getAddressLine2() {
  return addressLine2;
 }

 public void setAddressLine2(String addressLine2) {
  this.addressLine2 = addressLine2;
 }
}

What in the heck is going on here? Is it some kind of weird scoping problem? Is it some kind of character encoding thing?

P.S. - I'm using spring's SimpleJdbcTemplate, and the "pseudo database" I'm talking to is ProvideX which I'm accessing via ODBC (sun.jdbc.odbc.JdbcOdbcDriver).

Upvotes: 0

Views: 5592

Answers (1)

user98989
user98989

Reputation:

Note what the javadocs say about ResultSet: "For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once." So when you're reading the column by using the debugger, that's one read, then your code reads it a second time when setAddressLine1 is called.

How does it behave when you only look at addressLine1 and don't fiddle with the ResultSet?

Upvotes: 4

Related Questions