将ResultSet转为List

来源:互联网 发布:java redis maven 编辑:程序博客网 时间:2024/04/28 13:15

http://www.cnblogs.com/seaven/archive/2009/07/21/1527509.html

复制代码
public static List resultSetToList(ResultSet rs) throws java.sql.SQLException {   
           
if (rs == null)   
               
return Collections.EMPTY_LIST;   
           ResultSetMetaData md 
= rs.getMetaData(); //得到结果集(rs)的结构信息,比如字段数、字段名等   
           int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数   
           List list = new ArrayList();   
           Map rowData 
= new HashMap();   
           
while (rs.next()) {   
            rowData 
= new HashMap(columnCount);   
            
for (int i = 1; i <= columnCount; i++) {   
                    rowData.put(md.getColumnName(i), rs.getObject(i));   
            }   
            list.add(rowData);   
            System.out.println(
"list:" + list.toString());   
           }   
           
return list;   
   }  

复制代码
接着在其他方法里处理返回的List
List ls = resultSetToList(rs);   
Iterator it 
= ls.iterator();   
while(it.hasNext()) {   
    Map hm 
= (Map)it.next();   
    System.out.println(hm.get(
"字段名大写"));   
}  
分类: Java
好文要顶 关注我 收藏该文  
seaven
关注 - 0
粉丝 - 10
+加关注
1
0
(请您对文章做出评价)
« 上一篇:Java Timer
» 下一篇:Java调用C#的web Service

0 0