sql

Delete table row example

In this example we shall show you how to delete a table row in Java. We can use the Statement and PreparedStatement API to delete a row in a table, according to how frequently a delete query is executed. To delete a row into a table one should perform the following steps:

  • Load the JDBC driver, using the forName(String className) API method of the Class. In this example we use the MySQL JDBC driver.
  • Create a Connection to the database. Invoke the getConnection(String url, String user, String password) API method of the DriverManager to create the connection.
  • For deletes that are not executed frequently over the table use the Statement API. Create a Statement, using the createStatement() API method of the Connection. Execute the query, with the executeUpdate(String sql) API method. It returns the number of deleted rows.
  • For frequent deletes over the table use the PreparedStatement API. Create a PreparedStatement, using the prepareStatement(String sql) API method of the Connection, to create the query. Invoke the setString(int parameterIndex, String x) to set the parameters. Then invoke the executeUpdate() to execute the query,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class DeleteExample {
 
  public static void main(String[] args) {

    Connection connection = null;
    try {

  // Load the MySQL JDBC driver

  String driverName = "com.mysql.jdbc.Driver";

  Class.forName(driverName);


  // Create a connection to the database

  String serverName = "localhost";

  String schema = "test";

  String url = "jdbc:mysql://" + serverName +  "/" + schema;

  String username = "username";

  String password = "password";

  connection = DriverManager.getConnection(url, username, password);

  

  System.out.println("Successfully Connected to the database!");

  
    } catch (ClassNotFoundException e) {

  System.out.println("Could not find the database driver " + e.getMessage());
    } catch (SQLException e) {

  System.out.println("Could not connect to the database " + e.getMessage());
    }

    try {


/* 

 * For deletes that are not executed frequently we should use the statement API.

 * deleteCount contains the number of deleted rows

 */

Statement statement = connection.createStatement();

int deleteCount = statement.executeUpdate("DELETE FROM test_table WHERE test_col='test_value_1'");


System.out.println("Deleted test_value_1 row successfully : " + deleteCount);



/* 

 * For deletes that are executed frequently we should 

 * use the prepared statement API.

 * deleteCount contains the number of deleted rows

 */

PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM test_table WHERE test_col=?");

preparedStatement.setString(1, "test_value_2");

deleteCount = preparedStatement.executeUpdate();



System.out.println("Deleted test_value_2 row successfully : " + deleteCount);

    } catch (SQLException e) {

 System.out.println("Could not execute statement " + e.getMessage());
    }

  }
}

Example Output:

Successfully Connected to the database!
Deleted test_value_1 row successfully : 1
Deleted test_value_2 row successfully : 1

 
This was an example of how to delete a table row in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button