首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

关于Vector存储对象.想了半天就是搞不懂,达人来帮帮啊··解决方法

2012-02-23 
关于Vector存储对象..想了半天就是搞不懂,达人来帮帮啊importjava.util.Vector/***p Title:/p**p D

关于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;


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()) {
Vector v = new Vector();


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;
}


结贴给分吧,谢谢



[解决办法]
修改了下,应该可以.
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;
}
[解决办法]
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()) {
DatebaseTest a = new DatebaseTest();//在这里New,每循环一次就创建一个对像
DatebaseTest b = new DatebaseTest();//在这里New,每循环一次就创建一个对像


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;
}

[解决办法]
这些都是 java基础知识,看看书,补补基础就解决了,好运

热点排行
Bad Request.