转载自: http://ehilcoder.iteye.com/blog/1722805
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
2.下载HSQL
访问Hsqldb的官方地址:http://hsqldb.org/,下载最新的hsqldb,下载地址:http://sourceforge.net/projects/hsqldb/files/
下载后解压即可。
3.创建HSQL数据库(以文件类型数据库为例)
(1)建立一个文件夹,目录格式如下:d:\hsql\data
(2)复制hsqldb.jar文件到d:\hsql\下
※hsqldb.jar文件在..\hsqldb-2.2.6\hsqldb\lib目录下
(3)创建数据库
进入命令提示符,进入data文件夹,输入命令:
D:\hsqldb\data>java -classpath ../hsqldb.jar org.hsqldb.Server -database.0 file:DB数据库名
此时,在d:\hsql\data目录下有如下的文件产生,数据库服务启动完毕。
---mydb.tmp(目录)
---mydb.lck
---mydb.log
---mydb.properties
---mydb.script
4.创建表并插入数据
(1)打开数据库控制界面
进入命令提示符,进入data文件夹,输入命令:
D:\hsqldb\data>java -cp ../hsqldb.jar org.hsqldb.util.DatabaseManager 。
(2)进入数据库控制界面后,输入【create table】和【insert into 】来创建和插入数据。
5.测试代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnetcHsqlDb {
public static void main(String[] args) {
String url = "jdbc:hsqldb:hsql://localhost/";
String user = "SA";
String password = "";
try {
Class.forName("org.hsqldb.jdbcDriver");
Connection conn = DriverManager.getConnection(url, user, password);
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery("SELECT FIRSTNAME,LASTNAME FROM customer");
while (rs.next()) {
System.out.print(rs.getString("FIRSTNAME") + " ");
System.out.print(rs.getString("LASTNAME") + " ");
System.out.println("");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
6.给数据库起别名
创建命令:java -cp ../hsqldb.jar org.hsqldb.Server -database.0 mydb -dbname.0 mydbalias
访问时,需要在在localhost后面加一个sun,如示:jdbc:hsqldb:hsql://localhost/mydbalias
7.完毕。
8.感谢【Hadi Teo 】和【AnkerJia】的资料
AnkerJia: http://jiaxiaoyuan1204.blog.163.com/blog/static/65553152011219115420173/
Hadi Teo:http://www.coderanch.com/t/553739/java/Unable-create-hsqldb-spring
转载自: http://ehilcoder.iteye.com/blog/1722805