English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JDBC Delete Database

This chapter provides examples on how to use JDBC applications to delete existing databases. Before executing the following examples, please ensure that you have the following conditions:

  • To execute the following example, you need to replaceUsernameAndPasswordReplace it with the actual username and password.

  • Your MySQL or any database you are using has already been started and is running.

NOTE: This is a serious operation, you must make a firm decision before continuing to delete the database, because all the content in the database will be lost.

Necessary steps

To create a new database using a JDBC application, you need to perform the following steps-

  • Import package:You are required to include the software package that contains the JDBC classes required for database programming. Usually, useimport java.sql.*That's enough.

  • Register the JDBC driver: You are required to initialize the driver so that you can open the communication channel with the database.

  • You need to use the DriverManager.getConnection () method to create a Connection object that represents a physical connection to the database server.

    It is not necessary to include the database name in the database URL when deleting a database. The following example will deleteSTUDENTSDatabase.

  • Execute query:You need to use an object of the Statement type to generate and submit SQL statements to delete the database.

  • Clean up environment: All database resources must be explicitly closed instead of relying on JVM garbage collection.

Sample code

Copy and paste the following example into JDBCExample.java, compile and run as follows-

//Step1Import the required packages
import java.sql.*;
public class JDBCExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/";
   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";
   
   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //Step2Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
      //Step3Establish connection
      System.out.println("Connecting to a selected database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("Connected database successfully...");
      
      //Step4Execute query
      System.out.println("Deleting database...");
      stmt = conn.createStatement();
      
      String sql = "DROP DATABASE STUDENTS";
      stmt.executeUpdate(sql);
      System.out.println("Database deleted successfully...");
   catch(SQLException se){
      //Handling JDBC error
      se.printStackTrace();
   }
      //Handling Class.forName error
      e.printStackTrace();
   }
      //Used for closing resources
      try{
         if(stmt!=null)
            conn.close();
      catch(SQLException se){
      }// Nothing to do
      try{
         if(conn!=null)
            conn.close();
      catch(SQLException se){
         se.printStackTrace();
      }
   }//End try
   System.out.println("Goodbye!");
}//End main
}//End JDBCExample

Now, let's compile the above example as follows:

C:\>javac JDBCExample.java
C:\>

RuntimeJDBCExampleIt will produce the following result-

C:\>java JDBCExample
Connecting to a selected database...
Database connected successfully...
Deleting database...
Database deleted successfully...
Goodbye!
C:\>