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文檔[..\doc\apidocs\org\hsqldb\jdbc\jdbcConnection.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", "");  

一個數據庫有許多可選的屬性,在章節中描述。可以在“鏈接”或創建數據庫的第一個連接的連接屬性中指定這些屬性的大部分屬性。查看屬性章。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM