我有一个应用程序,我需要将数据库备份为.sql文件。我有以下代码,但它备份为.zip
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 ' ?或任何其他方法
答案 0 :(得分: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");
}