This is an example of how to use the ResultSet interface.
import java.sql.*; /** ResultSetExample.java This program demonstrates using a ResultSetMetaData and a ResultSet to display all the data in a table even though the program that gets the data does not know what the table is going to look like (the user passes in the values for the table and library). **/ public class ResultSetExample { public static void main(java.lang.String[] args) { if (args.length != 2) { System.out.println("Usage: java ResultSetExample <library> <table>"); System.out.println(" where <library> is the library that contains <table>"); System.exit(0); } Connection con = null; Statement s = null; ResultSet rs = null; ResultSetMetaData rsmd = null; try { // Get a database connection and prepare a statement. Class.forName("com.ibm.db2.jdbc.app.DB2Driver"); con = DriverManager.getConnection("jdbc:db2:*local"); s = con.createStatement(); rs = s.executeQuery("SELECT * FROM " + args[0] + "." + args[1]); rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); int rowCount = 0; while (rs.next()) { rowCount++; System.out.println("Data for row " + rowCount); for (int i = 1; i <= colCount; i++) System.out.println(" Row " + i + ": " + rs.getString(i)); } } catch (Exception e) { // Handle any errors. System.out.println("Oops... we have an error... "); e.printStackTrace(); } finally { // Ensure we always clean up. If the connection gets closed, the // statement under it closes as well. if (con != null) { try { con.close(); } catch (SQLException e) { System.out.println("Critical error - cannot close connection object"); } } } } }