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

10 comments on “JDBC PreparedStatement – Delete a row

  1. 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?

  2. 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.

  3. 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
    }

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

Leave a Comment

Your email address will not be published. Required fields are marked *