`
maimode
  • 浏览: 416350 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hsqldb常识

阅读更多

HSQLDB一大特色就是能够在内存中建立数据库,当然它也能将这些内存数据库保存到文件中以便实现真正的持久化。

 

先睹为快!

 

下面是一个In-Process方式访问内存数据库的代码示例:

 

下面代码需要引入hsqldb.jar包 (hsqldb-2.2.8)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {

	public static void main(String[] args) {
		try {
			//加载HSQLDB的JDBC驱动
			Class.forName("org.hsqldb.jdbcDriver");
			//在内存中建立数据库memdb,用户名为sa,密码为空
			Connection conn = DriverManager.getConnection("jdbc:hsqldb:mem:memdb","username","password");
			System.out.println("connect to memdb OK");
			
			Statement stat = conn.createStatement();
			//新建数据表
			stat.executeUpdate("create table person(NAME VARCHAR(20), AGE INTEGER)");
			System.out.println("create TABLE:person OK");
			
			//插入数据
			stat.executeUpdate("INSERT INTO person VALUES('张三丰',22)");
			stat.executeUpdate("INSERT INTO person VALUES('amos','25')");
			System.out.println("insert data into TABLE:person OK!");

			conn.close();
			
//			stat.execute("SHUTDOWN");
//			System.out.println("SHUTDOWN");
			
			Connection conn2 = DriverManager.getConnection("jdbc:hsqldb:mem:memdb","username","password");
			
			//查询数据
			PreparedStatement pstmt = conn2.prepareStatement("SELECT * FROM person");
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()) {
				String s = null;
				s = rs.getString(1) + "," + rs.getString(2);
				System.out.println(s);
			}
			System.out.println("select data OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 

 

 

The HSQLDB jar package is located in the /lib directory of the ZIP package and contains several components and programs.

 

• HyperSQL RDBMS Engine (HSQLDB)

• HyperSQL JDBC Driver

• Database Manager (GUI database access tool, with Swing and AWT versions)

• Sql Tool (command line database access tool)

 

Running Database Access Tools

 

java -cp ../lib/hsqldb.jar org.hsqldb.util.DatabaseManagerSwing

 

 

When a tool is up and running, you can connect to a database (may be a new database) and use SQL commands to access and modify the data.

 

 

A HyperSQL Database

 

Types of catalog data

• mem: stored entirely in RAM - without any persistence beyond the JVM process's life

• file: stored in filesystem files

• res: stored in a Java resource, such as a Jar and always read-only

 

 

 

 

In-Process Access to Database Catalogs

file:

 

Connection c = DriverManager.getConnection("jdbc:hsqldb:file:testdb", "SA", "");

 或者

 

Connection c = DriverManager.getConnection("jdbc:hsqldb:file:/opt/db/testdb", "SA", "");
 

mem:

 

Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:mymemdb", "SA", "");

 res:

 

Connection c = DriverManager.getConnection("jdbc:hsqldb:res:org.my.path.resdb", "SA", "");
 

 

The first time in-process connection is made to a database, some general data structures are initialised and a few helper threads are started. After this, creation of connections and calls to JDBC methods of the connections execute as if they are part of the Java application that is making the calls. When the SQL command "SHUTDOWN" is executed, the global structures and helper threads for the database are destroyed.

 

 

Note that only one Java process at a time can make in-process connections to a given file: database. However, if the file: database has been made read-only, or if connections are made to a res: database, then it is possible to make inprocess connections from multiple Java processes.

 

HyperSQL HSQL Server

 

java -cp ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:mydb --dbname.0 xdb
 

HyperSQL HTTP Server

 

This method of access is used when the computer hosting the database server is restricted to the HTTP protocol. The only reason for using this method of access is restrictions imposed by firewalls on the client or server machines and it should not be used where there are no such restrictions. The HyperSQL HTTP Server is a special web server that allows JDBC clients to connect via HTTP. The server can also act as a small general-purpose web server for static pages.

 

 

org.hsqldb.server.WebServer
 

HyperSQL HTTP Servlet

 

The Servlet class, in the HSQLDB jar, should be installed on the application server to provide the

connection. The database is specified using an application server property. Refer to the source file src/org/hsqldb/server/Servlet.java to see the details.

 

 

Both HTTP Server and Servlet modes can only be accessed using the JDBC driver at the client end. They do not provide a web front end to the database. The Servlet mode can serve only a single database.

 

Connecting to a Database Server

 

A common example is connection to the default port (9001) used for the hsql: protocol on the same machine:

 

try {
Class.forName("org.hsqldb.jdbc.JDBCDriver" );
} catch (Exception e) {
System.err.println("ERROR: failed to load HSQLDB JDBC driver.");
e.printStackTrace();
return;
}
Connection c = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb", "SA", "");

 

 If the HyperSQL HTTP server is used, the protocol is http: and the URL will be different:

 

Connection c = DriverManager.getConnection("jdbc:hsqldb:http://localhost/xdb", "SA", "");

 

Security Considerations

the password for the default system user should be changed from the default empty string 

 

 

HyperSQL provides two optional security mechanisms. The encrypted SSL protocol , and Access Control Lists .

Both mechanisms can be specified when running the Server or WebServer. On the client, the URL to connect to an SSL server is slightly different:

 

Java code to connect to the local secure SSL hsql and http Servers

 

Connection c = DriverManager.getConnection("jdbc:hsqldb:hsqls://localhost/xdb", "SA", "");
Connection c = DriverManager.getConnection("jdbc:hsqldb:https://localhost/xdb", "SA", "");

 

Accessing the Data

 

A connection should be reused as much as possible and closed only when it is not going to be used again for a long while.

 

A java.sql.DatabaseMetaData object is used to get metadata for the database.

 

 

A java.sql.CallableStatement object is used to execute an SQL CALL statement. The SQL CALL statement may contain parameters, which should be set to new values before each reuse.

 

A java.sql.Connection object also has some methods for transaction control.

 

Closing the Database

All databases running in different modes can be closed with the SHUTDOWN command, issued as an SQL statement.

 

 

A special form of closing the database is via the SHUTDOWN COMPACT command. This command rewrites the .data file that contains the information stored in CACHED tables and compacts it to its minimum size. This command should be issued periodically, especially when lots of inserts, updates or deletes have been performed on the cached tables. Changes to the structure of the database, such as dropping or modifying populated CACHED tables or indexes also create large amounts of unused file space that can be reclaimed using this command.

 

 

Databases are not closed when the last connection to the database is explicitly closed via JDBC. A connection property, shutdown=true, can be specified on the first connection to the database (the connection that opens the database) to force a shutdown when the last connection closes.

 

 

specifying a connection property to shutdown the database when the lastconnection is closed

 

Connection c = DriverManager.getConnection(
"jdbc:hsqldb:file:/opt/db/testdb;shutdown=true", "SA", "");
 

Creating a New Database

 

When a server instance is started, or when a connection is made to an in-process database, a new, empty database is created if no database exists at the given path.

If no username or password is specified, the default SA user and an empty password are used.

 

specifying a connection property to disallow creating a new database

 

Connection c = DriverManager.getConnection(
"jdbc:hsqldb:file:/opt/db/testdb;ifexists=true", "SA", "");
 

Creating and Accessing an Encrypted Database

 

First, a key must be created for the desired cipher and configuration. This is done by calling the function CRYPT_KEY(<cipher spec>, <provider>). If the default provider (the built-in JVM ciphers) is used, then NULL should be specified as the provider. The CRYPT_KEY function returns a hexadecimal key. The function call can be made in any HyperSQL database, so long as the provider class is on the classpath. This key can be used to create a new encrypted database. Calls to this function always return different keys, based on a generated random values.

 

As an example, a call to CRYPT_KEY('Blowfish', null) returned the string, '604a6105889da65326bf35790a923932'.

 

To create a new database, the URL below is used:

 

jdbc:hsqldb:file:<database
path>;crypt_key=604a6105889da65326bf35790a923932;crypt_type=blowfish
 

The third property name is crypt_provider. This is specified only when the provider is not the default provider.


 

HyperSQL works with any symmetric cipher that may be available from the JVM.

The files that are encrypted include the .script, .data, .backup and .log files. The .lobs file is not encrypted by default.

The property crypt_lobs=true must be specified to encrypt the .lobs file.

1
1
分享到:
评论
1 楼 mekingshow 2012-08-14  

相关推荐

    hsqldb使用(转载)

    HSQldb是一个轻量级的关系型数据库管理系统,特别适合用于系统演示、开发和测试环境。它的特点是快速、灵活且易于使用。HSQldb支持多种运行模式,满足不同场景的需求。 1. **数据库实例创建** 创建HSQldb数据库...

    HSQLDB

    **HSQLDB(HyperSQL Database)详解** HSQLDB,全称HyperSQL数据库管理系统,是一款开源、轻量级、高性能的关系型数据库系统。它支持Java平台,并且完全遵循SQL标准,提供内存和磁盘两种存储方式,使得它适用于多种...

    hsqldb-2.2.8数据库

    **HSQldb 2.2.8 数据库详解** HSQldb(HyperSQL Database)是一款高效、轻量级且开源的Java数据库管理系统,它在IT领域中被广泛应用于开发、测试以及小型应用环境。HSQldb完全用Java编写,因此具有良好的跨平台性,...

    hsqldb 2.25

    **HSQldb 2.25 知识点详解** HSQldb,全称为HyperSQL Database,是一款开源、轻量级、嵌入式的关系型数据库管理系统。它支持标准的SQL语法,包括SQL-92和SQL:2003,且在Java环境中运行,无需依赖外部操作系统服务。...

    HSQLDB快速连接数据库

    ### HSQLDB快速连接数据库 #### 一、HSQLDB简介与特点 HSQLDB(HyperSQL Database)是一款优秀的轻量级开源纯Java SQL数据库管理系统。它被设计为易于集成到现有的Java应用环境中,尤其适合那些对性能和资源消耗有...

    hsqldb-2.5.0.jar

    hsqldb数据库下载,很好用,简易的内存数据库,特别适合初学者。

    hsqldb的最新版本

    HSQldb,全称HyperSQL Database,是一款开源的、轻量级的关系型数据库管理系统,尤其适合于嵌入式应用和开发测试环境。HSQldb完全用Java编写,因此具有跨平台性,能在任何支持Java的环境中运行,包括Windows操作系统...

    hsqldb-lib.zip

    《HSQldb与Java数据库连接详解》 HSQldb(HyperSQL Database)是一款开源、轻量级、嵌入式的关系型数据库管理系统,广泛应用于Java应用程序中。它支持SQL标准,提供单用户和多用户模式,并且可以运行在内存中或磁盘...

    HSQLDB中文帮助文档

    ### HSQLDB中文帮助文档知识点总结 #### 一、HSQLDB概述 - **定义**:HSQLDB(HyperSQL Database)是一款轻量级、开源的纯Java SQL数据库管理系统。它能够作为嵌入式数据库使用,也可以作为一个独立的服务器运行。 ...

    hsqldb jdbc driver

    hsqldb jdbc driver适合于hsqldb

    hsqldb demo

    **HSQldb 概述** HSQldb,全称 HyperSQL Database,是一个开源、轻量级、完全Java编写的数据库管理系统。它支持多种数据库模式,包括纯内存储存、文件系统存储以及网络服务器模式,使其在多种场景下都能发挥效用。...

    hsqldb包和使用说明

    HSQldb是一个开源的、轻量级的、嵌入式的Java数据库引擎,常用于开发测试环境和小型应用程序。它的全称是HyperSQL Database,能够支持SQL标准,包括SQL:2011。HSQldb因其小巧、快速和易用的特点,在Java开发中尤其受...

    开源数据库软件hsqldb

    《开源数据库软件HSQldb深度解析》 HSQldb,全称HyperSQL Database,是一款完全开源、免费的Java实现的关系型数据库管理系统(RDBMS),它支持多种运行环境,包括独立服务器模式、嵌入式模式以及Web应用。HSQldb因...

    HSQLDB 1.8.0

    《HSQLDB 1.8.0:轻量级数据库引擎的深度剖析》 HSQLDB,全称为HyperSQL Database,是一款开源、轻量级、高性能的关系型数据库管理系统,广泛应用于嵌入式系统和测试环境。HSQLDB 1.8.0是该数据库引擎的一个重要...

    hsqldb-2.3.3.zip

    《HSQldb 2.3.3:轻量级数据库引擎深度解析》 HSQldb,全称为HyperSQL Database,是一款开源、纯Java语言编写的轻量级关系型数据库管理系统,广泛应用于测试环境、嵌入式系统以及小型应用中。HSQldb 2.3.3是其稳定...

    HSQLDB中文手册

    ### HSQLDB中文手册知识点概览 #### 一、HSQLDB概述与核心特性 HSQLDB,全称HyperSQL Database,是一款轻量级、开源的纯Java关系型数据库管理系统,以其高性能、高兼容性和易用性而著称。HSQLDB完全由Java语言编写...

    hsqldb相关几个文件

    标题 "hsqldb相关几个文件" 提到的是与HSQldb相关的压缩包资源,HSQldb是一个开源的关系型数据库管理系统,特别适用于Java应用程序。描述中提及的有两个zip文件:hsqldb_1_8_0_10.zip 和 hsqldb-mini.zip,以及一个...

Global site tag (gtag.js) - Google Analytics