2

Is it possible to check a ResultSet to see if it contains a column label for the current row. I would assume that it is backed by some kind of Map, so providing an rs.contains("label") method can't be that expensive, but I can't find one in the javadoc.

The current way I'm testing for the presence of a label is:

BigDecimal bid;
    try {
        bid = rs.getBigDecimal("bid");
    }
    catch(final SQLException e) {
        bid = null;
    }

But this seems untidy to me, and will be unreadable if you want to test multiple rows in this manner.

Richard
  • 9,972
  • 4
  • 26
  • 32
  • See related post: http://stackoverflow.com/questions/3942583/how-to-check-that-a-resultset-contains-a-specifically-named-field/3942652#3942652 – Buhake Sindi Nov 10 '10 at 09:31

3 Answers3

5

You can use metadata that you can obtain from resultset. Use rs.getMetaData(), and then on metadata there are getColumnName() and getColumnLabel() methods.

Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
2

You can use ResultSetMetaData (see my related post here). This is what I would prefer you to use. What you do is retrieve the ResultSetMetaData from the ResultSet, iterate through it and see if ResultSetMetaData.getColumnName(int index) returns the column name you've been looking for.

Alternatively, you can use ResultSet.findColumn() to find a column within a ResultSet.

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0

Why? Your query will already determine what columns are returned. But the ResultSetMetadata tells you what columns are present.

user207421
  • 305,947
  • 44
  • 307
  • 483