JDBC Delete Example | JDBC Delete Program | executeUpdate() JDBC

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorials, we are going to understand the JDBC delete program example. To do the delete data from database in JDBC, the API has given us two different methods executeUpdate(String qry) and execute(String qry).
By using the any one of those two, we can do the jdbc delete program in java. Here is the difference between the Jdbc executeUpdate() and execute().
executepdate() method returns the integer value. The value represents that the number of rows effected in the database.
int rowsEffected = stmt.executepUpdate("delete command");
execute() method returns boolean value. As we already discussed in the JDBC select example, we can use the execute() method for both select and non-select (insert, update, delete) operations. If the resultant object contains ResultSet then the execute() method returns the true, it returns false if it is an update count or no records found.
boolean isResultSet = stmt.executep("update command");
Here is the complete example for JDBC Delete.
package com.onlinetutorialspoint.jdbc; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.Statement; 
import java.util.Scanner; 
 
public class Jdbc_DeleteOperation_Example { 
 
    public static void main(String[] args) throws Exception { 
 
        Scanner scanner = new Scanner(System.in); 
        System.out.println("Enter Student Number to delete the record"); 
 
        int studentNo = scanner.nextInt(); 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlinetutorialspoint", "root", "systemuser23!"); 
        con.setAutoCommit(false); 
        Statement stmt = con.createStatement(); 
        String query = "delete from student where sid='" + studentNo + "'"; 
        int result = stmt.executeUpdate(query); 
        con.commit(); 
        if (result == 0) { 
            System.out.println("record not found to delete"); 
        } else { 
            System.out.println(result+" no.of record(s) found and deleted"); 
        } 
        stmt.close(); 
        con.close(); 
 
    } 
 
}
Enter Student Number to delete the record
2002
1  record(s) found and deleted
Happy Learning 🙂

HQL update, delete Query Example
JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC PreparedStatement Example Program
Step by Step JDBC Program Example
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file

HQL update, delete Query Example
JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC PreparedStatement Example Program
Step by Step JDBC Program Example
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file
 


Δ
 JDBC Driver Types
 Step by Step JDBC Program
 JDBC Select Program
 JDBC Insert Program
 JDBC Update Program
 JDBC Delete Program
 JDBC Connection – Properties File
 JDBC PreparedStatement Program
 JDBC – CallableStatement Example
 JDBC – Read an Image from DB
 JDBC – Insert an Image in DB
 JDBC – Updatable ResultSet
 JDBC – Scrollable ResultSet
 JDBC – ResultSetMetaData
 JDBC – DatabaseMetaData
 JDBC – Transaction Management
 JDBC – Batch Processing
 JDBC Interview Questions