Skip to content

Instantly share code, notes, and snippets.

@prisoner
Created July 29, 2011 08:05
Show Gist options
  • Save prisoner/1113421 to your computer and use it in GitHub Desktop.
Save prisoner/1113421 to your computer and use it in GitHub Desktop.
The try-with-resources Statement
public static void viewTable(Connection con) throws SQLException {
String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";
try (Statement stmt = con.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + ", " + supplierID + ", " + price +
", " + sales + ", " + total);
}
} catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment