SQL"SCRIPT"备份h2数据库的命令 [英] SQL "SCRIPT" command to backup h2 database
问题描述
我有一个具有h2
数据库的应用程序.我想在Java中使用SCRIPT
命令创建.sql
文件.
I have an application with h2
database. I want to create .sql
file using SCRIPT
command in Java.
如果我使用Prepared Statement执行它:
If I am executing it using Prepared Statement:
PreparedStatement stmt = con.prepareStatement("SCRIPT");
ResultSet rs = stmt.executeQuery();
然后我如何才能在单个String中获得整个结果.我是Java的新手,因此无法找到获取该查询结果的出路,因为它不包含列名.
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.
然后我将使用InputStream
将其写入文件.
Then I will write it in file using InputStream
.
推荐答案
如果要备份到文件中,则将H2
实例的内容作为 SQL脚本,您可以直接使用 SCRIPT TO 'path/to/my/file.sql'
.
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));
}
这篇关于SQL"SCRIPT"备份h2数据库的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!