Slide 1
Slide 2
Slide 3

Drop database from java programming (V88)

No Comments

How to Delete a Database
 
In SQL, DROP DATABASE is a Data Definition Language (DDL) command used to permanently delete an entire database, including all tables, views, stored procedures, functions, indexes, and data stored within it.

Example: Deleting a Database in Java


Source Code:

Copied!
    import java.sql.*;
import javax.swing.*;
public class DropTable extends javax.swing.JFrame {

    public DropTable() {
        initComponents();
        loadDatabases();  // Load databases when form opens
    }

    private void loadDatabases() {
        try {
            String url = "jdbc:mysql://localhost:3306/?useSSL=false&serverTimezone=Asia/Phnom_Penh";
            String user = "root"; // change your MySQL username
            String password = "root"; // change your MySQL password

            Connection conn = DriverManager.getConnection(url, user, password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SHOW DATABASES");

            comboDatabase.removeAllItems();
            while (rs.next()) {
                comboDatabase.addItem(rs.getString(1));
            }

            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error loading databases: " + e.getMessage());
        }
    }

    private void loadTables(String databaseName) {
        try {
            String url = "jdbc:mysql://localhost:3306/" + databaseName + "?useSSL=false&serverTimezone=Asia/Phnom_Penh";
            String user = "root";
            String password = "root";

            Connection conn = DriverManager.getConnection(url, user, password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SHOW TABLES");

            comboTable.removeAllItems();
            while (rs.next()) {
                comboTable.addItem(rs.getString(1));
            }

            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error loading tables: " + e.getMessage());
        }
    }

    private void deleteTable(String databaseName, String tableName) {
        try {
            String url = "jdbc:mysql://localhost:3306/" + databaseName + "?useSSL=false&serverTimezone=Asia/Phnom_Penh";
            String user = "root";
            String password = "root";

            Connection conn = DriverManager.getConnection(url, user, password);
            Statement stmt = conn.createStatement();

            stmt.executeUpdate("DROP TABLE IF EXISTS " + tableName);
            JOptionPane.showMessageDialog(this, "Table '" + tableName + "' deleted successfully!");

            stmt.close();
            conn.close();

            // Refresh table list
            loadTables(databaseName);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error deleting table: " + e.getMessage());
        }
    }

    private void btnDeleteTableActionPerformed(java.awt.event.ActionEvent evt) {                                               
        String db = (String) comboDatabase.getSelectedItem();
        String table = (String) comboTable.getSelectedItem();
        
        if (db == null || table == null) {
            JOptionPane.showMessageDialog(this, "Please select database and table!");
            return;
        }

        deleteTable(db, table);
    }     

    private void comboDatabaseActionPerformed(java.awt.event.ActionEvent evt) {
    String db = (String) comboDatabase.getSelectedItem();
    if (db != null) {
        loadTables(db);
    }
    }	                                                                                                                                                            

Result:









back to top