Java SQL ResultSet Read getHashMap(ResultSet resultSet)

Here you can find the source of getHashMap(ResultSet resultSet)

Description

Description - static method which takes the result set as an argument ,iterates over it to build a list of maps with column name as key,column value as its value

License

Open Source License

Parameter

Parameter Description
resultSet - ResultSet object

Exception

Parameter Description
SQLException an exception

Return

rowList - List of maps which has all records

Declaration


public static List<Map<String, Object>> getHashMap(ResultSet resultSet) throws SQLException 

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;

public class Main {
    /** /*from w w w  . ja va2  s . c  om*/
     * Description - static method which takes the result set as an argument ,iterates over it to 
     *               build a list of maps with column name as key,column value as its value 
     *               
     * @param resultSet - ResultSet object
     * @return rowList - List of maps which has all records 
     * @throws SQLException                
     */

    public static List<Map<String, Object>> getHashMap(ResultSet resultSet) throws SQLException {

        // declare a list of maps to hold all records 
        List<Map<String, Object>> rowList = new ArrayList<>();

        // get result set meta data 
        ResultSetMetaData metaData = resultSet.getMetaData();

        // get column count 
        int colCount = metaData.getColumnCount();

        // loop through the result set ,  create a map for every record,put all 
        // columns names and values and add it to a list
        while (resultSet.next()) {

            // declare a map for each record
            Map<String, Object> columns = new HashMap<>();

            // keeping loading the map for each column 
            for (int i = 1; i <= colCount; i++) {
                columns.put(metaData.getColumnLabel(i), resultSet.getObject(i));
            }

            // add it to a list after gathering all columns info
            rowList.add(columns);
        }

        // return list of maps 
        return rowList;
    }
}

Related

  1. getFieldsPresentInResultSet(ResultSet rs)
  2. getFirstGeometryFieldIndex(ResultSet resultSet)
  3. getFirstInt(ResultSet resultSet)
  4. getFlags(ResultSet rs)
  5. getGeneratedIdFromResultSet(ResultSet resultSet)
  6. getHeaders(ResultSetMetaData rsmd)
  7. getHighPrecisionString(ResultSet rs, int ix, int sql_type)
  8. getHtmlRows(ResultSet results)
  9. getHtmlTable(ResultSet results)