List Columns in a ResultSet
1 min read

List Columns in a ResultSet

List Columns in a ResultSet

In order to list all columns in a JDBC ResultSet (e.g. for debug purposes), you need to iterate through its metadata:

// Get the metadata
ResultSetMetaData md = rs.getMetaData();

// For each column, list its name
for (int i=0;i < md.getColumnCount(); i++)
    logger.debug("Column :" + i +
      " = " + md.getColumnName(i + 1)
    );

Note: Column numbers start from 1, not 0 (hence the md.getColumnName(i + 1) above).

I usually wrap this in a if(logger.isTraceEnabled()) so it doesn't get triggered all the time.

HTH,