把从数据库中查出来的记录存到list当中,怎么做不用开始的时候建一个对象数组,一个个存入list,
[code]
public List<StudentTest> getFirstTest() throws Exception {

dataConection first = null;

List<StudentTest> firstList = new ArrayList();
StudentTest[] st = new StudentTest[27];

int i_flag;
i_flag=0;


try{
first = new dataConection("select t_first_id,t_first_content,t_first_fb from cqes_eval_first order by t_first_id asc");
first.dataConnect();
ResultSet first_un = first.dataresult();

while(first_un.next()){
st[i_flag] = new StudentTest();
Integer tid = (Integer)first_un.getInt(1);
st[i_flag].setTestId(tid.intValue());
st[i_flag].setTestsub(first_un.getString(2));
st[i_flag].setTestfb(first_un.getString(3));

firstList.add(st[i_flag]);
i_flag++;
}


}
catch(Exception e){
e.printStackTrace();
throw new Exception("查询数据库一时出现异常");
}
finally{
first.dataClosed();
}

return firstList;
} public List<StudentTest> getSecondTest() throws Exception {
// TODO Auto-generated method stub
return null;
}
[/code]

解决方案 »

  1.   

    不用建个对象数组吧? 建个对象把从数据库里的记录绑定到对象的属性 然后把对象放到list里面
      

  2.   

    数据库记录放到对象中
    对象放到list里
      

  3.   

    比如你有一个实体类Bean
    对应数据库中有一个表有两列name,pass
    那么你给实体类设置两属性name,pass用来保存相应列的数据在得到RsultSet后用循环
    Bean bean=null;
    List list = new ArrayList();
    while(rs.next()){
      bean = new Bean();
      bean.setName(rs.getString("name"));
      bean.setPass(rs.getString("pass"));
      list.add(bean);
    }就可以了
      

  4.   

    给你个JDBC的例子
    前面的SQL语句省略
    ArrayList list=new ArrayList();rs=pst.executeQuery();
    while(rs.next()){
        Person p=new Person();
        p.setName(rs.getString("name"));
        p.setSex(rs.getInt("sex"));
        p.setAddress(rs.getString("address"));
        list.add(p);
    }
      

  5.   

    你这个很像hibernate中的查询,ls的灵活度不高
      

  6.   

    按照楼主的意思他应该没用hibernate,一般的话就是JDBC
    这样的灵活性是不高,但是容易让楼主看懂
      

  7.   

    说的不错,我就是jdbc,嘎嘎,谢谢
      

  8.   

    LZ说的是范型吧
    List<Object>list里面可以放对象
    6楼正解