HSQLDB(HyperSQL Database)初识

一、简介

HSQLDB(HyperSQL Database)是一个轻量级的纯Java开发的开放源代码的关系型数据库,其体积小,占用空间小,使用简单,支持内存运行方式等特点。

hsqldb-2.3.4下载地址:http://download.csdn.net/detail/fanxiaobin577328725/9564835

 

HSQLDB官网:http://www.hsqldb.org/

 

Hsqldb所涉及的一些文件。每个Hsqld数据库包含了2到5个命名相同但扩展名不同的文件,这些文件位于同一个目录下。

例如,名为"test"的数据库包含了以下几个文件:

  • test.properties——>properties文件描述了数据库的基本配置。
  • test.script——> script文件记录了表和其它数据库对象的定义,此外还有non-cached(无缓冲)表的数据。
  • test.log——>log文件记录了数据库最近所做的更新。
  • test.data——>data文件包含了cached(缓冲)表的数据。
  • test.backup——>backup文件是将data文件压缩备份,它包含了data文件上次的最终状态数据。

所有这些文件都是必不可少的,千万不可擅自删除。但如果你的数据库没有缓冲表(cached table),test.data和test.backup文件是不会存在。

 

hsqldb.jar包位于/lib目录下,它包含了一些组件和程序。每个程序需要不同的命令来运行。

hsqlddb.jar包中有下列这些组件:

  • HSQLDB RDBMS
  • HSQLDB JDBC Driver
  • Database Manager(Swing and AWT versions)
  • Transfer Tool(AWT versions)
  • Query Tool(AWT versions)
  • SQL Tool(command line)

其中HSQLDB RDBMS和JDBC Driver提供了HSQLDB的核心功能。其余组件都是通用的数据库工具。这些通用工具可以使用在任何带有JDBC驱动的数据库上。

二、运行工具

Hsqldb提供的主要的工具类:

  • org.hsqldb.util.DatabaseManager
  • org.hsqldb.util.DatabaseManagerSwing
  • org.hsqldb.util.Transfer
  • org.hsqldb.util.QueryTool

其中DatabaseManage和Sql Tool,只能用命令行参数来运行。你可以在命令行后面加上参数-?来查看这些工具可用的参数列表。
其他工具可以通过DatabaseManager的主界面启动,便于交互式操作。

注意:在这里我们强调一下hsqldb.jar的位置,因为所有启动命令都是参照hsqldb.jar的位置编写的。(可以参考/bin目录下的批处理文件)

运行DatabaseManager

<1>如果hsqldb.jar在当前文件夹下,则命令行启动语句为:

 

[java] view plain copy
 
  1. //Swing版本的  
  2. java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing  
  3. <pre name="code" class="java">//Awt版本的  
  4. java -cp hsqldb.jar org.hsqldb.util.DatabaseManager   
  5. /*Double clicking the HSQLDB jar will start the DatabaseManagerSwing application.*/  

 

三、HyperSQL Database类型

 

Each HyperSQL database is called a catalog. There are three types of catalog depending on how the data is stored.

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

<1>mem

All-in-memory,mem:catalogs can be used for test data or as sophisticated caches for an application. these databases do not have any files.

catalogs可以被用作测试数据或者作为应用程序的复杂的高速缓存。因为只存在于内存中,所以其没有任何文件。

<2>file

catalog consists of between 2 to 6 files, all named the same but with different extensions(扩展名), located in the same directory(目录).

For example, the database named "test" consists of the following files:

  • test.properties
  • test.script
  • test.log
  • test.data
  • test.backup
  • test.lobs

The properties file contains a few settings about the database.The script file contains the definition(定义) of tables and other database objects, plus the data for non-cached tables.The log file contains recent changes to the database.The data file contains the data for cached tables and the backup file is a compressed backup of the last known consistent state of the data file.

All these files are essential and should never be deleted. (以上文件是绝对不可以删除的)

For some catalogs, the test.data and test.backup files will not be present. In addition to those files, a HyperSQL database may link to any formatted text files, such as CSV lists, anywhere on the disk.

While the "test" catalog is open, a test.log file is used to write the changes made to data. This file is removed at a normal SHUTDOWN. Otherwise (with abnormal shutdown) this file is used at the next startup to redo the changes. A test.lck file is also used to record(记录) the fact that the database is open. This is deleted at a normal SHUTDOWN.

<3>res

catalog consists of the files for a small, read-only(只读) database that can be stored inside a Java resource such as a ZIP or JAR archive and distributed as part of a java application program.

四、Hsqldb运行模式

<1>进程内(In-Process)模式(独立模式)

In-Process模式又称Standalone模式。这种模式下,数据库引擎作为应用程序的一部分在同一个JVM中运行。对于一些应用程序来说, 这种模式因为数据不用转换和通过网络的传送而使得速度更快一些。其主要的缺点就是默认的不能从应用程序外连接到数据库。所以当应用程序正在运行的时候,你不能使用类似于Database Manager的外部工具来查看数据库的内容。

1.file

此模式从应用程序启动数据库,由于所有的数据都将写到文件中,所以,即使程序退出,数据也不会被销毁。

Access to an in-process database is started from JDBC, with the database path specified in the connection URL.

假如:数据库名称为——"testdb",并且位于当前目录。

 

[java] view plain copy
 
  1. //连接数据库  
  2.  Connection c = DriverManager.getConnection("jdbc:hsqldb:file:testdb", "SA", "");  

如果不在当前路径,可以采用相对路径(relative paths)

[java] view plain copy
 
  1. Connection c = DriverManager.getConnection("jdbc:hsqldb:file:/opt/db/testdb", "SA", "");  

注意:

 

  • Paths and database names for file databases are treated as case-sensitive(区分大小写) when the database is created or the first connection is made to the database.
  • Windows中的路径是不区分大小写的,所以在第二次连接的时候是可以不一样的,但是不建议。

2.mem

因为实在内存中运行,所以路径名为简单的mem。

 

[java] view plain copy
 
  1. //the database is called "mymemdb"  
  2. Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:mymemdb", "SA", "");  

 

3.res

As it is a Java resource, the database path is a Java URL (similar to the path to a class).

In the example below, "resdb" is the root name of the database files, which exists in the directory "org/my/path" within the classpath (probably in a Jar).

A Java resource is stored in a compressed format and is decompressed in memory when it is used. For this reason, a res: database should not contain large amounts of data and is always read-only.

 

[java] view plain copy
 
  1. Connection c = DriverManager.getConnection("jdbc:hsqldb:res:org.my.path.resdb", "SA", "");  

 

<2>Server 模式

1.HyperSQL HSQL Server

这种模式是server模式中的首选,其速度是最快的。它采用HSQLDB专有的通信协议。启动服务器需要编写批处理命令。Hsqldb提供的所有工具都能以java class归档文件(也就是jar)的标准方式运行。假如hsqldb.jar位于相对于当前路径的../lib下面。我们的命令将这样写:

  java -cp ../lib/hsqldb.jar org.hsqldb.Server -database.0 mydb -dbname.0 demoDB

现在你可能会疑惑,[-database.0 ]、 [dbname.0]为什么在后面加[0]。_... ...我们不是在前面说服务模式运行的时候可以指定10个数据库吗,如有多个数据库,则继续写命令行参数-database.1 aa -dbname.1 aa -database.2 bb-dbname.2 bb ... ...新建文本文件保存上面命令,文件名可以随意,将后缀名改成bat,然后直接执行批处理文件即可。在以后介绍的执行启动工具的命令采用同样方法。
上面启动服务器的命令启动了带有一个(默认为一个数据库)数据库的服务器,这个数据库是一个名为"mydb.*"文件,这些文件就是mydb.Properties、mydb.script、mydb.log等文件。其中demoDB是mydb的别名,可在连接数据库时使用。

2.HyperSQL HTTP Server

这种模式只能用在通过HTTP协议访问数据库服务器主机,采用这种模式唯一的原因是客户端或服务器端的防火墙对数据库对网络连接强加了限制。其他情况下,这种模式不推荐被使用。

运行web服务器的时候,只要将刚才命令行中的主类(main class)替换成:org.hsqldb.WebServer

3.HyperSQL HTTP Servlet

这种模式也是使用HTTP协议,当如Tomcat或Resin等servlet引擎(或应用服务器)提供数据库的访问时,可以使用这种模式。但是Servlet模式不能脱离servlet引擎独立启动。为了提供数据库的连接,必须将HSQLDB.jar中的hsqlServlet类放置在应用服务器的相应位置。

Web Server和Servlet模式都只能在客户端通过JDBC驱动来访问。Servlet模式只能启动一个单独的数据库。请注意做为应用程序服务器的数据库引擎通常不使用这种模式。

当HSQLDB Server运行时,客户端程序就可以通过hsqldb.jar中带有的HSQLDB JDBC DRRIVER连接数据库。如何连接服务器的详细说明可以参见jdbcConnection的Java文档[..docapidocsorghsqldbjdbcjdbcConnection.html](位于HSQLDB发布包中)。下面是一个简单的例子,它采用hsqldb协议连接到本机的默认的9001端口。

Example 1.1. Java code to connect to the local hsql Server

[java] view plain copy
 
  1.  try {  
  2.      Class.forName("org.hsqldb.jdbc.JDBCDriver" );  
  3.  } catch (Exception e) {  
  4.      System.err.println("ERROR: failed to load HSQLDB JDBC driver.");  
  5.      e.printStackTrace();  
  6.      return;  
  7.  }  
  8.   
  9.  Connection c = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb", "SA", "");  
  10. /*  
  11. 有些情况下,你也可以使用下面的代码来获取驱动(driver)  
  12. Class.forName("org.hsqldb.jdbcDriver").newInstance();  
  13. 可以注意到,在上面的链接中,没有提到数据库文件,因为这些在Server运行时,数据库文件就已经被指定为dbname.0的值了。对于每个Server不只有一个数据库实例的情况的连接URL,这里暂时不详解。  
  14. *  

Example 1.2. Java code to connect to the local http Server

[java] view plain copy
 
  1. Connection c = DriverManager.getConnection("jdbc:hsqldb:http://localhost/xdb", "SA", "");  

HSQLDB以Server模式运行的时候,网络访问应该受到充分的保护。源IP地址可能会因为TCP过滤、防火墙程序或者防火墙的使用,而受到限制。如果数据流要穿越一个不受保护的网络(比如Internet时),数据流应该被加密(比如采用VPN,SSH隧道或者TLS)。只有安全的密码才可以使用——最重要的是,应该将为系统默认用户设置的空字符串密码修改为安全密码。如果你想公开自己的数据的话,那么完全开放的网络连接应该限制为只有通过只读的账号来访问这些公开的数据。(比如,对于非机密数据或非特权用户可以考虑使用这种连接方式)。这些考虑因素也使用于采用HTTP协议运行HSQLDB服务器的情况下。

 

五、创建数据库

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.

当一个服务器实例启动时,或者当一个连接连接到In-process模式的数据库时,如果指定路径下没有此数据库,就会在指定路径下创建一个新的空数据库。

With HyperSQL 2.0 the username and password that are specified for the connection are used for the new database. Both the username and password are case-sensitive. (The exception is the default SA user, which is not case-sensitive). If no username or password is specified, the default SA user and an empty password are used.
如果没有指定数据库账号密码,默认情况下账号为SA(不区分大小写),密码为空。其他情况的时候账号和密码是区分大小写的。

This feature has a side effect that can confuse new users. If a mistake is made in specifying the path for connecting to an existing database, a connection is nevertheless established to a new database. For troubleshooting purposes, you can specify a connection property ifexists=true to allow connection to an existing database only and avoid creating a new database. In this case, if the database does not exist, the getConnection() method will throw an exception.

此功能有一个副作用,使用户很容易困惑。如果在指定连接到现有数据库的路径上有错误,则将其建立到一个新的数据库中。为排除故障,你可以指定一个连接属性ifexists=true来允许连接到现有的数据库,从而避免创建新数据库。在这种情况下,如果数据库不存在,该getconnection()方法将抛出一个异常。

Example 1.5. specifying a connection property to disallow creating a new database

 

[java] view plain copy
 
  1. Connection c = DriverManager.getConnection(  
  2.          "jdbc:hsqldb:file:/opt/db/testdb;ifexists=true", "SA", "");  

一个数据库有许多可选的属性,在章节中描述。可以在“链接”或创建数据库的第一个连接的连接属性中指定这些属性的大部分属性。查看属性章。

原文地址:https://www.cnblogs.com/ikei/p/7326750.html