KEMBAR78
JDBC Drop Database | PDF | Databases | Sql
0% found this document useful (0 votes)
13 views8 pages

JDBC Drop Database

This document provides a guide on how to drop a database using JDBC in Java and the MySQLAdmin console. It outlines the necessary steps, including importing packages, opening a connection, executing a query, and cleaning up resources, along with example code for dropping databases. Additionally, it explains an alternative method using the MySQL command line interface to drop a database.

Uploaded by

zabronjoshua003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

JDBC Drop Database

This document provides a guide on how to drop a database using JDBC in Java and the MySQLAdmin console. It outlines the necessary steps, including importing packages, opening a connection, executing a query, and cleaning up resources, along with example code for dropping databases. Additionally, it explains an alternative method using the MySQL command line interface to drop a database.

Uploaded by

zabronjoshua003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Page 1 of 8

Home Whiteboard Online Compilers Practice Articles AI Assistant

Chapters Categories

SQL HTML CSS Javascript Python Java C C++ PHP Scala C#

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.
Page 2 of 8

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 −


Page 3 of 8

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);)


Page 4 of 8

{
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
-------------------------------------------
Page 5 of 8

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 Files\MySQL\MySQL Server 8.4\bin>

From this directory, type: following command

C:\Program Files\MySQL\MySQL Server 8.4\bin> mysqladmin -u root -p drop sample_db

 

Once you type this, you will be asked for password. Enter the password for user 'root'.
Page 6 of 8

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

TOP TUTORIALS

Python Tutorial

Java Tutorial
C++ Tutorial
C Programming Tutorial
Page 7 of 8

C# Tutorial
PHP Tutorial
R Tutorial

HTML Tutorial
CSS Tutorial
JavaScript Tutorial

SQL Tutorial

TRENDING TECHNOLOGIES

Cloud Computing Tutorial


Amazon Web Services Tutorial
Microsoft Azure Tutorial

Git Tutorial
Ethical Hacking Tutorial
Docker Tutorial

Kubernetes Tutorial
DSA Tutorial
Spring Boot Tutorial

SDLC Tutorial
Unix Tutorial

CERTIFICATIONS

Business Analytics Certification


Java & Spring Boot Advanced Certification

Data Science Advanced Certification


Cloud Computing And DevOps
Advanced Certification In Business Analytics

Artificial Intelligence And Machine Learning


DevOps Certification
Game Development Certification

Front-End Developer Certification


AWS Certification Training
Python Programming Certification

COMPILERS & EDITORS

Online Java Compiler


Page 8 of 8

Online Python Compiler


Online Go Compiler

Online C Compiler
Online C++ Compiler
Online C# Compiler

Online PHP Compiler


Online MATLAB Compiler
Online Bash Terminal

Online SQL Compiler


Online Html Editor

ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE |

PRIVACY POLICY | REFUND POLICY | COOKIES POLICY | FAQ'S

Tutorials Point is a leading Ed Tech company striving to provide the best learning material on
technical and non-technical subjects.

© Copyright 2025. All Rights Reserved.

You might also like