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

StringgetResultSetRowString(ResultSet rs)
Turn all columns into one ; delimited seting Primarily useful in testing that two rows are equivalent
int nCols = getNumberColumns(rs);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < nCols; i++) {
    sb.append(rs.getString(i + 1));
    if (i < nCols - 1)
        sb.append(";");
return (sb.toString());
...
String[]getResultSetStrings(ResultSet rs)
get Result Set Strings
int nCols = getNumberColumns(rs);
String[] ret = new String[nCols];
for (int i = 0; i < ret.length; i++) {
    ret[i] = rs.getString(i + 1);
return (ret);
ObjectgetResultSetValue(ResultSet rs, int index)
Retrieve a JDBC column value from a ResultSet, using the most appropriate value type.
Object obj = null;
try {
    obj = rs.getObject(index);
} catch (SQLException e) {
    if (e.getMessage().equals("The conversion from char to SMALLINT is unsupported.")) {
        obj = rs.getString(index);
    } else {
        throw e;
...
ObjectgetResultSetValue(ResultSet rs, int index)
get Result Set Value
Object obj = rs.getObject(index);
String className = null;
if (obj != null) {
    className = obj.getClass().getName();
if (obj instanceof Blob) {
    obj = rs.getBytes(index);
} else if (obj instanceof Clob) {
...
ObjectgetResultSetValue(ResultSet rs, int index, Class requiredType)
adopted from springframework
if (requiredType == null) {
    return getResultSetValue(rs, index);
Object value = null;
boolean wasNullCheck = false;
if (String.class.equals(requiredType)) {
    value = rs.getString(index);
} else if (boolean.class.equals(requiredType) || Boolean.class.equals(requiredType)) {
...
ObjectgetResultSetValue(ResultSet rs, int index, Class requiredType)
get Result Set Value
if (requiredType == null) {
    return getResultSetValue(rs, index);
Object value;
boolean wasNullCheck = false;
if (String.class.equals(requiredType)) {
    value = rs.getString(index);
} else if (boolean.class.equals(requiredType) || Boolean.class.equals(requiredType)) {
...
intgetReturnCountInt(ResultSet rs, int column)
Fetches the return count from a query such as "SELECT COUNT(*) FROM ...".
if (!(rs.next()))
    throw new SQLException("expected return count row, not present");
return rs.getInt(column);
ListgetRow(ResultSet rs)

Purpose: get the next row from the ResultSet as a Collection
Assumptions: that rs.next() was already called and is true.
ResultSetMetaData meta = rs.getMetaData();
int numberOfColumns = meta.getColumnCount();
return getRow(rs, meta, numberOfColumns, null);
intgetRowCount(final ResultSet rs)
Get a ResultSet's size.
return rs.last() ? rs.getRow() : 0;
intgetRowCount(ResultSet set)
get Row Count
int rowCount;
int currentRow = set.getRow(); 
rowCount = set.last() ? set.getRow() : 0; 
if (currentRow == 0) 
    set.beforeFirst(); 
else 
    set.absolute(currentRow); 
return rowCount;
...