Java一点通

Java高级进阶

HSQL DB

HSQL DB
1.从网上下载:hsqldb_1_8_0_7.zip 压缩包,解压。
http://www.hsqldb.org/
然后建立一个文件夹,比如D:"hsqldb,在hsqldb目录下建立data,lib两个子文件夹,
将解压后的lib目录下的hsqldb.jar文件copy到 D:"hsqldb"lib 目录下,  
将解压后的demo目录下的runServer.bat和runManagerSwing.bat文件copy到 D:"hsqldb 目录下,

2.
修改runServer.bat文件的内容如下:
cd data
@java
-classpath ../lib/hsqldb.jar org.hsqldb.Server %1 %2 %3 %4 %5 %6 %7 %8 %9

修改runManagerSwing.bat文件的内容如下:
cd data

@java -classpath ../lib/hsqldb.jar org.hsqldb.util.DatabaseManagerSwing %1 %2 %3 %4 %5 %6 %7 %8 %9

3.先运行runServer.bat,启动HSQLDB server,
接着运行runManagerSwing.bat, 先弹出cmd窗口,接着弹出Connect窗口,
将Connect窗口的Type选项设置为 HSQL Database Engine Server,按ok连接到db,进入HSQL Database Manager窗口。
这时查看data目录下,发现生成4个文件:test.lck, test.log, test.properties, test.script

4.在HSQL Database Manager中execute下面sql 后,
create table test(id bigint , name varchar , remark varchar);
insert into test(id,name,remark)values(1,'admin','this is remark!');

在 test.log 中出现内容:
/*C2*/SET SCHEMA PUBLIC
CONNECT USER SA
create table test(id bigint , name varchar , remark varchar)
INSERT INTO TEST VALUES(1,'admin','this is remark!')

HSQL db中的数据就是储存在这个test.log文件中的。

5.在hsqldb目录下建立一java文件,内容如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import org.hsqldb.jdbcDriver;

public class HsqlDemo{
    
public static void main(String[] args){
        
try {
             Class.forName(
"org.hsqldb.jdbcDriver" );            
             Connection c
= DriverManager.getConnection("jdbc:hsqldb:hsql://localhost", "sa", "");
            
if(c != null){
                 System.out.println(
"Connected db success!");
                 String sql
= "CREATE TABLE T_USER(ID BIGINT,NAME VARCHAR,BIRTHDAY DATE);";
                 Statement st
= c.createStatement();
                 st.execute(sql);
                 sql
= "INSERT INTO T_USER(ID,NAME,BIRTHDAY)VALUES('1','ADMIN',SYSDATE);";
                 st.executeUpdate(sql);
                
if(st != null){
                     st.close();
                 }
                 c.close();
             }

         }
catch (Exception e) {
             System.out.println(
"ERROR: failed to load HSQLDB JDBC driver.");
             e.printStackTrace();
            
return;
         }

     }
}

再建立一test.bat文件,内容如下(注意配置path为你jdk的安装路径):
@echo off
set path=D:Program FilesJavajdk1.5.0_07bin
javac
-classpath lib/hsqldb.jar HsqlDemo.java
java
-cp lib/hsqldb.jar;./ HsqlDemo

运行test.bat文件后,在 HSQL Database Manager窗口选择View,Refresh tree    ,可以发现已经多了T_USER这个表,并且表里面已有一条记录。

6. 退出HSQL Database Manager, 到Hsql Db 的cmd窗口,按Ctrl+C结束DB。
在hsqldb目录下建立server.properties,内容如下:

server.port=9394
server.database.0=file:data/mydb9394
server.dbname.0=mydb
server.silent=true

建立runServer2.bat,内容如下:
@java -classpath lib/hsqldb.jar org.hsqldb.Server %1 %2 %3 %4 %5 %6 %7 %8 %9

这时运行runServer2.bat,则会在data目录下创建mydb9394.properties,mydb9394.log,mydb9394.lck三个文件,
再运行runManagerSwing.bat,弹出Connect窗口,这时需要将
Type选项设置为 HSQL Database Engine Server
URL 处设置为 jdbc:hsqldb:hsql://localhost:9394/mydb
按ok即可连接db进入 HSQL Database Manager界面。

7. Table的数据类型

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

posted on 2008-12-27 10:23 佰友 阅读(639) 评论(0)  编辑  收藏