SQL"SCRIPT"备份h2数据库的命令 [英] SQL "SCRIPT" command to backup h2 database

查看:129
本文介绍了SQL"SCRIPT"备份h2数据库的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有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));
}


如果要备份作为 ZIP存档,可以使用


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆