Ken Chan
Ken Chan

Reputation: 90457

How can I determine if the column name exist in the ResultSet?

As the ResultSet contains the data returned from the dynamic SQL, if there are any method to determine if the ResultSet contains a particular column name?

For example , if I run rs.getString("Column_ABC") but "Column_ABC" does not really exist, it will throw out the exception.

How can I test if the ResultSet can get a data from a column named "Column_ABC"?

Upvotes: 92

Views: 100463

Answers (8)

Nigel-Lee
Nigel-Lee

Reputation: 155

For anyone who's using kotlin, you can use runCatching to either return the value if the column exists or null if not.

Example (considering breadType is nullable):

val breadType = runCatching { rs.getString("bread_type") }.getOrNull()

Upvotes: 0

Harshal Shinde
Harshal Shinde

Reputation: 9

public static boolean hasColumn(ResultSet rs, String columnName) 
throws SQLException {
if(Objects.nonNull(((ResultSetImpl)resultSet).getColumnDefinition()) 
&& Objects.nonNull(((ResultSetImpl)resultSet).getColumnDefinition().getColumnLabelToIndex()) 
&& Objects.nonNull(((ResultSetImpl) resultSet).getColumnDefinition().getColumnLabelToIndex().get(columnName))) {
        return true;
    }
}
return false;

The Approved answer uses a for loop but do keep in mind that this code will be executed for each row fetched. That will be very time-consuming. I suggest doing it this way.

Upvotes: 0

Talick
Talick

Reputation: 348

private boolean isThere(ResultSet rs, String column){
    try{
        rs.findColumn(column);
        return true;
    } catch (SQLException sqlex){
        logger.debug("column doesn't exist {}", column);
    }

    return false;
}

Upvotes: 23

sanket patwardhan
sanket patwardhan

Reputation: 15

resultSet.getColumnMetaData().contains(columnName)

Upvotes: -3

VivekJ
VivekJ

Reputation: 49

/**
 * returns default value if column is not present in resultset
 * 
 * @param rs
 * @param columnLabel
 * @param defaultValue
 * @return
 */
@SuppressWarnings("unchecked")
private static <T> T getValueFromResultSet(final ResultSet rs,
        String columnLabel, T defaultValue) {
    try {
        return (T) rs.getObject(columnLabel);
    } catch (SQLException e) {
        return defaultValue;
    }
}

In java version >=7 you have a option of passing Class type in ResultSet#getObject method

Upvotes: -4

Zip184
Zip184

Reputation: 1890

Not sure if this is more or less efficient than Erick's answer but it's easier.

String str;

try {
    str = rs.getString(columnName);
} catch (java.sql.SQLException e) {
    str = null;
}

Upvotes: 8

Erick Robertson
Erick Robertson

Reputation: 33078

Use the ResultSetMetaData class.

public static boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int columns = rsmd.getColumnCount();
    for (int x = 1; x <= columns; x++) {
        if (columnName.equals(rsmd.getColumnName(x))) {
            return true;
        }
    }
    return false;
}

The thing I don't understand is why this function would ever be needed. The query or stored procedure being executed should have known results. The columns of the query should be known. Needing a function like this may be a sign that there is a design problem somewhere.

Upvotes: 149

Amit
Amit

Reputation: 7035

if not rs.getString("Column_ABC")= nothing then ' your code here

Upvotes: -21

Related Questions