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下保存数据
三、资源(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();
}
推荐阅读
-
代码-php通过ado方式连接access数据库的问题?
-
PHP调用MySQL的存储过程的实现代码
-
图的存储方式(邻接矩阵,邻接表,链式前向星)
-
自己收集比较强大的分页存储过程 推荐
-
线性表的链式存储结构:定义、单链表存储结构、给链表头结点分配空间、初始化链表数据、输出链表、在某个位置上插入数据、头插法、尾插法、删除某个位置上的数据、删除某个数据、删除整个链表计算链表的长度
-
php生成二维码的几种方式整理及使用实例_php技巧
-
修改mysql数据存储的地址_MySQL
-
引入css的四种方式_html/css_WEB-ITnose
-
七牛云存储 - 小白求助:关于百度ueditor编辑器 php版 文件保存位置的问题
-
HTML5中5大存储方式汇总