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.

Deleting a Table from Database

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.

Tutorials

  1. Change Column Name in MySQL
  2. Java Mysql Connection Example
  3. JDBC Example with MySQL
  4. Jdbc Mysql Connection String
  5. Jdbc Mysql Connection Url
  6. Getting Column Names from a database table in Java
  7. Count Rows from a Database Table
  8. JDBC Example with MySQL
  9. Write Text File to Table
  10. Inserting Image in Database Table
  11. Insert Data in Table Using Stored Procedure
  12. Copy One Database Table to Another
  13. Copy Table in a MySQL Database
  14. Set Timestamp by using the Prepared Statement
  15. Set Time by using the Prepared Statement
  16. Set Date by using the Prepared Statement
  17. Prepared Statement Set Big Decimal
  18. Set byte, short and long data types by using the Prepared Statement
  19. Set Data Types by using Prepared Statement
  20. Using the Prepared Statement Twice
  21. Deleting Records using the Prepared Statement
  22. Count Records using the Prepared Statement
  23. Inserting Records using the Prepared Statement
  24. Update Records using Prepared Statement
  25. Select Records Using Prepared Statement
  26. Prepared Statement With Batch Update
  27. Statement Batch Update
  28. Prepared Statement Set Object
  29. Cross Join Tables in a Specific Database
  30. Join tables with the NATURAL RIGHT JOIN operation
  31. Join tables with the NATURAL LEFT JOIN operation
  32. Join tables in the specific database
  33. Delete a Column from a Database Table
  34. Delete a Specific Row from a Database Table
  35. Deleting All Rows from a Database Table
  36. Sum of Column in a Database Table
  37. Arrange a Column of Database Table
  38. Arrange a Column of Database Table
  39. Remove Unique Column in Database Table
  40. Make Unique Column in Database Table