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 the Author

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.

Comments

avatar
7 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
IboshPallavi PriyadarshiniroshanFiyazJim Brumbaugh Recent comment authors
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… Read more »

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?

x
Guest
x

Thanks

Pallavi Priyadarshini
Guest
Pallavi Priyadarshini

yes

Umair Aslam
Guest
Umair Aslam

this class should be named JDBCPreparedStatementDeleteExample