HSQLDB使用

HSQLDB 是一個用Java編寫的關(guān)系數(shù)據(jù)庫管理系統(tǒng)旭斥。它有一個JDBC驅(qū)動程序并且支持一個SQL-92和SQL:2008標(biāo)準(zhǔn)的較大子集。它提供了一個快速輕量級)數(shù)據(jù)庫引擎弃酌,該數(shù)據(jù)庫引擎提供基于磁盤和內(nèi)存的表甘穿。

官方介紹如下:

HSQLDB (HyperSQL DataBase) is the leading SQL relational database software written in Java. It offers a small, fast multithreaded and transactional database engine with in-memory and disk-based tables and supports embedded and server modes. It includes a powerful command line SQL tool and simple GUI query tools.

HSQLDB supports the widest range of SQL Standard features seen in any open source database engine: SQL:2011 core language features and an extensive list of SQL:2011 optional features. It supports nearly full Advanced ANSI-92 SQL (BNF format). Many extensions to the Standard, including syntax compatibility modes and features of other popular database engines, are also supported.

Version 2.3.4 is fully multithreaded and supports high performance 2PL and MVCC (multiversion concurrency control) transaction control models. See the list of new features in version 2.3.4.

快速入門

maven依賴:

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

HyperSQL Database

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

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.

A 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

A 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.

應(yīng)用專屬 Database Catalogs

1、文件型數(shù)據(jù)庫

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

或者database file 路徑為: /opt/db/testdb.

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

2监憎、內(nèi)存數(shù)據(jù)庫

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

3庆揩、res數(shù)據(jù)庫

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

關(guān)閉數(shù)據(jù)庫

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

Creating a New Database

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.

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

創(chuàng)建表

語法如下:

CREATE TABLE table_name (column_name column_type);

例如:

CREATE TABLE t_user (
    id BIGINT NOT NULL, 
    name VARCHAR(20) NOT NULL,
    password VARCHAR(32) NOT NULL, 
    age SMALLINT, 
    birthday TIMESTAMP,
    PRIMARY KEY (id)
);

代碼如下:

package com.bytebeats.codelab.hsqldb;

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

/**
 *
 * @author Ricky Fung
 * @create 2017-03-05 11:59
 */
public class CreateTableDemo {

    public static void main(String[] args) {

        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName("org.hsqldb.jdbc.JDBCDriver" );
            conn = DriverManager.getConnection("jdbc:hsqldb:file:/D:db/hsqldb/testdb", "SA", "");
            stmt = conn.createStatement();

            String sql = "CREATE TABLE t_user (" +
                    "id BIGINT NOT NULL, " +
                    "name VARCHAR(20) NOT NULL," +
                    "password VARCHAR(32) NOT NULL, " +
                    "age SMALLINT, " +
                    "birthday TIMESTAMP," +
                    "PRIMARY KEY (id)" +
                    ");";

            System.out.println(sql);

            int result = stmt.executeUpdate(sql);

            System.out.println("result:"+result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

}

插入記錄

語法如下:

INSERT INTO table_name (field1, field2,...fieldN)
VALUES (value1, value2,...valueN );

例如:

INSERT INTO t_user(id, name, password, age, birthday)  VALUES (1,'ricky', '12345', 28, '1989-09-15 15:00:00');

代碼如下:

package com.bytebeats.codelab.hsqldb;

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

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 * @create 2017-03-05 13:08
 */
public class InsertQueryDemo {

    public static void main(String[] args) {

        Connection con = null;
        Statement stmt = null;

        try {
            Class.forName("org.hsqldb.jdbc.JDBCDriver" );
            con = DriverManager.getConnection("jdbc:hsqldb:file:/D:db/hsqldb/testdb", "SA", "");
            stmt = con.createStatement();
            int result = stmt.executeUpdate("INSERT INTO t_user VALUES (1,'ricky', '12345', 28, '1989-09-15 15:00:00');");
            System.out.println(result);
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

查詢

語法如下:

SELECT field1, field2,...fieldN table_name1, table_name2...
[WHERE Clause]
[OFFSET M ][LIMIT N]

例如:

SELECT id, name, password, age, birthday FROM t_user

代碼如下:

package com.bytebeats.codelab.hsqldb;

import java.sql.*;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 * @create 2017-03-05 13:13
 */
public class SelectQueryDemo {

    public static void main(String[] args) {
        Connection con = null;
        Statement stmt = null;
        ResultSet result = null;

        try {
            Class.forName("org.hsqldb.jdbc.JDBCDriver");
            con = DriverManager.getConnection("jdbc:hsqldb:file:/D:db/hsqldb/testdb", "SA", "");
            stmt = con.createStatement();
            result = stmt.executeQuery(
                    "SELECT id, name, password, age, birthday FROM t_user");

            while(result.next()){
                System.out.println(result.getInt("id")+" | "+
                        result.getString("name")+" | "+
                        result.getString("password")+" | "+
                        result.getShort("age")+" | "+
                        result.getTimestamp("birthday"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                result.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

HSQLDB支持自增id,使用 IDENTITY 指定洽洁,如下:


CREATE TABLE t_user (
    id BIGINT IDENTITY NOT NULL,
    name VARCHAR(20) NOT NULL,
    password VARCHAR(32) NOT NULL,
    age SMALLINT,
    birthday TIMESTAMP,
    PRIMARY KEY (id)
);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末痘系,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子饿自,更是在濱河造成了極大的恐慌汰翠,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件昭雌,死亡現(xiàn)場離奇詭異奴璃,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)城豁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門苟穆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人唱星,你說我怎么就攤上這事雳旅。” “怎么了间聊?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵攒盈,是天一觀的道長。 經(jīng)常有香客問我哎榴,道長型豁,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任尚蝌,我火速辦了婚禮迎变,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘飘言。我一直安慰自己衣形,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布姿鸿。 她就那樣靜靜地躺著谆吴,像睡著了一般倒源。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上句狼,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天笋熬,我揣著相機(jī)與錄音,去河邊找鬼腻菇。 笑死突诬,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的芜繁。 我是一名探鬼主播旺隙,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼骏令!你這毒婦竟也來了蔬捷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤榔袋,失蹤者是張志新(化名)和其女友劉穎周拐,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凰兑,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡妥粟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了吏够。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片勾给。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖锅知,靈堂內(nèi)的尸體忽然破棺而出播急,到底是詐尸還是另有隱情,我是刑警寧澤售睹,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布桩警,位于F島的核電站,受9級特大地震影響昌妹,放射性物質(zhì)發(fā)生泄漏捶枢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一飞崖、第九天 我趴在偏房一處隱蔽的房頂上張望烂叔。 院中可真熱鬧,春花似錦蚜厉、人聲如沸长已。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽术瓮。三九已至,卻和暖如春贰健,著一層夾襖步出監(jiān)牢的瞬間胞四,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工伶椿, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留辜伟,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓脊另,卻偏偏與公主長得像导狡,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子偎痛,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容