关于Vector存储对象..想了半天就是搞不懂,达人来帮帮啊··
import java.util.Vector;
/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2007 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class VectorTest {
public VectorTest() {
}
String a;
String b;
public String toString(){
return a+ " "+b;
}
public static void main(String[] args) {
Vector test=new Vector();
VectorTest vectortest = new VectorTest();
vectortest.a= "a ";
vectortest.b= "b ";
test.add(vectortest);
vectortest.a= "b ";
vectortest.b= "a ";
test.add(vectortest);
System.out.println(test);
}
}
这样的输出结果总是 后一个的【b a,b a】前面的总 是被覆盖 为什么啊,如果我要把前面的和后面的都输出要怎么做呢?就像【a b,b a】
------解决方案--------------------你add到vector的是同一个引用,即vectortest
你new两个vectortest就可以了
------解决方案--------------------public Vector dateQuery(String sql) {
Vector v = new Vector();
ResultSet rs = null;
DatebaseTest a = new DatebaseTest();
DatebaseTest b = new DatebaseTest();
try {
Connection con = this.getConnection();
Statement st = con.createStatement();
rs = st.executeQuery(sql);
int count = 0;
while (rs.next()) {
count++;
if (count == 1) {
a.name = rs.getString(1);
a.sex = rs.getString(2);
a.age = rs.getString(3);
v.add(a);
} else if (count == 2) {
b.name = rs.getString(1);
b.sex = rs.getString(2);
b.age = rs.getString(3);
v.add(b);
}
}
rs.close();
st.close();
con.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return v;
}
放到另外一个位置new
public Vector dateQuery(String sql) {
// Vector v = new Vector();
ResultSet rs = null;