Delete record using Statement in Java

We saw how to Create a table and Insert Data using Statement in Java.

Now lets delete some of the data from the table using Statement.

Delete record SQL

Below is the query that can be fired directly on database to delete record.

Method from Statement Interface used

“Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.”

In case of Deleting records, executeUpdate(String sql) returns the total number of records that where deleted using the Delete SQL statement.

Now lets see the sample code for firing this SQL using JDBC

Example : Delete record using Statement in Java

Output


Now if we try to re-run the code, since no records will be deleted for the same criteria, the return value of executeUpdate(String sql) will be 0.

Since we have JDK 7, we are using Try-with-resources Statement to handle the resources that are opened.

Reference

1. Statement executeUpdate()

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn