Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.apache.commons.io.FileUtils;
- import java.io.File;
- import java.io.IOException;
- public class DBtest{
- private static int cnt = 0;
- public static void main(String[] args) throws SQLException{
- System.out.print('u000C');
- String db = "test2";
- File file = new File(db);
- Connection conn = connectDB(db);
- close(conn);
- deleteDB(file);
- System.exit(0);
- }
- public static void close(Connection conn){
- try{
- if (conn != null) conn.close();
- System.out.println("Connection closed");
- }catch(SQLException e){
- System.out.println("connection NOT closed " + e);
- System.exit(0);
- }
- }
- public static void deleteDB(File file) {
- try{
- DriverManager.getConnection("jdbc:derby:;shutdown=true");
- }catch(SQLException e1){
- if(e1.getSQLState().equals("XJ015") && e1.getErrorCode() == 50000){
- try{
- FileUtils.deleteDirectory(file);
- }catch(IOException e2){
- System.out.println("FATAL ERROR: folder not deleted " + e2);
- System.exit(0);
- }
- System.out.println("Database shutdown successful");
- }else{
- System.out.println("FATAL ERROR: Database not shutdown " + e1);
- System.exit(0);
- }
- }
- }
- public static Connection connectDB(String database) {
- Connection conn = null;
- try{
- File db = new File(database);
- String URL = "jdbc:derby:" + database + ";create=true";
- System.out.println("db exists " + db.exists());
- conn = DriverManager.getConnection(URL);
- System.out.println("Succesfully connected to " + database);
- }catch(SQLException e){
- System.out.println("FATAL ERROR: from getDB " + e);
- System.exit(0);
- }
- return conn;
- }
- }
Add Comment
Please, Sign In to add comment