Resultset 转化成list或map

Posted xunyi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Resultset 转化成list或map相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

list转换成MAP 求各位大神啊!!!

将JDBC ResultSet结果集转成List

mybatis如何转化成map<DATE,list>

java8 .stream().sorted().filter().map().collect()用法

数据格式汇总2-java

如何将这个JSON字符串转化成list对象