Java JDBC H2 Connection deleteDatabase()

Here you can find the source of deleteDatabase()

Description

delete Database

License

Open Source License

Declaration

public static void deleteDatabase() throws SQLException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    public static final String JDBC_URL = "jdbc:sqlite:C:/Users/timothyi/mta/database/development.sqlite3";

    public static void deleteDatabase() throws SQLException {
        if ("sqlite".equals(JDBC_URL.substring(5, 11))) {
            deleteDatabaseSQLITE();//  w w w .j  av  a2s .c o  m
        } else {
            deleteDatabaseH2();
        }
    }

    public static void deleteDatabaseSQLITE() throws SQLException {
        File f = new File(JDBC_URL.substring(12));
        f.delete();
    }

    public static void deleteDatabaseH2() throws SQLException {
        Connection conn = DriverManager.getConnection(JDBC_URL, "sa", "");

        Statement stmt = conn.createStatement();

        try {
            stmt.execute("DROP ALL OBJECTS DELETE FILES");
            // stmt.execute("SHUTDOWN"); TODO: db files get removed if this line
            // is uncommented
        } finally {
            stmt.close();
            conn.close();
        }
    }
}

Related

  1. createDBWithTB(String db)
  2. deleteDatabaseH2()
  3. getConnection()
  4. getH2Connection(String dbname)
  5. getH2Connection(String filename)