JDBC Statement example to Drop a table

Here is an example to show you how to drop a table from database via JDBC statement.
To issue a drop statement, calls the Statement.executeUpdate() method as shown below :
Statement statement = connection.createStatement(); // execute delete SQL stetement statement.executeUpdate(dropTableQuery);
Full example…
File: JDBCStatementDropExample.java
package com.simplecode.jdbc;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
public class JDBCStatementDropExample
{
private static final String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String serverName = "127.0.0.1";
private static String portNumber = "1521";
private static String sid = "XE";
private static final String dbUrl ="jdbc:oracle:thin:@"+serverName+":"+ portNumber+":"+sid;
private static final String dbUser = "system";
private static final String dbPassword = "admin";
public static void main(String[] argc)
{
try
{
dropTable();
}
catch (SQLException e)
{
System.err.println(e.getMessage());
}
}
private static void dropTable() throws SQLException
{
Connection connection = null;
Statement statement = null;
String dropTableQuery = "DROP TABLE EMPLOYEE";
try
{
connection = getDBConnection();
statement = connection.createStatement();
// execute delete SQL statement
statement.executeUpdate(dropTableQuery);
System.out.println("EMPLOYEE table is deleted from database!");
}
catch (SQLException e)
{
System.err.println(e.getMessage());
}
finally
{
if (statement != null)
{
statement.close();
}
if (connection != null)
{
connection.close();
}
}
}
private static Connection getDBConnection()
{
Connection dbConnection = null;
try
{
Class.forName(dbDriver);
}
catch (ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
try
{
dbConnection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return dbConnection;
}
catch (SQLException e)
{
System.err.println(e.getMessage());
}
return dbConnection;
}
}



