HSQLDB教程

HSQLDB (HyperSQL DataBase) is a relational database software written in Java. HSQLDB is very easy to use and requires very less memory. HSQLDB provides multithreading support and it’s a great choice to easily test your application against a database.

HSQLDB(HyperSQL数据库)是用Java编写的关系数据库软件。 HSQLDB非常易于使用,所需的内存也很少。 HSQLDB提供多线程支持,这是轻松针对数据库测试应用程序的绝佳选择。

HSQLDB安装 (HSQLDB Installation)

Let’s see how we can install HSQLDB easily.

让我们看看如何轻松安装HSQLDB。

  1. Download the HyperSQL Database Engine from SourceForge. Unzip to your favorite location after downloading the zip file.

    SourceForge下载HyperSQL数据库引擎。 下载zip文件后,将其解压缩到您喜欢的位置。
  2. HyperSQL Database latest version is 2.4.1 and it supports Java 8, Java 9 and Java 10. If you have required java version installed, then you are ready to start the database.

    HyperSQL Database的最新版本是2.4.1,它支持Java 8Java 9Java 10 。 如果已安装所需的Java版本,则可以启动数据库了。

启动HSQLDB数据库 (Starting HSQLDB Database)

Below command will start the HSQLDB database instance.

下面的命令将启动HSQLDB数据库实例。

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

Above command will run HSQLDB in server mode, we are providing the location where database script file will be created. dbname.0 specifies the public name of the database and it’s used by client applications to connect to this database instance.

上面的命令将在服务器模式下运行HSQLDB,我们提供了创建数据库脚本文件的位置。 dbname.0指定数据库的公用名,客户端应用程序使用它来连接到该数据库实例。

Below image shows the console output produced by the command, notice that terminal will remain open after database server is started. If you want HSQLDB to work in the background, then add & to the above command.

下图显示了该命令产生的控制台输出,请注意,启动数据库服务器后,终端将保持打开状态。 如果您希望HSQLDB在后台运行,请在上述命令中添加&

HSQL数据库管理器 (HSQL Database Manager)

HSQL Database manager provides a GUI to connect to HSQL database instances. It allows us to run some useful commands, such as populating test data, shutting down the server etc. We can use below command to launch the HSQL Database Manager Swing application.

HSQL数据库管理器提供了一个GUI以连接到HSQL数据库实例。 它允许我们运行一些有用的命令,例如填充测试数据,关闭服务器等。我们可以使用以下命令来启动HSQL Database Manager Swing应用程序。

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

When the application launches, a popup window opens to enter the database configuration details to connect. Provide the required information as shown in below image and connect to the database instance.

应用程序启动时,将弹出一个窗口,以输入要连接的数据库配置详细信息。 提供所需的信息,如下图所示,并连接到数据库实例。

HSQLDB插入测试数据 (HSQLDB Insert Test Data)

We can use HSQL Database Manager to populate test data, as shown in the below image.

我们可以使用HSQL数据库管理器来填充测试数据,如下图所示。

This will create few tables and insert some values into it, as shown in the below image.

这将创建一些表并将一些值插入其中,如下图所示。

HSQLDB关闭服务器 (HSQLDB Shutdown Server)

We can quit the terminal to abruptly shut down the HSQLDB server. When we will start the server next time, it will pick where it has been terminated. However, we can use HSQL Database Manager to shut down the server gracefully.

我们可以退出终端以突然关闭HSQLDB服务器。 下次启动服务器时,它将选择终止的位置。 但是,我们可以使用HSQL数据库管理器正常关闭服务器。

Once “SHUTDOWN” command is executed, it will produce the following screen.

HSQLDB shutdown complete

一旦执行“ SHUTDOWN”命令,将产生以下屏幕。

HSQLDB脚本文件 (HSQLDB Script File)

HSQLDB stores database table and its data in the form of SQL script file. For every database instance, HSQLDB creates a script file. If you open the script file in a text editor, it will look like a SQL script file. You can use this script file to export data from HSQLDB to other databases such as MySQL, Oracle etc.

HSQLDB以SQL脚本文件的形式存储数据库表及其数据。 对于每个数据库实例,HSQLDB都会创建一个脚本文件。 如果在文本编辑器中打开脚本文件,它将看起来像一个SQL脚本文件。 您可以使用此脚本文件将数据从HSQLDB导出到其他数据库,例如MySQL,Oracle等。

HSQLDB示例 (HSQLDB Example)

Let’s look at a simple example where we will connect to our HSQLDB database instance and retrieve some data from the test data we have inserted.

让我们看一个简单的示例,在该示例中,我们将连接到HSQLDB数据库实例并从插入的测试数据中检索一些数据。

Create a simple maven project and add HSQLDB driver dependency.

创建一个简单的maven项目并添加HSQLDB驱动程序依赖项。

<dependency>
	<groupId>org.hsqldb</groupId>
	<artifactId>hsqldb</artifactId>
	<version>2.4.1</version>
</dependency>

We will use org.hsqldb.jdbc.JDBCDriver driver class for creating connection to our HSLQDB database server. Below is the utility class showing how to create SQL Connection to HSQLDB database.

我们将使用org.hsqldb.jdbc.JDBCDriver驱动程序类来创建与我们的HSLQDB数据库服务器的连接。 下面的实用程序类显示了如何创建与HSQLDB数据库SQL连接。

package com.journaldev.hsqldb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class HSQLDBConnection {

	public static Connection getConnection() {
		Connection con = null;

		try {
			Class.forName("org.hsqldb.jdbc.JDBCDriver");
			System.out.println("HSQLDB JDBCDriver Loaded");
			con = DriverManager.getConnection(
					"jdbc:hsqldb:hsql://localhost/Test", "SA", "");
			System.out.println("HSQLDB Connection Created");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
}

Here is the test class where we are getting the HSQLDB connection and running some database queries.

这是我们获得HSQLDB连接并运行一些数据库查询的测试类。

package com.journaldev.hsqldb;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class HSQLDBExample {

	public static void main(String[] args) {
		Connection con = HSQLDBConnection.getConnection();
		System.out.println("Connection Obtained");

		try {
			PreparedStatement ps = con.prepareStatement(
					"select id, firstName, lastName from customer");
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				System.out.println("ID = " + rs.getInt("id") + 
						", Name = " + rs.getString("firstName") + " "
						+ rs.getString("lastName"));
			}
			rs.close();
			con.close();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}
}

Notice that except the Driver class, everything is using generic SQL classes. You can run other CRUD operation commands too.

请注意,除了Driver类之外,其他所有东西都使用通用SQL类。 您也可以运行其他CRUD操作命令。

Output produced by the main program:

主程序产生的输出:

HSQLDB JDBCDriver Loaded
HSQLDB Connection Created
Connection Obtained
ID = 0, Name = Laura Steel
ID = 1, Name = Robert King
...
ID = 49, Name = Robert Steel

Below image shows the final structure of our very simple HSQLDB example project.

下图显示了我们非常简单的HSQLDB示例项目的最终结构。

GitHub Repository. GitHub Repository下载示例代码。

摘要 (Summary)

HSQLDB is a very good utility to have a local database for development and running test systems. It has very low memory requirements and provides most of the basic features for the application. I think it lags into security area because the data is stored in simple text files. You can use it for basic development purposes and switch to better relational databases such as MySQL at later point of time.

HSQLDB是一个非常好的实用程序,拥有用于开发和运行测试系统的本地数据库。 它具有非常低的内存需求,并为应用程序提供了大多数基本功能。 我认为它属于安全领域,因为数据存储在简单的文本文件中。 您可以将其用于基本开发目的,并在稍后的时间切换到更好的关系数据库,例如MySQL。

Reference: Official Website

参考: 官方网站

翻译自: https://www.journaldev.com/21466/hsqldb-tutorial

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值