30

Having rs, an instance of java.sql.ResultSet, how to check that it contains a column named "theColumn"?

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
Ivan
  • 63,011
  • 101
  • 250
  • 382
  • 2
    possible duplicate of [How can I determine if the column name exist in the ResultSet ?](http://stackoverflow.com/questions/3599861/how-can-i-determine-if-the-column-name-exist-in-the-resultset) – Riduidel Oct 15 '10 at 13:24
  • possible duplicate of [How do I check to see if a column name exists in a CachedRowSet?](http://stackoverflow.com/questions/462534/how-do-i-check-to-see-if-a-column-name-exists-in-a-cachedrowset) – bluish Apr 22 '15 at 08:55

5 Answers5

36

You can use ResultSetMetaData to iterate through the ResultSet columns and see if the column name matches your specified column name.

Example:

ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();

// get the column names; column indexes start from 1
for (int i = 1; i < numberOfColumns + 1; i++) {
    String columnName = rsMetaData.getColumnName(i);
    // Get the name of the column's table name
    if ("theColumn".equals(columnName)) {
        System.out.println("Bingo!");
    }
}
Lii
  • 11,553
  • 8
  • 64
  • 88
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 2
    @Ivan, be aware there are some complexities if you use an alias (SELECT col AS foo). http://bugs.mysql.com/bug.php?id=43684 – Joshua Martell Oct 15 '10 at 23:17
  • 2
    @JoshuaMartell, `ResultSetMetaData.getColumnLabel` can be used if interested in getting the name as defined by the `AS` clause of the SQL query – Santosh Feb 18 '15 at 10:48
19

Try using the method ResultSet#findColumn(String)

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;
}
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
  • 3
    It may be compared to a regular execution of Java. Bear in mind that usually bottleneck is not the Java code but the thins that are running on the other side -- the database. Most probably execution of the query and transport of data is few orders of magnitude slower than exception handling. – Boris Pavlović Oct 15 '10 at 14:47
  • 1
    Link gives 404. – Itération 122442 Feb 24 '20 at 08:47
3

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;
}
madx
  • 6,723
  • 4
  • 55
  • 59
3

You can do:

rs.findColumn("theColum")

and check for SQLException

Alois Cochard
  • 9,812
  • 2
  • 29
  • 30
  • Yeah, easiest way I would think is to just try to retrieve the column one way or another and catch the exception. – Jay Oct 15 '10 at 15:51
  • 4
    this is generally a bad solution since you're knowingly throwing an Exception for a common case, throwing Exceptions is for rare cases and is expensive in terms of resources – Martin Asenov Sep 29 '15 at 16:04
0

Use the ResultSetMetaData object provided by the ResultSet object via rs.getMetaData()

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
darri
  • 562
  • 1
  • 5
  • 10