Java中如何将ReslutSet转换为List<T>
publicstaticList<Departments>convertList(ResultSetrs)throwsSQLException{List<Departme...
public static List<Departments> convertList(ResultSet rs) throws SQLException {
List<Departments> list = new ArrayList<Departments>();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount(); // Map rowData;
Departments dep = new Departments();
while (rs.next()) { // rowData = new HashMap(columnCount);
for(int i = 1; i <= columnCount; i++){
dep.setDeptNo(rs.getString(1));
dep.setDeptName(rs.getString(2));
}
//System.out.println(dep.getDeptNo()+dep.getDeptName());
list.add(dep);
}
rs.close();
return list;
}
以上是我写的程序。虽然程序将ReslutSet中的内容转换成了Departments类,并存入了List<Departments>中,但有一个问题:List中存入的只是最后一条记录,而且这条记录被重复了11次(即要查询的departments这个table中记录的个数)。自己不知道问题处在了哪里,还望指教,不胜感激! 展开
List<Departments> list = new ArrayList<Departments>();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount(); // Map rowData;
Departments dep = new Departments();
while (rs.next()) { // rowData = new HashMap(columnCount);
for(int i = 1; i <= columnCount; i++){
dep.setDeptNo(rs.getString(1));
dep.setDeptName(rs.getString(2));
}
//System.out.println(dep.getDeptNo()+dep.getDeptName());
list.add(dep);
}
rs.close();
return list;
}
以上是我写的程序。虽然程序将ReslutSet中的内容转换成了Departments类,并存入了List<Departments>中,但有一个问题:List中存入的只是最后一条记录,而且这条记录被重复了11次(即要查询的departments这个table中记录的个数)。自己不知道问题处在了哪里,还望指教,不胜感激! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询