把从数据库中查出来的记录存到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]
[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]
对象放到list里
对应数据库中有一个表有两列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);
}就可以了
前面的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);
}
这样的灵活性是不高,但是容易让楼主看懂
List<Object>list里面可以放对象
6楼正解