01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.samples;
07:
08: import java.sql.Connection;
09: import java.sql.DriverManager;
10: import java.sql.SQLException;
11: import java.sql.Statement;
12:
13: import org.h2.tools.Script;
14: import org.h2.tools.DeleteDbFiles;
15: import org.h2.tools.RunScript;
16:
17: /**
18: * This sample application shows how to compact the database files.
19: * This is done by creating a SQL script, and then re-creating the database
20: * using this script.
21: */
22: public class Compact {
23:
24: public static void main(String[] args) throws Exception {
25: DeleteDbFiles.execute("data", "test", true);
26: Class.forName("org.h2.Driver");
27: Connection conn = DriverManager.getConnection(
28: "jdbc:h2:data/test", "sa", "");
29: Statement stat = conn.createStatement();
30: stat
31: .execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
32: stat
33: .execute("INSERT INTO TEST VALUES(1, 'Hello'), (2, 'World');");
34: conn.close();
35: System.out.println("Compacting...");
36: compact("data", "test", "sa", "");
37: System.out.println("Done.");
38: }
39:
40: /**
41: * Utility method to compact a database.
42: *
43: * @param dir the directory
44: * @param dbName the database name
45: * @param user the user name
46: * @param password the password
47: */
48: public static void compact(String dir, String dbName, String user,
49: String password) throws SQLException {
50: String url = "jdbc:h2:" + dir + "/" + dbName;
51: String file = "data/test.sql";
52: Script.execute(url, user, password, file);
53: DeleteDbFiles.execute(dir, dbName, true);
54: RunScript.execute(url, user, password, file, null, false);
55: }
56: }
|