Java JDBC Statement close

Description

Java JDBC Statement close

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

public class Main {

   public static void queryDatabase(Connection conn) {
      String qry = "select recipe_num, name, description from recipes";
      Statement stmt = null;//from  w  ww .  j  a v  a 2  s .  c om

      try {
         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);
         }
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         if (stmt != null) {
            try {
               stmt.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
         }
         if (conn != null) {
            try {
               conn.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
         }
      }
   }
}



PreviousNext

Related