JDBC – Drop Database


JDBC – Drop Database



”;


This chapter provides examples on how to drop an existing Database using JDBC application and MySQLAdmin console. Before executing the following example, make sure you have the following in place −

  • To execute the following example you need to replace the username and password with your actual user name and password.

  • Your MySQL is up and running.

NOTE: This is a serious operation and you have to make a firm decision before proceeding to delete a database because everything you have in your database would be lost.

Required Steps

The following steps are required to create a new Database using JDBC application −

  • Import the packages − Requires that you include the packages containing the JDBC classes needed for database programming. Most often, using import java.sql.* will suffice.

  • Open a connection − Requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with a database server.

    Deleting a database does not require database name to be in your database URL. Following example would delete STUDENTS database.

  • Execute a query − Requires using an object of type Statement for building and submitting an SQL statement to delete the database.

  • Clean up the environment − try with resources automatically closes the resources.

Example: Dropping a Database

In this example, we”ve three static strings containing a dababase connection url, username, password. Now using DriverManager.getConnection() method, we”ve prepared a database connection. Once connection is prepared, we”ve created a Statement object using connection.createStatement() method. Then using statement.executeUpdate(), we”ve run the query to drop a database named Students and printed the success message.

In case of any exception while creating the database, a catch block handled SQLException and printed the stack trace.

Copy and paste the following example in JDBCExample.java, compile and run as follows −


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCExample {
   static final String DB_URL = "jdbc:mysql://localhost/";
   static final String USER = "guest";
   static final String PASS = "guest123";

   public static void main(String[] args) {
      // Open a connection
      try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
         Statement stmt = conn.createStatement();
      ) {		      
         String sql = "DROP DATABASE STUDENTS";
         stmt.executeUpdate(sql);
         System.out.println("Database dropped successfully...");   	  
      } catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}

Output

Now let us compile the above example as follows −


C:>javac JDBCExample.java
C:>

When you run JDBCExample, it produces the following result −


C:>java JDBCExample
Database dropped successfully...
C:>

In next example, we”re dropping a database TUTORIALSPOINT_COPY. Before deleting, we”re showing all available databases and then after deleting the same list is reprinted to check if database is dropped or not. See the example below using JDBC code:

Example: Dropping a Database

In this example, we”ve three static strings containing a dababase connection url, username, password. Now using DriverManager.getConnection() method, we”ve prepared a database connection. Once connection is prepared, we”ve created a Statement object using connection.createStatement() method.

The SQL command “SHOW DATABASES” is used to show list of available databases. Now using “DROP DATABASE TUTORIALSPOINT_COPY”, we”re dropping the TUTORIALSPOINT_COPY database. Then, again SQL query is issued “SHOW DATABASES”, to show the updated list of databases.

In case of any exception while creating the database, a catch block handled SQLException and printed the stack trace.

Copy and paste the following example in JDBCExample.java, compile and run as follows −


import java.sql.*;

// Drop database example
public class JDBCExample {

   static final String DB_URL = "jdbc:mysql://localhost/";
   static final String USER = "guest";
   static final String PASS = "guest123";

   public static void main(String args[]) {

      try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);) {		      
         System.out.println("Connected to database successfully...");

         Statement stmt = conn.createStatement();
         ResultSet rs1 = stmt.executeQuery("SHOW DATABASES");
         System.out.println("DATABASES");
         System.out.println("-------------------------------------------");
         while( rs1.next()){
            System.out.println(rs1.getString(1));
         }
         //Now DROP DATABASE
         int r = stmt.executeUpdate("DROP DATABASE TUTORIALSPOINT_COPY");

         System.out.println("TutorialsPoint_copy database successfully deleted. No. of tables removed = " + r);
         System.out.println("-------------------------------------------");
         ResultSet rs2 = stmt.executeQuery("SHOW DATABASES");
         System.out.println("DATABASES after DROP DATABASE has been called.");
         System.out.println("-------------------------------------------");
         while( rs2.next()){
            System.out.println(rs2.getString(1));
         }
      }catch(SQLException e){
         e.printStackTrace();
      }
   }
}

Output

Now let us compile the above example as follows −


C:>javac JDBCExample.java
C:>

When you run JDBCExample, it produces the following result −


C:>java JDBCExample
Connected to database successfully...
DATABASES
-------------------------------------------
information_schema
mysql
performance_schema
sample_db1
students
sys
tutorialspoint
tutorialspoint_copy
world

TutorialsPoint_copy database successfully deleted. No. of tables removed = 4
-------------------------------------------
DATABASES after DROP DATABASE has been called.
-------------------------------------------
information_schema
mysql
performance_schema
sample_db1
students
sys
tutorialspoint
world

C:>

Another way to delete a database is through mysqladmin console. From command prompt, go to the bin directory of MySQL installation directory. For example:


C:Program FilesMySQLMySQL Server 8.4bin>

From this directory, type: following command


C:Program FilesMySQLMySQL Server 8.4bin> mysqladmin -u root -p drop sample_db1

Once you type this, you will be asked for password. Enter the password for user ”root”.


Drop Database

After dropping, type SHOW DATABASES in MySQL prompt. The database removed will not show. See screenshot below:


Drop Database

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *