Java SQL ResultSet to resultSet2List(ResultSet rs)

Here you can find the source of resultSet2List(ResultSet rs)

Description

result Set List

License

Open Source License

Declaration

private static List<Map<String, Object>> resultSet2List(ResultSet rs) 

Method Source Code

//package com.java2s;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.base.Strings;

public class Main {
    private static List<Map<String, Object>> resultSet2List(ResultSet rs) {
        List<Map<String, Object>> list = null;
        if (rs != null) {
            list = new ArrayList<Map<String, Object>>();
            try {
                while (rs.next()) {
                    Map<String, Object> map = resultSet2Map(rs);
                    if (map != null && map.size() > 0) {
                        list.add(map);/*from  w w  w. ja va  2 s.  c o m*/
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    private static Map<String, Object> resultSet2Map(ResultSet rs) {
        Map<String, Object> map = null;
        if (rs != null) {
            map = new HashMap<String, Object>();
            try {
                ResultSetMetaData rsMetaData = rs.getMetaData();
                int columnCount = rsMetaData.getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    String cName = rsMetaData.getColumnLabel(i);
                    if (!Strings.isNullOrEmpty(cName)) {
                        map.put(cName, rs.getString(cName));
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return map;
    }
}

Related

  1. convert(int sqlType, String type, ResultSet rs, String name)
  2. convert(ResultSet rs)
  3. convertResultSetToJSON(ResultSet resultSet)
  4. convertResultSetToMap(final ResultSet rs)
  5. convertToMap(Map metaData, ResultSet rs)
  6. resultSet2Map(ResultSet rs)
  7. resultSetAsCSV(ResultSet rsh)
  8. resultSetCurrentData(ResultSet rs)
  9. resultSetParseVO(ResultSet rs, Class cls)