Here you can find the source of getHashMap(ResultSet resultSet)
Parameter | Description |
---|---|
resultSet | - ResultSet object |
Parameter | Description |
---|---|
SQLException | an exception |
public static List<Map<String, Object>> getHashMap(ResultSet resultSet) throws SQLException
//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; } }