September 19, 2017

How To Check If Column Exists In ResultSet

Below APIs can be used for checking a column name exist is result set or not. Checking of column name can be used only in case if we are not sure about the existence of column in the resultset.In any other case, it always good to directly access.

/**
 * 
 * @param resultSet  
 * @param columnName Name of column to be checked
 * @return
 * @throws SQLException
 */
private boolean hasColumn(ResultSet resultSet, String columnName) throws SQLException {
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int coulmnCount = resultSetMetaData.getColumnCount();
for (int i = 1; i <= coulmnCount; i++) {
if (columnName.equals(resultSetMetaData.getColumnName(i))) {
return true;
}
}
return false;
}
Share:

0 comments:

Post a Comment