Java JDBC Statement run update statement

Description

Java JDBC Statement run update statement

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

public class Main {

   public static void performUpdate(Connection conn){
      String sql = "UPDATE RECIPES " +
                   "SET RECIPE_NUM = '1' " +
                   "WHERE RECIPE_NUM = '4'";
      Statement stmt = null;/* w  w  w. ja  v a 2  s. co m*/

      try {
          stmt = conn.createStatement();
          int result = stmt.executeUpdate(sql);
          if (result > 0){
              System.out.println("-- Record Updated --");
          } else {
              System.out.println("!! Record NOT Updated !!");
          }
      } catch (SQLException e) {
          e.printStackTrace();
      } finally {
          if (stmt != null) {
              try {
                  stmt.close();
              } catch (SQLException ex) {
                  ex.printStackTrace();
              }
          }
      }
      
  }
}



PreviousNext

Related