PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
Home / PostgreSQL JDBC / How To Delete Data From A PostgreSQL Table Using JDBC

How To Delete Data From A PostgreSQL Table Using JDBC

Summary: in this tutorial, you will learn how to delete data from a table in the PostgreSQL database using JDBC.

To delete data from a Java program, you follow these steps:

  1. Establish a database connection.
  2. Create a PreparedStatement object.
  3. Execute a DELETE statement.
  4. Close the database connection.

Check it out how to connect to the PostgreSQL database to learn more about how to establish a database connection.

We will use the actor table in the sample database for the demonstration. The following program demonstrates how to delete a row in the actor table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.postgresqltutorial;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
/**
*
* @author postgresqltutorial.com
*/
public class Main {
 
    private final String url = "jdbc:postgresql://localhost/dvdrental";
    private final String user = "postgres";
    private final String password = "postgres";
 
    /**
     * Connect to the PostgreSQL database
     *
     * @return a Connection object
     */
    public Connection connect() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }
 
    /**
     * Delete actors by id
     *
     * @param id
     * @return
     */
    public int deleteActor(int id) {
        String SQL = "DELETE FROM actor WHERE actor_id = ?";
 
        int affectedrows = 0;
 
        try (Connection conn = connect();
                PreparedStatement pstmt = conn.prepareStatement(SQL)) {
 
            pstmt.setInt(1, id);
 
            affectedrows = pstmt.executeUpdate();
 
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return affectedrows;
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Main main = new Main();
        main.deleteActor(214);
 
    }
}

How it works.

The connect() method establishes a database connection to the dvdrental sample database. It returns a Connection object.

In the deleteActor() method:

  • First, prepare a DELETE statement that removes a row specified by its id from the actor table. We used the question mark (?) as the placeholder.
  • Next, establish a the database connection and create a PreparedStatement object in the try-with-resources statement.
  • Then, supply the id value in place of question mark placeholder (?) by calling the setInt() method.
  • After that, execute the UPDATE statement by calling the executeUpdate() method.
  • Finally, return the number of rows affected.

In the main method, we call the deleteActor() to delete actor with id 214.

Before running the program, let’s check the actor table to verify that the actor with id 214 exists.

1
2
3
4
5
6
7
8
SELECT
actor_id,
first_name,
last_name
FROM
actor
WHERE
actor_id = 214;

PostgreSQL JDBC Delete Example

Now, we run the delete program.

postgresql jdbc delete example

If we check the actor table again, the row with id 214 does not exist. It means that we have successfully deleted the actor with id 214 in the actor table.

In this tutorial, we have shown you step by step how to delete data from a table using JDBC.

Previous Tutorial: How To Update Data in PostgreSQL Database Using JDBC
Next Tutorial: The Beginner’s Guide To PostgreSQL JDBC Transaction

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Union
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Call PostgreSQL Stored Function Using JDBC
  • The Beginner's Guide To PostgreSQL JDBC Transaction
  • How To Delete Data From A PostgreSQL Table Using JDBC
  • How To Update Data in PostgreSQL Database Using JDBC
  • The Complete Guide to Insert Data Into a Table Using JDBC
  • The Ultimate Guide to Query Data from The PostgreSQL Using JDBC
  • Connecting To The PostgreSQL Database
  • PostgreSQL JDBC
  • PL/pgSQL Function That Returns A Table
  • Using PostgreSQL CAST To Convert From One Data Type Into Another

More Tutorials

  • PostgreSQL Functions
  • PostgreSQL JDBC

Site Info

  • Home
  • About Us
  • Contact Us
  • PostgreSQL Resources
  • Privacy Policy

Copyright © 2016 by PostgreSQL Tutorial Website. All Rights Reserved.