HsqlDB Demo

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.logging.Level;

import java.util.logging.Logger;

import org.hsqldb.Server;

import org.hsqldb.result.Result;

/**

* 方便单机程序使用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 void main(String[] args) {

HSQL_Util.mode = HSQL_Util.STAND_ALONE_MODE;

HSQL_Util.startHSQL();

//        Connection conn = HSQL_Util.getConnection();

try {

createDB();

//            testQuery();

} catch (SQLException ex) {

Logger.getLogger(HSQL_Util.class.getName()).log(Level.SEVERE, null, ex);

}

HSQL_Util.stopHSQL();

}

private static void createDB() throws SQLException {

Statement statement = getConnection().createStatement();

statement.executeUpdate("create table customer2(id integer not null primary key,firstname varchar(100),lastname varchar(100))");

for (int i = 10; i < 60000; i++) {

statement.executeUpdate("insert into customer2 values(" + i + ",'liu','zhaoyang')");

}

statement.close();

}

private static void testQuery() throws SQLException {

Connection conn = HSQL_Util.getConnection();

ResultSet r = conn.createStatement().executeQuery("select * from customer");

while(!r.isLast()){

r.next();

Object o = r.getObject(1);

Object o1 = r.getObject(2);

System.out.println(o.toString()+o1.toString());

}

}

/**

* 启动数据库服务

*/

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模式,打开连接就同时启动数据库,所以这里可以什么都不做

System.out.println("hsqldb STAND_ALONE_MODE started...");

}

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;

}

}

上一篇:“耐撕”2016.04.13站立会议


下一篇:Git分支管理的基本操作