zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    数组的地址和vector数组的地址
    字节跳动 测试开发工程师 面经
    最短路径树
    SPFA
    树的直径
    树的重心
    CF1401D Maximum Distributed Tree
    期望简述
    CF723E One-Way Reform
    CF1409E Two Platforms
  • 原文地址:https://www.cnblogs.com/xunyi/p/10367466.html
Copyright © 2011-2022 走看看