Deleting a Table from Database

Imagine a situation where we need to delete a table
from the database. We can do it very easily by using the commands in the MySQL
database. But how we can delete the table using java methods and API. In this section
we are describing, how to delete a table from database using java methods.
Java provides the facility for deleting a specific table from a given database
with the help of some specified methods. See detailed information given below:
Description of program:
Create a class DeleteTable inside which, firstly
establishes the connection
with MySQL database. After establishing the connection we will delete a table
from specific database. If the
table which we want to delete get deletes then we will print the message
"Table Deletion process is completely
successfully!", otherwise it will display " Table is not exists!".
Description of code:
DROP TABLE table_name:
Above code is used for deleting any table from a given database.
Here is the code of program:
import java.sql.*;
public class DeleteTable{
public static void main(String[] args) {
System.out.println("Tabel Deletion Example");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try{
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url+dbName, userName, password);
try{
Statement st = con.createStatement();
st.executeUpdate("DROP TABLE Employee1");
System.out.println("Table Deletion process is completly successfully!");
}
catch(SQLException s){
System.out.println("Table is not exists!");
}
con.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
|
Download this example.

|