Monday, 28 July 2014

JDBC Drop Database Demo

Enjoying Java & Database Tutorials?

Subscribe to Ram N Java on YouTube for clear, beginner-friendly programming lessons!

🔔 Subscribe to Channel

How to Drop a Database Using JDBC in Java

Deleting an entire database is an administrative task that requires precision. In SQL, you remove a database using the DROP DATABASE statement. When automating database maintenance through Java, JDBC (Java Database Connectivity) allows you to execute this query programmatically.

The SQL Drop Command

The standard SQL command to remove a database is straightforward:

DROP DATABASE students;

Executing this query immediately removes the students database along with all its tables and records.

Step-by-Step Java Implementation

Here is the standard workflow to drop a database using JDBC:

  1. Register the Driver: Load the database driver using Class.forName().
  2. Establish a Connection: Connect to the database server using DriverManager.getConnection() without specifying a particular database name in the URL (connect at server level).
  3. Prepare the Statement: Create a PreparedStatement or Statement object containing the DROP DATABASE command.
  4. Execute the Query: Call executeUpdate() to send the command to the database engine.
  5. Clean Up Resources: Always close the statement and database connection in a finally block or use try-with-resources.

Watch More JDBC Tutorials


Click here to watch in Youtube : https://www.youtube.com/watch?v=_FC3b_sVGHQ

Click the below Image to Enlarge

JDBCDropDatabaseDemo Project Dir Structure
JDBC Drop Database Demo
JDBC Drop Database Demo

JDBCDropDatabase.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCDropDatabase
{
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL      = "jdbc:mysql://localhost:3306/";

    // Database credentials
    static final String USERNAME    = "root";
    static final String PASSWORD    = "root";

    public static void main( String[] args )
    {

        JDBCDropDatabase jdbcDropDatabase = new JDBCDropDatabase();
        jdbcDropDatabase.dropDatabase();

    }

    private void dropDatabase()
    {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try
        {
            /*
             * Register the JDBC driver in DriverManager
             */

            Class.forName(JDBC_DRIVER);

            /*
             * Establish connection to the Database using DriverManager
             */

            connection = DriverManager
                    .getConnection(DB_URL, USERNAME, PASSWORD);

            String sql = "DROP DATABASE STUDENTS";

            /*
             * Execute the query
             */
            preparedStatement = connection.prepareStatement(sql);
                        
            int returnValue = preparedStatement.executeUpdate();

            System.out.println("returnValue : " + returnValue);
            
            System.out.println("STUDENTS database has been Droped");

        }
        catch( SQLException se )
        {
            /*
             * Handle errors for JDBC
             */
            se.printStackTrace();
        }
        catch( ClassNotFoundException e )
        {
            /*
             * Handle errors for Class.forName
             */
            e.printStackTrace();
        }
        finally
        {
            /*
             * finally block used to close resources
             */
            try
            {
                if( preparedStatement != null )
                {
                    preparedStatement.close();
                }
            }
            catch( SQLException sqlException )
            {
                sqlException.printStackTrace();
            }
            try
            {
                if( connection != null )
                {
                    connection.close();
                }
            }
            catch( SQLException sqlException )
            {
                sqlException.printStackTrace();
            }
        }

    }
}

Output
returnValue : 0
STUDENTS database has been Droped

Environment Used 

JDK version : 1.7.0_51
Mysql Server version : 5.6.19 

To Download JDBCDropDatabaseDemoApp Project Click the below link

https://sites.google.com/site/javaee4321/jdbc/JDBCDropDatabaseDemoApp.zip?attredirects=0&d=1

See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment

    Tutorials