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

author image

mkyong

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. Read all published posts by

Comments

avatar
3000
newest oldest most voted
Pallavi Priyadarshini
Guest
Pallavi Priyadarshini

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.

Ibosh
Guest
Ibosh

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

roshan
Guest
roshan

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
Guest
Fiyaz

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

Jim Brumbaugh
Guest
Jim Brumbaugh

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
Guest
Allen Jackson

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

Pallavi Priyadarshini
Guest
Pallavi Priyadarshini

yes

x
Guest
x

Thanks

Umair Aslam
Guest
Umair Aslam

this class should be named JDBCPreparedStatementDeleteExample