Java JDBC Statement create with try-with-resources

Description

Java JDBC Statement create with try-with-resources


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
   public static void queryDatabase() {
      String qry = "select recipe_num, name, description from recipes";

      try (Connection conn = getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(qry);) {

         while (rs.next()) {
            String recipe = rs.getString("RECIPE_NUM");
            String name = rs.getString("NAME");
            String desc = rs.getString("DESCRIPTION");

            System.out.println(recipe + "\t" + name + "\t" + desc);
         }//from  w  ww . j  av  a2 s.co m
      } catch (SQLException e) {
         e.printStackTrace();
      }
   }
}



PreviousNext

Related