JDBC PreparedStatement example – Delete a record

Here’s an example to show you how to delete a record from a table via JDBC PreparedStatement. To issue a delete statement, calls the PreparedStatement.executeUpdate() method like this :


String deleteSQL = "DELETE DBUSER WHERE USER_ID = ?";
PreparedStatement preparedStatement = dbConnection.prepareStatement(deleteSQL);
preparedStatement.setInt(1, 1001);
// execute delete SQL stetement
preparedStatement.executeUpdate();

Full example…


package com.mkyong.jdbc;

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

public class JDBCPreparedStatementSelectExample {

	private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
	private static final String DB_USER = "user";
	private static final String DB_PASSWORD = "password";

	public static void main(String[] argv) {

		try {

			deleteRecordFromTable();

		} catch (SQLException e) {

			System.out.println(e.getMessage());

		}

	}

	private static void deleteRecordFromTable() throws SQLException {

		Connection dbConnection = null;
		PreparedStatement preparedStatement = null;

		String deleteSQL = "DELETE DBUSER WHERE USER_ID = ?";

		try {
			dbConnection = getDBConnection();
			preparedStatement = dbConnection.prepareStatement(deleteSQL);
			preparedStatement.setInt(1, 1001);

			// execute delete SQL stetement
			preparedStatement.executeUpdate();

			System.out.println("Record is deleted!");
			
		} catch (SQLException e) {

			System.out.println(e.getMessage());

		} finally {

			if (preparedStatement != null) {
				preparedStatement.close();
			}

			if (dbConnection != null) {
				dbConnection.close();
			}

		}

	}

	private static Connection getDBConnection() {

		Connection dbConnection = null;

		try {

			Class.forName(DB_DRIVER);

		} catch (ClassNotFoundException e) {

			System.out.println(e.getMessage());

		}

		try {

			dbConnection = DriverManager.getConnection(
                            DB_CONNECTION, DB_USER,DB_PASSWORD);
			return dbConnection;

		} catch (SQLException e) {

			System.out.println(e.getMessage());

		}

		return dbConnection;

	}

}

Result

A record with “user_id=1001″ is deleted from table.

About the Author

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

Comments

Leave a Reply

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

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

Ibosh
Guest
Ibosh

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