码迷,mamicode.com
首页 > 其他好文 > 详细

Resultset 转化成list或map

时间:2019-02-12 23:14:08      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:next   generate   treemap   result   exception   tst   ada   nbsp   i++   

public static List<Map<String, Object>> convertList(ResultSet rs) {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    try {
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        while (rs.next()) {
            Map<String, Object> rowData = new HashMap<String, Object>();
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));
            }
            list.add(rowData);
        }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            if (rs != null)
            rs.close();
            rs = null;
        } catch (SQLException e) {
            e.printStackTrace();
    }
}
    return list;
}


public static Map<String, Object> convertMap(ResultSet rs){
    Map<String, Object> map = new TreeMap<String, Object>();
    try{
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        while (rs.next()) {
            for (int i = 1; i <= columnCount; i++) {
                map.put(md.getColumnName(i), rs.getObject(i));
            }
        }
    } catch (SQLException e){
        e.printStackTrace();
    } finally {
        try {
            if (rs != null)
            rs.close();
            rs = null;
        } catch (SQLException e) {
            e.printStackTrace();
    }
    return map;
}

 

Resultset 转化成list或map

标签:next   generate   treemap   result   exception   tst   ada   nbsp   i++   

原文地址:https://www.cnblogs.com/xunyi/p/10367466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!