Skip to content

Instantly share code, notes, and snippets.

@swrnvinod40
Forked from cworks/ResultSet to List
Created April 21, 2017 20:32
Show Gist options
  • Save swrnvinod40/b8b19295092d2b3457e1b66ba93c8c28 to your computer and use it in GitHub Desktop.
Save swrnvinod40/b8b19295092d2b3457e1b66ba93c8c28 to your computer and use it in GitHub Desktop.
Java - Convert a ResultSet to a List of Maps, where each Map is a row of data
/**
* Convert the ResultSet to a List of Maps, where each Map represents a row with columnNames and columValues
* @param rs
* @return
* @throws SQLException
*/
private List<Map<String, Object>> resultSetToList(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
while (rs.next()){
Map<String, Object> row = new HashMap<String, Object>(columns);
for(int i = 1; i <= columns; ++i){
row.put(md.getColumnName(i), rs.getObject(i));
}
rows.add(row);
}
return rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment