Share on Google+Share on Google+

JDBC: Drop Table Example

In this section, we will discuss how to drop table using JDBC.

JDBC: Drop Table Example

In this section, we will discuss how to drop table using JDBC.

Drop Table :

Dropping table means deleting all the rows of the table and deleting the schema of table  that is, you will loose all the saved records of table. You can easily delete table in MySql and also write query for dropping table. When you call drop table query, it throws an exception if table doesn't exist.

Before writing query for table drop we need to understand some terms which are important in database connection.

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:

  • host :The host name where MySQL server is running. Default is - 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 sql): 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 table, named student. First create connection to the database, using jdbc API. Write query of drop table 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 DropTable{
public static void main(String[] args){
System.out.println("Drop Table 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 {

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

//Dropping Table
String sql = "DROP TABLE student";
System.out.println("Table is deleted.");

} catch (SQLException e) {
System.out.println("Table doesn't exist.");
} catch (Exception e) {

Output :

Drop Table Example...
Table is deleted.

If you execute this code again you will get output -

Drop Table Example...
Table doesn't exist.



Posted on: September 25, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.


Advertisement null