Java JDBC PreparedStatement run delete statement

Description

Java JDBC PreparedStatement run delete statement

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Main {
   public static void deleteRecord(Connection conn){
       String sql = "DELETE FROM RECIPES WHERE " +
                    "RECIPE_NUM = ?";
       PreparedStatement pstmt = null;
       try{//w w w.  ja  va  2  s.c  om
           pstmt = conn.prepareStatement(sql);
           pstmt.setString(1, "1001");
           pstmt.executeUpdate();
           System.out.println("Recipe 1001 successfully deleted.");
       } catch (SQLException ex){
           ex.printStackTrace();
       } finally {
           if (pstmt != null){
               try {
                   pstmt.close();
               } catch (SQLException ex) {
                   ex.printStackTrace();
               }
           }
       }
       
   }
}



PreviousNext

Related