欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hsqldb的存储方式

程序员文章站 2022-05-24 18:13:51
...

Maven依赖

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.5.0</version>
</dependency>

一、内存(mem)存储方式

jdbc:hsqldb:mem:testdb

    @Test
    public void testMem() throws ClassNotFoundException, SQLException {
        Class.forName("org.hsqldb.jdbc.JDBCDriver");
//        String hsqldbMemUrl = "jdbc:hsqldb:mem:.";
        String url = "jdbc:hsqldb:mem:testdb";
        Connection connection = DriverManager.getConnection(url, "SA", "");
        Statement statement = connection.createStatement();
        statement.execute("CREATE TABLE student (id INTEGER, name VARCHAR(10))");
        statement.execute("INSERT INTO student VALUES (1, 'zhang1')");
        statement.execute("INSERT INTO student VALUES (2, 'zhang2')");

        ResultSet rs = statement.executeQuery("SELECT * FROM student");
        while (rs.next()) {
            int col1 = rs.getInt(1);
            String col2 = rs.getString(2);
            System.out.println(String.format("%s %s", col1, col2));
        }

        rs.close();
        statement.close();
        connection.close();
    }

二、文件(file)存储方式

jdbc:hsqldb:file:/home/zxm/hsqldb/testdb

    @Test
    public void testFile() throws ClassNotFoundException, SQLException {
        Class.forName("org.hsqldb.jdbc.JDBCDriver");
        String url = "jdbc:hsqldb:file:/home/zxm/hsqldb/testdb";
        Connection connection = DriverManager.getConnection(url, "SA", "");
        Statement statement = connection.createStatement();
        statement.execute("CREATE TABLE student (id INTEGER, name VARCHAR(10))");
        statement.execute("INSERT INTO student VALUES (1, 'zhang1')");
        statement.execute("INSERT INTO student VALUES (2, 'zhang2')");

        ResultSet rs = statement.executeQuery("SELECT * FROM student");
        while (rs.next()) {
            int col1 = rs.getInt(1);
            String col2 = rs.getString(2);
            System.out.println(String.format("%s %s", col1, col2));
        }

        rs.close();
        statement.close();
        connection.close();
    }

会在file:/home/zxm/hsqldb/testdb下保存数据

hsqldb的存储方式

三、资源(res)存储方式

jdbc:hsqldb:res:testdb

它mem与file方式的结合,是从classpath下读取数据

可以把file方式生成的testdb.properties和 testdb.script放在classpath下,用res方式可以读取到数据,数据库启动的时候会去这两个文件里读取数据进行初始化,但不会生成testdb.log和testdb.lck,以后进行的所有操作就都在内存里了,关闭数据库也不会写入testdb.script

    @Test
    public void testRes() throws ClassNotFoundException, SQLException {
        Class.forName("org.hsqldb.jdbc.JDBCDriver");
        String url = "jdbc:hsqldb:res:testdb";
        Connection connection = DriverManager.getConnection(url, "SA", "");
        Statement statement = connection.createStatement();
//        statement.execute("CREATE TABLE student (id INTEGER, name VARCHAR(10))");
//        statement.execute("INSERT INTO student VALUES (1, 'zhang1')");
//        statement.execute("INSERT INTO student VALUES (2, 'zhang2')");

        ResultSet rs = statement.executeQuery("SELECT * FROM student");
        while (rs.next()) {
            int col1 = rs.getInt(1);
            String col2 = rs.getString(2);
            System.out.println(String.format("%s %s", col1, col2));
        }

        rs.close();
        statement.close();
        connection.close();
    }

四、服务器模式

jdbc:hsqldb:hsql://localhost:9001/testdb

    public static void main(String[] args) {
        Server server = new Server();
        server.setDatabaseName(0, "testdb");
        server.setDatabasePath(0, "/home/zxm/hsqldb/testdb");
        server.setPort(9001);
        server.setSilent(true);
        server.setTrace(true);
        server.start();
    }

    @Test
    public void testHSQLConnect() throws ClassNotFoundException, SQLException {
        Class.forName("org.hsqldb.jdbc.JDBCDriver");
        String url = "jdbc:hsqldb:hsql://localhost:9001/testdb";
        Connection connection = DriverManager.getConnection(url, "SA", "");
        Statement statement = connection.createStatement();
        statement.execute("CREATE TABLE student (id INTEGER, name VARCHAR(10))");
        statement.execute("INSERT INTO student VALUES (1, 'zhang1')");
        statement.execute("INSERT INTO student VALUES (2, 'zhang2')");

        ResultSet rs = statement.executeQuery("SELECT * FROM student");
        while (rs.next()) {
            int col1 = rs.getInt(1);
            String col2 = rs.getString(2);
            System.out.println(String.format("%s %s", col1, col2));
        }

        rs.close();
        statement.close();
        connection.close();
    }

五、Web服务器模式

jdbc:hsqldb:http://localhost:9001/testdb

    public static void main(String[] args) {
        WebServer webServer = new WebServer();
        webServer.setDatabaseName(0, "testdb");
        webServer.setDatabasePath(0, "/home/zxm/hsqldb/testdb");
        webServer.setPort(9001);
        webServer.setSilent(true);
        webServer.setTrace(true);
        webServer.start();
    }

    @Test
    public void testHSQLConnect() throws ClassNotFoundException, SQLException {
        Class.forName("org.hsqldb.jdbc.JDBCDriver");
        String url = "jdbc:hsqldb:http://localhost:9001/testdb";
        Connection connection = DriverManager.getConnection(url, "SA", "");
        Statement statement = connection.createStatement();
        statement.execute("CREATE TABLE student (id INTEGER, name VARCHAR(10))");
        statement.execute("INSERT INTO student VALUES (1, 'zhang1')");
        statement.execute("INSERT INTO student VALUES (2, 'zhang2')");

        ResultSet rs = statement.executeQuery("SELECT * FROM student");
        while (rs.next()) {
            int col1 = rs.getInt(1);
            String col2 = rs.getString(2);
            System.out.println(String.format("%s %s", col1, col2));
        }

        rs.close();
        statement.close();
        connection.close();
    }

 

相关标签: hsqldb hsqldb