
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to delete data in a MySQL database with Java?
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.
- Related Articles
- How to update data in a MySQL database with Java?
- How to insert data into a MySQL database with Java?
- Connecting to a MySQL database with Java
- Delete URLs with specific domains from MySQL database?
- How to use Boto3 to delete a database from AWS Data Catalog?
- Add data to existing data in a MySQL Database?
- How can you delete a table from a database in MySQL Python?
- How to delete everything in a MongoDB database?
- How to update data in a MySQL database without removing the old data?
- How can we delete a MySQL stored function from the database?
- How can we delete a MySQL database by using PHP script?
- How to insert raw data in MySQL database in Laravel?
- How to structure some data in a MySQL database for easier retrieval?
- How to use the name of the current database to delete it in MySQL?
- How to delete a table from MongoDB database?
