Deleting Data from MySQL Database using Java with JDBC


In this tutorial, we’ll explore how to delete data from a MySQL database using Java with JDBC (Java Database Connectivity). We’ll cover the process of establishing a database connection, preparing and executing SQL DELETE statements, and handling exceptions.


Deleting data from a database is a common operation in many applications. With JDBC, Java developers can easily perform delete operations on MySQL databases programmatically. This tutorial will guide you through the steps required to delete data from a MySQL database using Java.


  • Basic knowledge of the Java programming language.
  • Installed a MySQL Server.
  • MySQL Connector/J library added to your Java project.

Setting up the Database Connection:

First, we need to establish a connection to the MySQL database using the JDBC driver. The connection parameters such as URL, username, and password, must be provided.

String url = “jdbc:mysql://localhost:3306/your_database”;
String username = “your_username”;
String password = “your_password”;

Performing an SQL DELETE Operation:

To delete data from a MySQL table, we use an SQL DELETE statement. We’ll prepare a parameterized SQL statement with placeholders for the values to be deleted.

String sql = “DELETE FROM TableName WHERE condition_column = ?”;
int conditionValue = 10;

try (Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(sql)) {

pstmt.setInt(1, conditionValue);

int rowsAffected = pstmt.executeUpdate();
System.out.println(rowsAffected + ” row(s) deleted.”);

} catch (SQLException e) {
System.out.println(“Error: ” + e.getMessage());


In this tutorial, we’ve learned how to delete data from a MySQL database using Java with JDBC. By following similar steps, you can perform other CRUD operations such as INSERT, SELECT, and UPDATE. JDBC provides a powerful and flexible way to interact with databases from Java applications, making it a valuable tool for developers building database-driven applications.

Stay tuned for more tutorials on JDBC and database interaction in Java!


Leave a Reply

Your email address will not be published.Required fields are marked *