[英]Backup from H2 database as .sql file
我有一个应用程序,我需要将数据库备份为.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 ”来做到这一点? 或任何其他方法
终于我得到了这样的解决方案。 及其工作
/**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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.