views:

1452

answers:

1

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

+4  A: 

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?

lumpynose
I just noticed that you're going through ODBC; you probably want to strive for maximum portability in that case, as compared to a regular jdbc driver.
lumpynose
The same. When I don't debug and just look at the output, my addressLine1 value is 30 character string of whitespace, and addressLine2 is null.If I create a temp String like this: String tmp = rs.getString("AddressLine1"); and debug, the value of tmp is correct. I'm really at a loss here.
Boden
Uh, why can't you do `String tmp = rs.getString("AddressLine1"); address.setAddressLine1(tmp);`? Is it now just a matter of getting to the bottom of it?
Michael Myers
Actually I just tried that... same result. The way it behaves in the debugger is not terribly predictable.
Boden
Ok, I found a goof in my code where I checked a column twice. Fixing this and making things nice per lumpynose's suggestions (e.g. left-to-right reading order) got everything working. Thanks!
Boden