How to delete data in a MySQL database with Java?

MySQLMySQLi Database

Delete data from a MySQL database with the help of DELETE command. The syntax is as follows.

delete from yourTableName where condition;

I will delete data from a MySQL database with the help of JAVA programming language. First, create a table and insert some records. The following is the query to create a table.

mysql> create table DeleteTableDemo
   -> (
   -> id int,
   -> Name varchar(200)
   -> );
Query OK, 0 rows affected (0.94 sec)

Insert records in the above table. The query to insert records is as follows.

mysql> insert into DeleteTableDemo values(101,'Smith');
Query OK, 1 row affected (0.21 sec)

mysql> insert into DeleteTableDemo values(102,'Johnson');
Query OK, 1 row affected (0.27 sec)

Now we can check how many records are in my table. The query is as follows.

mysql> select *from DeleteTableDemo;

The following is the output.

+------+---------+
| id   | Name    |
+------+---------+
|  101 | Smith   |
|  102 | Johnson |
+------+---------+
2 rows in set (0.00 sec)

We have two records in the table. Now, let us deleted data from a MySQL database table with the help of delete command. Here is the JAVA code that deletes the data with id=101. Before that, we will establish a Java Connection to our MySQL database.

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
public class JavaDeleteDemo {
   public static void main(String[] args) {
       Connection conn = null;
      Statement stmt = null;
       try {
          try {
             Class.forName("com.mysql.jdbc.Driver");
          } catch (Exception e) {
             System.out.println(e);
          }
          conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/business", "Manish", "123456");
          System.out.println("Connection is created successfully:");
          stmt = (Statement) conn.createStatement();
          String query1 = "delete from  DeleteTableDemo " +
          "where id=101";
          stmt.executeUpdate(query1);
          System.out.println("Record is deleted from the table successfully..................");
       } catch (SQLException excep) {
          excep.printStackTrace();
       } catch (Exception excep) {
          excep.printStackTrace();
       } finally {
          try {
             if (stmt != null)
             conn.close();
          } catch (SQLException se) {}
          try {
             if (conn != null)
             conn.close();
          } catch (SQLException se) {
             se.printStackTrace();
          }
       }
       System.out.println("Please check it in the MySQL Table. Record is now deleted.......");
    }
}

The following is the output.

mysql> select *from DeleteTableDemo;

The following is the output.

+------+---------+
| id   | Name |
+------+---------+
| 102  |Johnson |
+------+---------+
1 row in set (0.00 sec) We have deleted the data with id 101.
raja
Published on 13-Dec-2018 10:03:34
Advertisements