HSQLDB資料庫整理

FXBStudy發表於2016-07-14

一、簡介

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目錄下的批處理檔案)

2.1 執行DatabaseManager

<1>如果hsqldb.jar在當前資料夾下,則命令列啟動語句為:

//Swing版本的
java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing
<pre name="code" class="java">//Awt版本的
java -cp hsqldb.jar org.hsqldb.util.DatabaseManager 
/*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

3.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可以被用作測試資料或者作為應用程式的複雜的快取記憶體。因為只存在於記憶體中,所以其沒有任何檔案。

3.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.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執行模式

4.1 程式內(In-Process)模式(獨立模式)

In-Process模式又稱Standalone模式。這種模式下,資料庫引擎作為應用程式的一部分在同一個JVM中執行。對於一些應用程式來說, 這種模式因為資料不用轉換和通過網路的傳送而使得速度更快一些。其主要的缺點就是預設的不能從應用程式外連線到資料庫。所以當應用程式正在執行的時候,你不能使用類似於Database Manager的外部工具來檢視資料庫的內容。

file

此模式從應用程式啟動資料庫,由於所有的資料都將寫到檔案中,所以,即使程式退出,資料也不會被銷燬。

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

假如:資料庫名稱為——"testdb",並且位於當前目錄。

//連線資料庫
 Connection c = DriverManager.getConnection("jdbc:hsqldb:file:testdb", "SA", "");

如果不在當前路徑,可以採用相對路徑(relative paths)

 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中的路徑是不區分大小寫的,所以在第二次連線的時候是可以不一樣的,但是不建議。

mem

因為實在記憶體中執行,所以路徑名為簡單的mem。

//the database is called "mymemdb"
Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:mymemdb", "SA", "");

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.

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

4.2 Server 模式

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的別名,可在連線資料庫時使用。

HyperSQL HTTP Server

這種模式只能用在通過HTTP協議訪問資料庫伺服器主機,採用這種模式唯一的原因是客戶端或伺服器端的防火牆對資料庫對網路連線強加了限制。其他情況下,這種模式不推薦被使用。

執行web伺服器的時候,只要將剛才命令列中的主類(main class)替換成:org.hsqldb.WebServer

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文件[..\doc\apidocs\org\hsqldb\jdbc\jdbcConnection.html](位於HSQLDB釋出包中)。下面是一個簡單的例子,它採用hsqldb協議連線到本機的預設的9001埠。

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

 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", "");
/*
有些情況下,你也可以使用下面的程式碼來獲取驅動(driver)
Class.forName("org.hsqldb.jdbcDriver").newInstance();
可以注意到,在上面的連結中,沒有提到資料庫檔案,因為這些在Server執行時,資料庫檔案就已經被指定為dbname.0的值了。對於每個Server不只有一個資料庫例項的情況的連線URL,這裡暫時不詳解。
*

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

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

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

一個資料庫有許多可選的屬性,在章節中描述。可以在“連結”或建立資料庫的第一個連線的連線屬性中指定這些屬性的大部分屬性。檢視屬性章。

參考資料:

讚賞

相關文章