Get a Map from a JDBC ResultSet
Get a Java Map from a JDBC ResultSet, using a specified column value as the Map key.
Javadoc available at https://www.javatapas.com/docs/javatapas/sql/GetMapFromResultSet.html
private Map<String, Map<String, String>> getMapFromResultSet(ResultSet rset, String keyCol) throws SQLException {
Map<String, Map<String, String>> rsMap = new LinkedHashMap<>();
ResultSetMetaData rsmd = rset.getMetaData();
//this.logger.log(Level.INFO, "rsmd.getColumnCount(): " + rsmd.getColumnCount());
int rowIdx = 0;
while (rset.next()){
rowIdx++;
String id = null;
if (keyCol != null){id = rset.getString(keyCol);}
else {id = String.valueOf(rowIdx);}
Map<String, String> valueMap = new LinkedHashMap<>();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String colName = rsmd.getColumnLabel(i);
if (valueMap.containsKey(colName)){colName = colName + i;}
valueMap.put(colName, rset.getString(i));
}
rsMap.put(id, valueMap);
}
return rsMap;
}