Update and Delete Operations Using JDBC in Java
Last Updated :
23 Jul, 2025
The UPDATE operation is used to modify an existing record in a table. It is done using the UPDATE SQL query, typically with a WHERE clause to specify which records to modify. The DELETE operation is used to remove one or more records from a table using the DELETE SQL Query.
Prerequisites
Create a simple register table
CREATE TABLE register (
name VARCHAR(100) NOT NULL,
email VARCHAR(100) PRIMARY KEY,
possword VARCHAR(100) NOT NULL,
gender VARCHAR(10),
city VARCHAR(100)
);
Update Operation in JDBC
It involves loading the JDBC driver, establishing a connection, preparing an SQL UPDATE query using PreparedStatement, setting parameters and executing the query with executeUpdate(). The method returns the number of rows affected and based on that, success or failure is determined. Proper exception handling and resource closing are also important.
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;
/**
* Hello world!
*
*/
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 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
It follows the same steps as other operations: load the JDBC driver, create a connection, prepare a DELETE SQL query using PreparedStatement, set any required parameters and execute it using executeUpdate(). The method returns how many rows were deleted and resources should be closed properly after the operation.
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.*;
/**
* Hello world!
*
*/
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:
outputExplanation:
- 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.
Similar Reads
PostgreSQL CRUD Operations using Java CRUD (Create, Read, Update, Delete) operations are the basic fundamentals and backbone of any SQL database system. CRUD is frequently used in database and database design cases. It simplifies security control by meeting a variety of access criteria. The CRUD acronym identifies all of the major funct
8 min read
Delete Contents From Table Using JDBC JDBC(Java Database Connectivity) is a standard API(application interface) between the java programming language and various databases like Oracle, SQL, PostgreSQL, etc. It connects the front end(for interacting with the users) with the backend(for storing data). Approach: 1. CREATE DATABASE: Create
2 min read
Performing Database Operations in Java | SQL CREATE, INSERT, UPDATE, DELETE and SELECT In this article, we will be learning about how to do basic database operations using JDBC (Java Database Connectivity) API in Java programming language. These basic operations are INSERT, SELECT, UPDATE, and DELETE statements in SQL language. Although the target database system is Oracle Database, t
6 min read
How to Execute SQL File with Java using File and IO Streams? In many cases, we often find the need to execute SQL commands on a database using JDBC to load raw data. While there are command-line or GUI interfaces provided by the database vendor, sometimes we may need to manage the database command execution using external software. In this article, we will le
5 min read
Inserting Records in Batch Using JDBC It is carried out using the functions namely addBatch() and executeBatch() methods. For that lets us do have a prior understanding of JDBC drivers. So, in order to connect to your database in Java, you need a JDBC driver. Every database (MySQL, Oracle, etc.) comes with its own JDBC driver, usually b
2 min read
Inserting Single and Multiple Records in MySQL using Java Java Database Connectivity is an API that helps us to connect a Java program to any SQL Database Instance. This connection gives power to developers to programmatically insert, delete, or update data from the database. JDBC contains in-built methods to run queries and updates on the database. In thi
6 min read