Java Utililty Methods SQL ResultSet to

List of utility methods to do SQL ResultSet to

Description

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

Method

ListresultSetToArrayList(ResultSet rs)
result Set To Array List
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
ArrayList list = new ArrayList();
while (rs.next()) {
    HashMap row = new HashMap(columns);
    for (int i = 1; i <= columns; ++i) {
        row.put(md.getColumnName(i), rs.getObject(i));
    list.add(row);
return list;
StringresultSetToHtmlTable(java.sql.ResultSet rs)
result Set To Html Table
int rowCount = 0;
final StringBuilder result = new StringBuilder();
result.append("<P ALIGN='center'>\n<TABLE BORDER=1>\n");
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
result.append("\t<TR>\n");
for (int i = 0; i < columnCount; ++i) {
    result.append("\t\t<TH>").append(rsmd.getColumnLabel(i + 1)).append("</TH>\n");
...
voidresultSetToMap(Map mappedValues, java.sql.ResultSet rs, int key, int value)
result Set To Map
while (rs.next()) {
    mappedValues.put(rs.getObject(key), rs.getObject(value));
voidresultSetToMap(Map mappedValues, ResultSet rs)
result Set To Map
while (rs.next()) {
    mappedValues.put(rs.getObject(1), rs.getObject(2));
MapresultSetToOrderedMap(ResultSet rs)
result Set To Ordered Map
Map<String, Object> result = new LinkedHashMap<>();
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
for (int i = 1; i <= cols; i++) {
    result.put(rsmd.getColumnName(i), rs.getObject(i));
return result;
SetresultSetToSet(PreparedStatement statement)
result Set To Set
ResultSet results = statement.executeQuery();
try {
    Set<String> strings = new HashSet<String>();
    while (results.next()) {
        strings.add(results.getString(1));
    return strings;
} finally {
...
StringresultSetToString(ResultSet resultSet)
result Set To String
StringBuilder sb = new StringBuilder();
ResultSetMetaData rsmd = resultSet.getMetaData();
int numOfColumns = rsmd.getColumnCount();
for (int i = 1; i <= numOfColumns; i++) {
    if (i > 1)
        sb.append(",");
    String columnName = rsmd.getColumnName(i);
    sb.append(columnName);
...
StringBuilderresultSetToString(ResultSet resultSet)
result Set To String
ResultSetMetaData metaData = resultSet.getMetaData();
int countOfColumns = metaData.getColumnCount();
StringBuilder builder = new StringBuilder();
builder.append("<table>");
builder.append("<tr>");
for (int i = 0; i < countOfColumns; i++) {
    builder.append("<th>");
    builder.append(metaData.getColumnName(i + 1));
...
StringresultSetToXML(ResultSet rs)
result Set To XML
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
StringBuffer ret = new StringBuffer();
while (rs.next()) {
    ret.append("<Table>");
    for (int i = 1; i <= colCount; i++) {
        String columnName = rsmd.getColumnName(i).replaceAll(" ", "&nbsp;");
        String value = rs.getString(i);
...
ListtoArrayList(ResultSet rs)
to Array List
List arrayList = null;
try {
    arrayList = new ArrayList();
    int iCol = rs.getMetaData().getColumnCount();
    while (rs.next()) {
        Object[] objArray = new Object[iCol];
        for (int i = 1; i <= iCol; i++) {
            objArray[i - 1] = rs.getObject(i);
...