- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How to drop a table from JavaDB using JDBC?
- How to drop a MongoDB Collection using Java?
- Drop a MySQL Table after x hours?
- How to drop a table from Oracle database using JDBC API?
- How to drop a table from a database using JDBC API?
- How can we drop UNIQUE constraint from a MySQL table?
- Fix Drop table view #1051 unknown table error in MySQL
- How to drop an index in MongoDB using Java?
- How to drop a database in MongoDB using java?
- How to drop table in Android sqlite?
- How can I drop an existing column from a MySQL table?
- How to automate drag & drop functionality using Selenium WebDriver Java?
- Display MySQL table values using Java
- Does MySQL DROP TABLE completely remove the table or just the structure?
- MySQL appears to DROP USER but user still exists in MySQL.users table?

Advertisements