HSQLD server模式
来源:互联网 发布:plc网络模块 编辑:程序博客网 时间:2024/05/22 03:10
使用java写单机小程序或者测试项目的时候,使用hsqldb是一个不错的选择。
介绍可以看这里:
http://www.javaeye.com/topic/78887
http://hi.baidu.com/mum0532/blog/item/b08c0c6d88e0faf9431694e5.html
从官网下载的压缩包,解压后的demo目录中,可以运行runManagerSwing,打开可视化管理界面(但没有可视化创建表,创建字段的功能)
连接的时候,先选择server模式,然后在下图的红框处输入启动服务的端口和数据库名称。例如jdbc:hsqldb:hsql://localhost:9002/test
这里9002是我程序启动hsqldb的时候使用的端口,test是建立的数据库名称
HSQLDB创建数据库和基本的数据库访问:
http://hi.baidu.com/hivemind/blog/item/83873bdf36611c1462279825.html
HSLQDB的sql语法跟MySQL的略有不同,使得很郁闷啊~~开始我都不知道怎么初始化HSQLdb的数据
HSQLDB和Hibernate结合的一个小例子:
http://hi.baidu.com/hivemind/blog/item/2c77fb00830e5a16738b6506.html
我自己写的一个Java类:(只需要加入hsqldb.jar即可)
/** * 方便单机程序使用HSQL的工具类,包括启动,关闭,连接。数据库默认不加密,用户为sa,密码空 * @author 郑高强 */public class HSQL_Util { public static final int PORT = 9002; public static final String DB_NAME = "kenko"; //数据库文件名,同时也是本类中的数据库名 public static final String DB_PATH = "./db/"; public static final String USER_NAME = "sa"; public static final String PASSWORD = ""; public static final int SERVER_MODE = 0; public static final int STAND_ALONE_MODE = 1; //In-Process public static int mode = SERVER_MODE; //记录当前用什么模式,开发时用Server,发布时用standalone /** * 启动数据库服务 */ public static boolean startHSQL() { if (mode == SERVER_MODE) { Server server = new Server();//它可是hsqldb.jar里面的类啊。 server.setDatabaseName(0, DB_NAME); server.setDatabasePath(0, DB_PATH + DB_NAME); server.setPort(PORT); server.setSilent(true); server.start(); //自动多线程运行 System.out.println("hsqldb started..."); } else if (mode == STAND_ALONE_MODE) { //standalone模式,打开连接就同时启动数据库,所以这里可以什么都不做 } try { Thread.sleep(800); // 等待Server启动 } catch (InterruptedException e) { } return true; } /** * 关闭数据库服务 */ public static boolean stopHSQL() { try { Statement statement = getConnection().createStatement(); statement.executeUpdate("SHUTDOWN;"); return true; } catch (SQLException ex) { Logger.getLogger(HSQL_Util.class.getName()).log(Level.SEVERE, null, ex); return false; } } /** * 获取连接 */ public static Connection getConnection() { Connection conn = null; try { Class.forName("org.hsqldb.jdbcDriver"); if (mode == SERVER_MODE) { conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost:" + PORT + "/" + DB_NAME, USER_NAME, PASSWORD); } else if (mode == STAND_ALONE_MODE) { conn = DriverManager.getConnection("jdbc:hsqldb:file:" + DB_PATH + DB_NAME, USER_NAME, PASSWORD); } } catch (ClassNotFoundException ex) { Logger.getLogger(HSQL_Util.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(HSQL_Util.class.getName()).log(Level.SEVERE, null, ex); } return conn; } /** * 测试 */ public static void main(String[] args) { HSQL_Util.mode = HSQL_Util.STAND_ALONE_MODE; HSQL_Util.startHSQL(); Connection conn = HSQL_Util.getConnection(); try { Statement statement = getConnection().createStatement(); statement.executeUpdate("create table customer(id integer not null primary key,firstname varchar,lastname varchar)"); for (int i = 10; i < 20; i++) { statement.executeUpdate("insert into customer values(" + i + ",'liu','zhaoyang')"); } statement.close(); } catch (SQLException ex) { Logger.getLogger(HSQL_Util.class.getName()).log(Level.SEVERE, null, ex); } HSQL_Util.stopHSQL(); }}
运行上述的类,在项目目录,会生成一个db文件夹,里边有数据库的一些基本文件:
String note = "XXXX"; //这里省略,实际是800个中文字 System.out.println(note.length()); HSQL_Util.mode = HSQL_Util.STAND_ALONE_MODE; HSQL_Util.startHSQL(); Connection conn = HSQL_Util.getConnection(); try { Statement statement = getConnection().createStatement(); statement.executeUpdate("create cached table customer(id integer not null primary key,firstname varchar,lastname varchar)"); for (int i = 1; i < 60000; i++) { //插入6万条数据用了43秒,生成262M的文件 statement.executeUpdate("insert into customer values(" + i + ",'" + note + "','zhaoyang')"); } statement.executeQuery("select * from customer where id = 40"); //6万条数据用了9秒 statement.close(); } catch (SQLException ex) { Logger.getLogger(HSQL_Util.class.getName()).log(Level.SEVERE, null, ex); } HSQL_Util.stopHSQL(); }
使用HSQL的时候,就需要注意创建表用什么类型了!!!
开始我不知道,使用了默认的Momery表,“create table” 测试了一下,速度慢得吐血。写6万条记录,用了五十多秒,而且只是写到3万多就崩溃了,内存溢出~~哈哈,其实也正常,十几M数据爆了~~然后从这3万个记录,select * from custemer where id = XX,用了几秒钟
后来我再找方法,换成cached方法,create cached table试了一下,果然有效果,直接写6万个记录,没有爆,用了43秒。
再select就用了9秒。相对而言,比momery表好一点。但看了一下数据文件,我~~~竟然生成了262M的数据文件!!!!!
- HSQLD server模式
- SQL Server 2005 模式
- log4cplus server client模式
- hive的server模式
- FastDb client-server模式
- log4cplus server client模式
- SQL Server 身份验证模式
- FastDb client-server模式
- Abstract Server模式
- FastDb client-server模式
- MSSQL Server备份 模式
- thrift server模式
- Sql Server 恢复模式
- sql-server(模式创建)
- JVM的Client模式与Server模式
- JVM的server模式和client模式
- JVM的Server模式和Client模式
- oracle的shared server模式和dedicated server模式
- 计算函数的值
- 关于RelativeLayout的使用的注意事项
- sencha touch 里Ext.list的简单使用
- 何去何从,把酒问今天。
- acm-输出学生成绩等级
- HSQLD server模式
- iOS开发非常用库
- poj2485
- python urllib2详解
- 程序员技术练级攻略(转)
- Android属性系统 Property service 设定分析
- 按行读取文件(php、c实现)
- 第八周上机实践
- 使用Spring中Transcation进行数据库集成测试