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

TgetEnum(ResultSet rs, int index, Class type)
get Enum
int value = rs.getInt(index);
for (T t : type.getEnumConstants()) {
    if (t.ordinal() == value)
        return t;
return null;
TgetEnumFromResultSet(ResultSet rset, Enum field, T[] enumValues)
get Enum From Result Set
return getEnumFromResultSet(rset, field.name(), enumValues);
ClassgetExpectedType(ResultSet resultSet, int columnIndex)
get Expected Type
Class expectedType;
ResultSetMetaData metaData = resultSet.getMetaData();
int sqlType = metaData.getColumnType(columnIndex);
int decimalDigits = metaData.getScale(columnIndex);
expectedType = getClassByJdbcType(sqlType, decimalDigits);
return expectedType;
StringgetExplainPlan(ResultSet rs)
get Explain Plan
StringBuilder buf = new StringBuilder();
while (rs.next()) {
    buf.append(rs.getString(1));
    buf.append('\n');
if (buf.length() > 0) {
    buf.setLength(buf.length() - 1);
return buf.toString();
String[]getFieldNames(ResultSet rs)
Return field names
if (rs == null)
    return null;
ResultSetMetaData metaData = rs.getMetaData();
int col_num = metaData.getColumnCount();
String[] col_names = new String[col_num];
for (int i = 0; i < col_num; i++)
    col_names[i] = metaData.getColumnName(i + 1);
return col_names;
...
ListgetFieldsPresentInResultSet(ResultSet rs)
get Fields Present In Result Set
List<String> fieldsPresentInResultSet = new ArrayList<String>();
ResultSetMetaData metaData = rs.getMetaData();
for (int index = 1; index <= metaData.getColumnCount(); index++) {
    fieldsPresentInResultSet.add(metaData.getColumnLabel(index).toLowerCase());
return fieldsPresentInResultSet;
intgetFirstGeometryFieldIndex(ResultSet resultSet)
Find the first geometry field name of a resultSet.
ResultSetMetaData meta = resultSet.getMetaData();
int columnCount = meta.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
    if (meta.getColumnTypeName(i).equalsIgnoreCase("geometry")) {
        return i;
return -1;
...
intgetFirstInt(ResultSet resultSet)
Returns int value of very first column in result set.
if (resultSet.next() == true) {
    return resultSet.getInt(1);
return -1;
FlagsgetFlags(ResultSet rs)
get Flags
Flags flags = new Flags();
for (int i = 0; i < attrArray.length; i++) {
    if ("Y".equals(rs.getString(attrArray[i]))) {
        flags.add(flagArray[i]);
return flags;
longgetGeneratedIdFromResultSet(ResultSet resultSet)
get Generated Id From Result Set
long orderId = -1;
while (resultSet != null && resultSet.next()) {
    orderId = resultSet.getLong(1);
return orderId;