到目前为止,我已经像其他任何写一样编写了我的方法(并且一切正常),但这有问题。 代码对数据库执行select查询并创建MyObject类型的ArrayList:
MyObject current;
ArrayList<MyObject> res = new ArrayList<MyObject>();
Connection conn=ConnectionManager.getConnection();
Statement stm = conn.createStatement();
ArrayList<MyObject> res = new ArrayList<MyObject>();
Result set rs = stm.executeQuery(QUERY);
while(rs.next()) {
current = new MyObject();
current.setField1(rs.getInt(1));
current.setField2(rs.getInt(2));
System.out.println(current.getField1+" "+current.getField2());
res.add(current);
}
(MyObject扩展了一个包含方法setField2(int a)的超类)。 后续问题。如果我直接在数据库上执行查询,我会得到以下结果:
0 5000
1 5000
2 6000
但是Java代码的查询执行返回
0 6000
1 6000
2 6000
MyObject和MySuperObject类
public class MyObject extends MySuperObject {
potected int field1;
public MyObject() {
super();
}
@Override
public int getField1() {
return this.field1;
}
public void setField1(int f) {
this.field1 = f;
}
}
public class MySuperObject {
potected int field1, field2;
public MySuperObject() {
}
public int getField1() {
return this.field1;
}
public void setField1(int f) {
this.field1 = f;
}
public void setField2(int f) {
this.field2 = f;
}
public int getField2() {
return this.field2;
}
}