Main Tutorials

JDBC PreparedStatement – Delete a row

A JDBC PreparedStatement example to delete a row.

RowDelete.java

package com.mkyong.jdbc.preparestatement.row;

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

public class RowDelete {

    private static final String SQL_DELETE = "DELETE FROM EMPLOYEE WHERE NAME=?";

    public static void main(String[] args) {

        try (Connection conn = DriverManager.getConnection(
                "jdbc:postgresql://127.0.0.1:5432/test", "postgres", "password");
             PreparedStatement preparedStatement = conn.prepareStatement(SQL_DELETE)) {

            preparedStatement.setString(1, "mkyong");

            int row = preparedStatement.executeUpdate();

            // rows affected
            System.out.println(row);

        } catch (SQLException e) {
            System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

P.S Tested with PostgreSQL 11 and Java 8

pom.xml

	<dependency>
		<groupId>org.postgresql</groupId>
		<artifactId>postgresql</artifactId>
		<version>42.2.5</version>
	</dependency>

Download Source Code

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
10 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Pallavi Priyadarshini
6 years ago

The above delete query “DELETE DBUSER WHERE USER_ID = ?”; is incorrect because the syntax for delete is DELETE FROM table_name
WHERE condition;
Here, FROM keyword is missing.

vinay
2 years ago

try (Connection conn = DriverManager.getConnection(
“jdbc:postgresql://127.0.0.1:5432/test”, “postgres”, “password”);
PreparedStatement preparedStatement = conn.prepareStatement(“DELETE FROM USER WHERE NAME=? and Keys in (?)”) {
preparedStatement.setString(1, “name”);
preparedStatement.setString(2, “listofkeys”);
( listofkeys , i am sending like ‘AS’,’Asdf’)
int row = preparedStatement.executeUpdate();

unable to perform operation?

Ibosh
5 years ago

statement.setInt(1, 1001); what does this line?

roshan
6 years ago

I created a frontend for my school project using jdbc drivers,i have a issue with the delete button.When i enter nothing in the text field and press the delete button Iam not getting a popup menu as an exception.
the code is pasted below,please suggest changes :
private void billdeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
int id=Integer.parseInt(billidtext.getText());
try (//step2 create the connection object
Connection con = DriverManager.getConnection(“jdbc:oracle:thin:localhost:xe”,”hr”,”****”)) {
Statement stmt=con.createStatement();
stmt = con.createStatement();
String sql = “DELETE FROM bill ” +
“WHERE bid = (‘”+id+”‘)”;
int w=stmt.executeUpdate(sql);
if(w!=0)
JOptionPane.showMessageDialog(null,”Deleted Successfully!”); //this is displayed successfully
else
JOptionPane.showMessageDialog(null,”value does not exists!”);// this is displayed successfully
supplieridtext.setText(“”);

//view trigger
String sql1=”SELECT * FROM bill”;

stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(sql1);
//STEP 5: Extract data from result set
billtable.setModel(DbUtils.resultSetToTableModel(rs));
rs.close();

//step5 close the connection object
}

}catch( ClassNotFoundException | SQLException e){
JOptionPane.showMessageDialog(null,”no value entered!”);} //this line is not displayed when the text field is empty
}

Fiyaz
7 years ago

How about a Chinese character in Where condition Please provide solution of it…

Jim Brumbaugh
7 years ago

Great article, my question is: I need to add a confirm delete statement, where do I put it and what should it look like?

Allen Jackson
9 years ago

Should “DELETE DBUSER WHERE USER_ID = ?” be “DELETE FROM DBUSER WHERE USER_ID = ?” instead?

Pallavi Priyadarshini
6 years ago
Reply to  Allen Jackson

yes

x
9 years ago
Reply to  Allen Jackson

Thanks

Umair Aslam
11 years ago

this class should be named JDBCPreparedStatementDeleteExample