Recent Posts

Tuesday, 13 November 2018

JDBC - Delete a record in the table

     This tutorial provides an example on how to delete a record in the table using JDBC application.

Note
     Before going into the program, it is advised to go through JDBC Programming Basic Steps where the meaning of Connection, Statement etc. are discussed.
import java.sql.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class DeleteRecordDemo { 
   public static void main(String[] args) throws Exception {
      String sqlQuery = "delete from employees where ename='Vinod'"; 
      Class.forName("oracle.jdbc.OracleDriver"); 
      Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger"); 
      Statement st = con.createStatement();
      int updateCount = st.executeUpdate(sqlQuery);
      System.out.println("The number of rows deleted : "+ updateCount);
      con.close();
   }
}
Delete Multiple Records in the Table
import java.sql.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class DeleteRecordDemo { 
   public static void main(String[] args) throws Exception {
      Class.forName("oracle.jdbc.OracleDriver"); 
      Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger"); 
      Statement st = con.createStatement();
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter CutOff Salary : ");
      double cutOff = sc.nextDouble();
      String sqlQuery=String.format("delete from emp where esal >= %f", cutOff);
      int updateCount = st.executeUpdate(sqlQuery);
      System.out.println("The number of rows deleted : "+ updateCount);
      con.close();
   }
}

Next Tutorial : JDBC - Select records from a table

Previous Tutorial : JDBC - Update a record in the table

No comments:

Post a Comment