Last active
January 17, 2022 20:56
-
-
Save cwdesautels/11188409 to your computer and use it in GitHub Desktop.
Sample using H2's Backup and Restore tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package h2; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import org.h2.jdbcx.JdbcDataSource; | |
import org.h2.tools.Backup; | |
public class H2BackupTool | |
{ | |
public static void main(String[] args) | |
{ | |
Connection conn = null; | |
Statement stmt = null; | |
try | |
{ | |
JdbcDataSource datasource = new JdbcDataSource(); | |
datasource.setUrl("jdbc:h2:memFS:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"); | |
conn = datasource.getConnection("sa", "sa"); | |
stmt = conn.createStatement(); | |
stmt.execute("create schema test authorization sa"); | |
stmt.execute("create table test.a(id int primary key)"); | |
stmt.execute("insert into test.a (id) values (0)"); | |
stmt.execute("insert into test.a (id) values (1)"); | |
ResultSet rs = stmt.executeQuery("select * from test.a"); | |
System.out.print("Expected 2: "); | |
if (rs.next()) | |
{ | |
System.out.print(rs.getInt(1)); | |
while (rs.next()) | |
{ | |
System.out.println(", " + rs.getInt(1)); | |
} | |
} | |
stmt.close(); | |
conn.close(); | |
System.out.println("Backing up test"); | |
Backup.execute("C:\\h2\\db\\" + H2BackupTool.class.getSimpleName() + ".zip", "memFS:", "test", true); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
finally | |
{ | |
if (stmt != null) | |
{ | |
try | |
{ | |
stmt.close(); | |
} | |
catch (SQLException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
if (conn != null) | |
{ | |
try | |
{ | |
conn.close(); | |
} | |
catch (SQLException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package h2; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import org.h2.jdbcx.JdbcDataSource; | |
import org.h2.tools.Restore; | |
public class H2RestoreTool | |
{ | |
public static void main(String[] args) | |
{ | |
Connection conn = null; | |
Statement stmt = null; | |
try | |
{ | |
System.out.println("Restoring test"); | |
Restore.execute("C:\\h2\\db\\" + H2BackupTool.class.getSimpleName() + ".zip", "memFS:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", "test"); | |
JdbcDataSource datasource = new JdbcDataSource(); | |
datasource.setUrl("jdbc:h2:memFS:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"); | |
conn = datasource.getConnection("sa", "sa"); | |
stmt = conn.createStatement(); | |
ResultSet rs = stmt.executeQuery("select * from test.a"); | |
System.out.print("Expected 2: "); | |
if (rs.next()) | |
{ | |
System.out.print(rs.getInt(1)); | |
while (rs.next()) | |
{ | |
System.out.print(", " + rs.getInt(1)); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
finally | |
{ | |
if (stmt != null) | |
{ | |
try | |
{ | |
stmt.close(); | |
} | |
catch (SQLException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
if (conn != null) | |
{ | |
try | |
{ | |
conn.close(); | |
} | |
catch (SQLException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i'm running to following error.
the zip file has the correct statements in the file by using SCRIPT TO
it seems restore.execute is unable to read from "memfs:test" directory, any idea what might cause the problem?
environment
h2: v1.4.200
os: macOS
java: v1.8