try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM MyTable")
) {
while (rs.next()) {
int numColumns = rs.getMetaData().getColumnCount();
for (int i = 1; i <= numColumns; i++) {
// Column numbers start at 1. // Also there are many methods on the result set to return // the column as a particular type. Refer to the Sun documentation // for the list of valid conversions. System.out.println( "COLUMN " + i + " = " + rs.getObject(i));
}
}
}
try (PreparedStatement ps =
conn.prepareStatement("SELECT i.*, j.* FROM Omega i, Zappa j WHERE i.name = ? AND j.num = ?")
) {
// In the SQL statement being prepared, each question mark is a placeholder // that must be replaced with a value you provide through a "set" method invocation. // The following two method calls replace the two placeholders; the first is // replaced by a string value, and the second by an integer value. ps.setString(1, "Poor Yorick");
ps.setInt(2, 8008);
// The ResultSet, rs, conveys the result of executing the SQL statement. // Each time you call rs.next(), an internal row pointer, or cursor, // is advanced to the next row of the result. The cursor initially is // positioned before the first row. try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
int numColumns = rs.getMetaData().getColumnCount();
for (int i = 1; i <= numColumns; i++) {
// Column numbers start at 1. // Also there are many methods on the result set to return // the column as a particular type. Refer to the Sun documentation // for the list of valid conversions. System.out.println("COLUMN " + i + " = " + rs.getObject(i));
} // for } // while } // try } // try