java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to delete record from table?

In this example we are showing you how to delete a record from table in the database. We use a standard JDBC library for this purpose. For the database we use MySQL, you can use any type of database you want. All you need to do is to find the JDBC driver for the database, and find the way how to connect to it.

So here is the code example for deleting records from a table in database.

package com.javacoderanch.example.sql;

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

public class DeleteRecordExample
    public static void main(String[] args) throws Exception
        // This is our connection url to MySQL database, where jdbc is the prefix
        // for all jdbc connection. The mysql sub telling that we are using MySQL
        // database. Localhost is where our database is reside and sampledb is 
        // our database name.
        String url = "jdbc:mysql://localhost/sampledb";
        // To connect to a database we will need a user and password for the 
        // database server to allow us to manipulate its databse.
        String username = "root";
        String password = "";
        Connection connection = null;
            // Load the jdbc driver class
            // Then we ask a connection from the DriverManager by passing the 
            // connection URL and the password.
            connection = DriverManager.getConnection(url, username, password);
            // To delete records from tables we create an SQL delete command.
            // The question mark that we used in the where clause will be the
            // holder of value that will be assigned by PreparedStatement class.
            String sql = "DELETE FROM users WHERE user_id = ?";
            int userId = 2;
            // Create a statement object. We use PreparedStatement here.
            PreparedStatement statement = connection.prepareStatement(sql);

            // Pass a value of a userId that will tell the database which
            // records in the database to be deleted. Remember that when
            // using a statement object the indext parameter is start from
            // 1 not 0 as in the Java array data type index.
            statement.setInt(1, userId);
            // Tell the statement to execute the command. The executeUpdate()
            // method for a delete command returns number of records deleted
            // as the command executed in the database. If no records was
            // deleted it will simply return 0
            int rows = statement.executeUpdate();
            System.out.println(rows + " record(s) deleted.");
        } catch (Exception e)
        } finally 
            if (connection != null && !connection.isClosed())