1. hsql 学习
1.1. 学习目的
本文档是针对hSQL 数据库方面的基础学习,为了使项目组成员能够达到使用hSQL 数据库的目的。
1.2. 培训对象
开发人员
1.3. 常用词及符号说明
常用词:
hsql:一种免费的跨平台的数据库系统
E:\hsqldb:表示是在dos 命令窗口下面
1.4. 参考信息
doc\guide\guide.pdf

2. HSQL
2.1. HSQL 运行工具
java -cp ../lib/hsqldb.jar org.hsqldb.util.DatabaseManager
注意hsqldb.jar 文件的文件路径,最好能放到classpath 里面,或者放到当前路径下.
java -cp hsqldb.jar org.hsqldb.util.DatabaseManager

2.2. 运行数据库
启动方式: Server Modes and
In-Process Mode (also called Standalone Mode).

一个test 数据库会包含如下文件:
? test.properties
? test.script
? test.log
? test.data
? test.backup
test.properties 文件包含关于数据库的一般设置.
test.script 文件包含表和其它数据库,插入没有缓存表的数据.
test.log 文件包含当前数据库的变更.
test.data 文件包含缓存表的数据
test.backup 文件是最近持久化状态的表的数据文件的压缩备份文件
所有以上这个文件都是必要的,不能被删除.如果数据库没有缓存表,test.data 和test.backup 文件将不会存在.另外,除了以上文件HSQLDB 数据库可以链接到任何文本文件,比如cvs 文件.

当操作test 数据库的时候, test.log 用于保存数据的变更. 当正常SHUTDOWN,这个文件将被删除. 否则(不是正常shutdown),这个文件将用于再次启动的时候,重做这些变更.test.lck 文件也用于记录打开的数据库的事实, 正常SHUTDOWN,文件也被删除.在一些情况下,test.data.old 文件会被创建,并删除以前的.






2.3. Server Mode
java -cp ../lib/hsqldb.jar org.hsqldb.Server -database.0 file:mydb -dbname.0 xdb

命令行方式:


启动数据,数据库文件mydb,数据库名称xdb

也可以在 server.properties 文件中定义启动的数据库,最多10个
例如: server.properties:
server.database.0=file:E:/hsqldb/data/mydb
server.dbname.0=xdb

server.database.1=file:E:/hsqldb/data/testdb
server.dbname.1=testdb

server.database.2=mem:adatabase
server.dbname.2=quickdb
启动命令: java -cp ../lib/hsqldb.jar org.hsqldb.Server
运行结果如下



java 测试程序:
package test;
import junit.framework.TestCase;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestConnect extends TestCase {
Connection connection;
protected void setUp()
{
try {
Class.forName("org.hsqldb.jdbcDriver" );
connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb","sa","");


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void testselect()
{
Statement stmt=null;
ResultSet rs=null;
try {
stmt = connection.createStatement();
String sql ="select * from test";
rs=stmt.executeQuery( sql);
while(rs.next() )
{
System.out.println("
Supported Properties
quoted = { true | false }
default is true. If false, treats double quotes as normal characters
all_quoted = { true | false }
default is false. If true, adds double quotes around all fields.
encoding =
character encoding for text and character fields, for example, encoding=UTF-8
ignore_first = { true | false }
default is false. If true ignores the first line of the file
cache_scale=
exponent to calculate rows of the text file in cache. Default is 8, equivalent to nearly 800 rows
cache_size_scale = r
exponent to calculate average size of each row in cache. Default is 8, equivalent to 256 bytes per row.
fs =
field separator
vs =
varchar separator
lvs =
long varchar separator
Special indicators for Hsqldb Text Table separators
\semi
semicolon
\quote
quote
\space
space character
\apos
apostrophe
\n
newline - Used as an end anchor (like $ in regular expressions)
\r
carriage return
\t
tab
\\
backslash
\u####
a Unicode character specified in hexadecimal
Only an administrator may do this.

6.1.51. SET WRITE DELAY[1]
SET WRITE_DELAY {{ TRUE | FALSE } | | MILLIS};
6.1.52. SHUTDOWN
SHUTDOWN [IMMEDIATELY | COMPACT | SCRIPT[1]];
6.2. Data Types
Table 9.1. Data Types. The types on the same line are equivalent.
Name Range Java Type
INTEGER | INT as Java type int | java.lang.Integer
DOUBLE [PRECISION] | FLOAT as Java type double | java.lang.Double
VARCHAR as Integer.MAXVALUE java.lang.String
VARCHAR_IGNORECASE as Integer.MAXVALUE java.lang.String
CHAR | CHARACTER as Integer.MAXVALUE java.lang.String
LONGVARCHAR as Integer.MAXVALUE java.lang.String
DATE as Java type java.sql.Date
TIME as Java type java.sql.Time
TIMESTAMP | DATETIME as Java type java.sql.Timestamp
DECIMAL No limit java.math.BigDecimal
NUMERIC No limit java.math.BigDecimal
BOOLEAN | BIT as Java type boolean | java.lang.Boolean
TINYINT as Java type byte | java.lang.Byte
SMALLINT as Java type short | java.lang.Short
BIGINT as Java type long | java.lang.Long
REAL as Java type double | java.lang.Double[1]

BINARY as Integer.MAXVALUE byte[]
VARBINARY as Integer.MAXVALUE byte[]
LONGVARBINARY as Integer.MAXVALUE byte[]
OTHER | OBJECT as Integer.MAXVALUE java.lang.Object
The uppercase names are the data types names defined by the SQL standard or commonly used by RDMS's. The data types in quotes are the Ja

6.2.1. 自动增长:
create table user(id IDENTITY,name varchar(20));
sql> create table dept(id int GENERATED BY DEFAULT AS IDENTITY(start with 10,increment by 5) not null PRIMARY KEY,name v
archar(20));
sql> insert into dept(name) values('asc');
1 row updated
sql> insert into dept(name) values('security');
1 row updated
sql> select * from dept;
ID NAME
-- --------
10 asc
15 security

2 rows

6.3. SQL Comments
-- SQL style line comment
// Java style line comment
/* C style line comment */
7. Hsqldb Test Utility
拷贝 junit.jar 到/lib 目录下
运行: ant hsqldbtest
生成 hsqldbtest.jar
运行: \hsqldb\testrun\hsqldb>runtest TestSelf

相关文章:

  • 2021-07-02
  • 2021-11-23
  • 2021-11-01
  • 2021-06-20
猜你喜欢
  • 2021-12-05
  • 2021-11-30
  • 2021-08-05
  • 2021-07-18
  • 2022-01-19
  • 2021-12-02
相关资源
相似解决方案