Chinaunix首页 | 论坛 | 博客
  • 博客访问: 701686
  • 博文数量: 178
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1507
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-27 23:20
文章分类

全部博文(178)

文章存档

2015年(58)

2014年(121)

我的朋友

分类: Java

2014-11-18 21:54:01

将ResultSet转为List


点击(此处)折叠或打开

  1. public static List resultSetToList(ResultSet rs) throws java.sql.SQLException {
  2.            if (rs == null)
  3.                return Collections.EMPTY_LIST;
  4.            ResultSetMetaData md = rs.getMetaData(); //得到结果集(rs)的结构信息,比如字段数、字段名等
  5.            int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数
  6.            List list = new ArrayList();
  7.            Map rowData = new HashMap();
  8.            while (rs.next()) {
  9.             rowData = new HashMap(columnCount);
  10.             for (int i = 1; i <= columnCount; i++) {
  11.                     rowData.put(md.getColumnName(i), rs.getObject(i));
  12.             }
  13.             list.add(rowData);
  14.             System.out.println("list:" + list.toString());
  15.            }
  16.            return list;
  17.    }
接着在其他方法里处理返回的List

点击(此处)折叠或打开

  1. List ls = resultSetToList(rs);
  2. Iterator it = ls.iterator();
  3. while(it.hasNext()) {
  4.     Map hm = (Map)it.next();
  5.     System.out.println(hm.get("字段名大写"));
  6. }


阅读(654) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~