SQL “SCRIPT” command to backup h2 database

自作多情 提交于 2020-01-11 07:01:07

问题


I have an application with h2 database. I want to create .sql file using SCRIPT command in Java.

If I am executing it using Prepared Statement:

PreparedStatement stmt = con.prepareStatement("SCRIPT");
ResultSet rs = stmt.executeQuery();

Then how can I able to get whole result in single String. I am new to Java so unable to find the way out to get result of that query because it doesn't contains column names in it.

Then I will write it in file using InputStream.


回答1:


If you want to backup into a file, the content of your H2 instance as a SQL script, you can directly use SCRIPT TO 'path/to/my/file.sql'.

try (Connection con = ...;
     Statement stmt = conn.createStatement()) {
    stmt.executeQuery(String.format("SCRIPT TO '%s'", sqlFilePath));
}

If you want to backup it as a ZIP archive, you can use BACKUP TO 'path/to/my/file.zip'.

try (Connection con = ...;
     Statement stmt = conn.createStatement()) {
    stmt.executeQuery(String.format("BACKUP TO '%s'", zipFilePath));
}


来源:https://stackoverflow.com/questions/40592133/sql-script-command-to-backup-h2-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!