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

StringgetOjbClassName(ResultSet rs)
Returns the name of the class to be instantiated.
try {
    return rs.getString(OJB_CLASS_COLUMN);
} catch (SQLException e) {
    return null;
OptionalIntgetOptionalInt(ResultSet rs, String name)
get Optional Int
int value = rs.getInt(name);
return rs.wasNull() ? OptionalInt.empty() : OptionalInt.of(value);
PathgetPath(ResultSet r, String columnName)
get Path
String val = r.getString(columnName);
return val != null ? Paths.get(val) : null;
List>getRecordsFromResultSet(ResultSet rs)
get Records From Result Set
String[] columnNames = new String[rs.getMetaData().getColumnCount()];
for (int i = 0; i < columnNames.length; i++)
    columnNames[i] = rs.getMetaData().getColumnName(i + 1);
List<Map<String, String>> records = new Vector<Map<String, String>>();
while (rs.next()) {
    Map<String, String> record = new HashMap<String, String>(columnNames.length);
    for (int i = 0; i < columnNames.length; i++) {
        String columnName = columnNames[i];
...
String[][]getResult(ResultSet rs)
get Result
List<String[]> results = new ArrayList<String[]>();
int noOfColumns = rs.getMetaData().getColumnCount();
while (rs.next()) {
    String[] oneRow = new String[noOfColumns];
    for (int i = 1; i <= noOfColumns; i++) {
        oneRow[i - 1] = rs.getString(i);
    results.add(oneRow);
...
double[]getResultArray(ResultSet resultSet, int size, java.util.Date startDate)
Pull the data out of the result set and date template it.
double[] returnArray = new double[size];
Calendar cal = new GregorianCalendar();
if (startDate != null) {
    cal.setTime(startDate);
} else {
    cal.setTime(new java.util.Date());
    cal.add(Calendar.DATE, -(size - 1));
Calendar cal2 = new GregorianCalendar();
try {
    int index = 0;
    while (resultSet.next() && index < size) {
        cal2.setTime(resultSet.getDate("Date"));
        while ((cal.get(Calendar.DATE) != cal2.get(Calendar.DATE))
                || (cal.get(Calendar.MONTH) != cal2.get(Calendar.MONTH))
                || (cal.get(Calendar.YEAR) != cal2.get(Calendar.YEAR))) {
            cal.add(Calendar.DATE, 1);
            returnArray[index] = 0;
            index++;
        returnArray[index] = resultSet.getDouble("Count");
        index++;
        cal.add(Calendar.DATE, 1);
    while (index < size) {
        returnArray[index] = 0;
        index++;
} catch (SQLException sqle) {
    System.err.println("ERROR - [DetectorHelper]: " + sqle.getMessage());
    sqle.printStackTrace();
return returnArray;
ListgetResultByMap(ResultSet rs)
get Result By Map
List<Object> list = new ArrayList<Object>();
ResultSetMetaData rsmData = rs.getMetaData();
while (rs.next()) {
    Map<Object, Object> map = new HashMap<Object, Object>();
    for (int i = 1; i <= rsmData.getColumnCount(); i++) {
        map.put(rsmData.getColumnName(i), rs.getObject(i));
    list.add(map);
...
List>getResults(ResultSet result)
get Results
List<List<Object>> results = new ArrayList<List<Object>>();
int numColumns = result.getMetaData().getColumnCount();
while (result.next()) {
    List<Object> row = new ArrayList<Object>();
    results.add(row);
    for (int index = 1; index <= numColumns; index++) {
        row.add(result.getObject(index));
return results;
MapgetResultsAsMap(ResultSet rs)
Get the first row of the ResultSet as Map, converting the column names to upper case.
Map<String, Object> result = new TreeMap<String, Object>();
ResultSetMetaData meta = rs.getMetaData();
int count = meta.getColumnCount();
List<String> colNames = new ArrayList<String>();
for (int i = 1; i <= count; i++) {
    colNames.add(meta.getColumnLabel(i).toUpperCase());
if (rs.next()) {
...
ResultSetgetResultSet(Connection c, String query)
get Result Set
return getResultSet(c, query, null);