Java Utililty Methods SQL ResultSet Read

List of utility methods to do SQL ResultSet Read

Description

The list of methods to do SQL ResultSet Read are organized into topic(s).

Method

List>getHashMap(ResultSet resultSet)
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
List<Map<String, Object>> rowList = new ArrayList<>();
ResultSetMetaData metaData = resultSet.getMetaData();
int colCount = metaData.getColumnCount();
while (resultSet.next()) {
    Map<String, Object> columns = new HashMap<>();
    for (int i = 1; i <= colCount; i++) {
        columns.put(metaData.getColumnLabel(i), resultSet.getObject(i));
    rowList.add(columns);
return rowList;
ListgetHeaders(ResultSetMetaData rsmd)
get Headers
List<String> h = new ArrayList<String>();
int columns = rsmd.getColumnCount();
for (int i = 1; i <= columns; i++) {
    h.add(rsmd.getColumnName(i));
return h;
StringgetHighPrecisionString(ResultSet rs, int ix, int sql_type)
get High Precision String
switch (sql_type) {
case Types.DATE:
    return getString(rs, ix, Types.TIMESTAMP);
default:
    return getString(rs, ix, sql_type);
StringgetHtmlRows(ResultSet results)
get Html Rows
StringBuffer htmlRows = new StringBuffer();
ResultSetMetaData metaData = results.getMetaData();
int columnCount = metaData.getColumnCount();
htmlRows.append("<tr>");
for (int i = 1; i <= columnCount; i++)
    htmlRows.append("<td><b>" + metaData.getColumnName(i) + "</td>");
htmlRows.append("</tr>");
while (results.next()) {
...
StringgetHtmlTable(ResultSet results)
get Html Table
StringBuilder htmlTable = new StringBuilder();
ResultSetMetaData metaData = results.getMetaData();
int columnCount = metaData.getColumnCount();
htmlTable.append("<table>");
htmlTable.append("<tr>");
for (int i = 1; i <= columnCount; i++) {
    htmlTable.append("<th>");
    htmlTable.append(metaData.getColumnName(i));
...
LonggetId(ResultSet key)
Extract key from given ResultSet.
if (key.getMetaData().getColumnCount() != 1) {
    throw new IllegalArgumentException("Given ResultSet contains more columns");
if (key.next()) {
    Long result = key.getLong(1);
    if (key.next()) {
        throw new IllegalArgumentException("Given ResultSet contains more rows");
    return result;
} else {
    throw new IllegalArgumentException("Given ResultSet contain no rows");
intgetIndex(ResultSet rs, int index)
get Index
return rs.getInt(index);
ListgetList(ResultSet resultSet, String columnLabel, Class clazz)
get List
return Arrays.asList(getArray(resultSet, columnLabel, clazz));
List>getList(ResultSet rs)
get List
if (rs == null) {
    return null;
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>(50);
while (rs.next()) {
    HashMap<String, String> row = new HashMap<String, String>(columns);
...
ListgetListFromRS(Class clazz, ResultSet rs)
get List From RS
return getListFromRS(clazz, rs, false);