Open In App

Update and Delete Operations Using JDBC in Java

Last Updated : 01 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The UPDATE operation modifies existing records in a table, usually with a WHERE clause to target specific rows. Without it, all records may be updated. The DELETE operation removes records from a table, also typically using a WHERE clause. Omitting it deletes all rows in the table.

Create a simple register table

Java
CREATE TABLE register (
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) PRIMARY KEY,
    password VARCHAR(100) NOT NULL,
    gender VARCHAR(10),
    city VARCHAR(100)
);

Update Operation in JDBC

Update Operation in JDBC involves loading the driver, connecting to the database, preparing an UPDATE query with PreparedStatement, setting parameters, and executing it using executeUpdate(). It returns the number of affected rows to determine success. Exception handling and resource cleanup are essential.

Syntax

UPDATE table_name SET column1 = value1 WHERE condition;

Implementation of the Update operation using JDBC

In JDBC, the Update operation is used to change current entries in a database table using SQL UPDATE statements.

Example: The following example updates the city column for a user in the register table based on their email ID.

App.java:

Java
import java.sql.*;
import javax.sound.midi.Soundbank;
public class App {
    public static void main(String[] args) throws Exception
    {

        // update operation

        String city = "noida";
        String email = "[email protected]";
        try {
            // load the driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            // use try with resource for automatically close jdbc resourse
            try (
                // create connection
                Connection con
                = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/jdbc_db",
                    "root", "password");
                // create statement
                PreparedStatement ps = con.prepareStatement(
                    "update register set city=? where email=?")) {
                // set the parameters
                ps.setString(1, city);
                ps.setString(2, email);
                // execute sql query
                int i = ps.executeUpdate();
                if (i > 0) {
                    System.out.println("success");
                }
                else {
                    System.out.println("fail");
                }
            }
        }
        catch (SQLException e) e.printStackTrace();
    }
    catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    }
}
}

Output:

update
update

Explanation

  • The above program updates a record in a MySQL table using JDBC.
  • A city value ("noida") is updated based on a matching email ("[email protected]").
  • The JDBC driver is loaded using Class.forName.
  • A database connection is established using DriverManager.getConnection.
  • An UPDATE SQL query with placeholders is prepared using PreparedStatement.
  • Parameters are set using setString() for city and the email, executeUpdate method is used to execute sql query, returning the affected row.
  • Print a message of success if the data is updated; otherwise, fail. Use try-with-resources to automatically close JDBC resources.
  • Exception handling is included for both SQL and class loading errors.

Delete Operation in JDBC

Delete Operation in JDBC follows the usual steps: load the driver, connect to the database, prepare a DELETE query with PreparedStatement, set parameters, and execute using executeUpdate(). It returns the number of rows deleted. Always close resources properly.

Syntax

DELETE FROM table_name WHERE condition;

Implementation of the Delete operation in JDBC

In JDBC, the Delete operation is used to delete current entries in a database table using SQL DELETE statements.

This example deletes a specific user from the register table based on their email.

App.java:

Java
import javax.sound.midi.Soundbank;
import java.sql.*;

public class App 
{
    public static void main( String[] args ) throws Exception {
        // Delete operation

        String email="[email protected]";
try{
    // load the driver
        Class.forName("com.mysql.cj.jdbc.Driver");
        
        // use try with resource for automatically close jdbc resourse
        try(
            
            // create connection
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","password");
        
        // create statement
        PreparedStatement ps= con.prepareStatement("Delete from register  where email=?")){
            
        // set the parameters
               ps.setString(1,email);
               
        //execute sql query
        int i= ps.executeUpdate();
    if(i>0) {
        System.out.println("Success");
    }else {
        System.out.println("Fail");
    }
}
}catch(SQLException e)
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
}
} 

Output:

delete
output

Explanation:

  • The program deletes a record from a MySQL table using JDBC.
  • The email value, like [email protected], is used to identify the record to be deleted.
  • The JDBC driver is loaded using Class.forName.
  • A database connection is established using DriverManager.getConnection.
  • A DELETE SQL query is prepared using a PreparedStatement.
  • The email value is set using setString() to replace the placeholder and the executeUpdate() method delete query and returns the number of rows affected.
  • Print a message of success if the data is deleted; otherwise, fail. Use try-with-resources to automatically close JDBC resources.

Article Tags :
Practice Tags :

Similar Reads