2015-04-27 50 views
1

我有一個應用程序,我需要將數據庫備份爲.sql文件。我有以下的代碼,但它備份,.ZIP從H2數據庫備份爲.sql文件

public void backupDatabase(Connection conn) throws SQLException { 
     //to get absolute path 
     String path = appConfigReader.getDbAbsolutePath(); 
     Statement stmt = conn.createStatement(); 
     String sql = "BACKUP TO '"+ path+"myApp001.zip'"; 
     stmt.executeUpdate(sql); 
    } 

是否有.sql文件複製到我的服務器本身內的位置的任何方式。我知道有命令,但我需要Java代碼。有沒有辦法與 'org.h2.tools.RunScrip'或'Scrip'?或任何其他方法

回答

1

最後我得到了這樣的解決方案。其工作

/**Thank god, hopefully this will backup the database*/ 
    public void backupDatabase(Connection conn) throws SQLException { 
     LOGGER.info("Inside backupDatabase"); 
     //used to get DB location 
     String path = appConfigReader.getDbAbsolutePath(); 
     Statement stmt = conn.createStatement(); 
     //used to get DB url 
     String dburl = AppConfigReader.getInstance().getDatabaseUrl(); 
     String[] bkp = {"-url", dburl, "-user", "sa", "-password","sa", "-script", path+"/myApp001.sql"}; 
     Script.main(bkp); 
     LOGGER.info("backupDatabase method executed successfully"); 
    }