How to Drop MySQL Table using Java?


Let us first create a table in a database. The query to create a table is as follows

mysql> create table customerDetails
   -> (
   -> CustomerId int,
   -> CustomerName varchar(30)
   -> );
Query OK, 0 rows affected (0.56 sec)

Now display all tables from the database in order to check the customerDetails table is present or not.

The query is as follows

mysql> show tables;

The following is the output

+------------------------------+
| Tables_in_test3              |
+------------------------------+
| bestdateformatdemo           |
| customerdetails              |
| deletedemo                   |
| differentdatetime            |
| expandedoutputdemo           |
| fieldlessthan5chars          |
| lastrecordbeforelastone      |
| mostrecentdatedemo           |
| nullcasedemo                 |
| order                        |
| orderbydatethentimedemo      |
| posts                        |
| productdemo                  |
| radiansdemo                  |
| selecttextafterlastslashdemo |
| siglequotesdemo              |
| studentinformation           |
| updatestringdemo             |
+------------------------------+
18 rows in set (0.00 sec)

Look at the sample output, we have ‘customerdetails’ table.

Here is the Java code to drop table. Our database is test3

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DropTableDemo {
   public static void main(String[] args) {
      Connection con = null;
      PreparedStatement ps = null;
      try {
         con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test3?useSSL=false", "root", "123456");
         ps = con.prepareStatement(
         String.format("DROP TABLE IF EXISTS %s", "customerdetails"));
         boolean result = ps.execute();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Now look at the database test3, to check the table ‘customerDetails’ is present or not, since we have deleted it above.

The query is as follows

mysql> show tables;

The following is the output

+------------------------------+
| Tables_in_test3              |
+------------------------------+
| bestdateformatdemo           |
| deletedemo                   |
| differentdatetime            |
| expandedoutputdemo           |
| fieldlessthan5chars          |
| lastrecordbeforelastone      |
| mostrecentdatedemo           |
| nullcasedemo                 |
| order                        |
| orderbydatethentimedemo      |
| posts                        |
| productdemo                  |
| radiansdemo                  |
| selecttextafterlastslashdemo |
| siglequotesdemo              |
| studentinformation           |
| updatestringdemo             |
+------------------------------+
17 rows in set (0.00 sec)

Yes, we have dropped the ‘customerDetails’ table successfully from database test3.

Updated on: 30-Jul-2019

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements