Skip to content

List Columns in a ResultSet

December 8th, 2016 - SoftwareTutorial(1 min)

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.


Share on

A little experiment: