java 怎样把resultset转成list
1个回答
展开全部
【方法1】借助bean的办法
//rs是一个查询语句所返回的结果集
ResultSet rs=new ResultSet();
rs=pstmt.executeQuery();
//将rs逐条取出,并将每一项放在一个bean对应的值里
while(rs.next())
{
RecBean rec=new RecBean();
rec.setId(rs.getInt(1));
rec.setAgentNo(rs.getString(2));
rec.setFileName(rs.getString(3));
list.add(rec);
}
2
【方法2】借助ResultSetMetaData 和Map
private static List convertList(ResultSet rs) throws SQLException {
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()) {
Map rowData = new HashMap();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
}
return list;
}
//rs是一个查询语句所返回的结果集
ResultSet rs=new ResultSet();
rs=pstmt.executeQuery();
//将rs逐条取出,并将每一项放在一个bean对应的值里
while(rs.next())
{
RecBean rec=new RecBean();
rec.setId(rs.getInt(1));
rec.setAgentNo(rs.getString(2));
rec.setFileName(rs.getString(3));
list.add(rec);
}
2
【方法2】借助ResultSetMetaData 和Map
private static List convertList(ResultSet rs) throws SQLException {
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()) {
Map rowData = new HashMap();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
}
return list;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询