这个把我难住了。
我有一个 java.sql.ResultSet,我正在从中提取字符串值,如下所示:
address.setAddressLine1(rs.getString("AddressLine1"));
address.setAddressLine2(rs.getString("AddressLine2"));
当我调试和检查 rs.getString("AddressLine1") 时,调试器说我有一个 30 个字符的字符串:"收件人:应付帐款”(删除尾随空格)。 但是,当我在该行之后立即检查我的地址对象时,它报告 addressLine1 是一个由 空格 或其他空白组成的 30 个字符的字符串。
当我调试和检查 rs.getString("AddressLine2") 时,调试器说我有一个 23 个字符的字符串:“10 Something Street”(删除了尾随空格)。 当我在该行之后立即检查我的地址对象时,它报告 addressLine2 为 null。
真正让我抓狂的是,这种情况并没有发生在所有值上,只有少数值会发生。
我的地址类正在存储带有完全愚蠢的 getter 和 setter 的普通旧字符串。 真的,我保证! 这是剪切& 删除其他愚蠢属性的粘贴:
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;
}
}
这里到底发生了什么? 这是某种奇怪的范围问题吗? 它是某种字符编码吗?
PS - 我正在使用 spring 的 SimpleJdbcTemplate,而我正在谈论的“伪数据库”是 ProvideX,我通过 ODBC (sun.jdbc.odbc.JdbcOdbcDriver) 访问它。
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).
发布评论
评论(1)
请注意 javadoc 关于 ResultSet 的说法:“为了获得最大的可移植性,每行中的结果集列应按从左到右的顺序读取,并且每列应仅读取一次。” 因此,当您使用调试器读取该列时,这是一次读取,然后当调用 setAddressLine1 时,您的代码会再次读取它。
当您只查看 addressLine1 而不摆弄 ResultSet 时,它的行为如何?
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?