5.2

Write a Java method using JDBC metadata features that takes a ResultSet as an input parameter and prints out the result in tabular form, with appropriate names as column headings.


printTable(ResultSet result) throws SQLException { 
    metadata = result.getMetaData();
    num_cols = metadata.getColumnCount();
    for (int i = 1; i <= num_cols; i++) {
        System.out.print(metadata.getColumnName(i) + '\t');
    }
    System.out.println();
    while (result.next()) {
        for (int i = 1; i <= num_cols; i++) { 
            System.out.print(result.getString(i) + '\t');
        }
        System.out.println();
    }
}