Home Tutorial Java Core JDBC: Drop Database Example

 
 

Share on Google+Share on Google+
JDBC: Drop Database Example
Posted on: September 21, 2012 at 12:00 AM
Advertisement
In this section, we are using JDBC API to drop database and describing it with example.

JDBC: Drop Database Example

In this section, we are using JDBC API to drop database and describing it with example.

Drop Database :

Database is an organized collection of data. It holds many tables required for your application. If you will drop your database you will loose all the tables too.
When you call drop database query, it throws an exception if database doesn't exist.

Connection: This  interface specifies connection with specific databases like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of this interface.

Class.forName(String driver): It loads the driver.

DriverManager  :  The DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property

getConnection(String url+dbName, String userName, String password): This method establishes a connection to specified database url.

The MySQL connection URL has the following format:
jdbc:mysql://[host][:port]/[database][?property1][=value1]...

  • host :The host name where MySQL server is running. Default is 127.0.0.1 - the IP address of localhost.
  • port : The port number where MySQL is listening for connection. Default is 3306.
  • dbName : The name of an existing database on MySQL server. If not specified, the connection starts no current database.
  • Property : The name of a supported connection properties. "userName" and "password" are 2 most important properties.

Statement: This interface executes the SQL statement and returns the result it produces.

createStatement(): It is a method of Connection interface which returns Statement object.

executeUpdate(String table): This method of Statement interface execute sql statements(insert or delete or update) which takes the sql query of string type and returns int.

Example :

In this example we are dropping database, named students. First create connection to the database, using jdbc API. Write query of drop database and call executeUpdate(sql)  method to execute the query.

package jdbc;

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

class DropDatabase {
public static void main(String[] args) {
System.out.println("Drop database Example...");
Connection con = null;
Statement statement = null;
String url = "jdbc:mysql://localhost:3306/";
//String dbName = "students";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driverName);

// Connecting to the database
con = DriverManager.getConnection(url + "students", userName, password);
try {
statement = con.createStatement();

//Dropping database
String sql = "DROP DATABASE students";
statement.executeUpdate(sql);
System.out.println("Database is deleted.");

} catch (SQLException e) {
System.out.println("doesn't exist.");
e.printStackTrace();
}
con.close();
} catch (Exception e) {
System.out.println("Database doesn't exist.");
}
}
}

Output :

Drop database Example...
Database is deleted.

If you run this code again then you will get output -

Drop database Example...
Database doesn't exist.
Advertisement

Related Tags for JDBC: Drop Database Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 21, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com