hsqldb实例代码
package com.dmmap.adaptor.dao;
?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
?
import org.hsqldb.server.Server;
?
?
/**
?* 便利单机法度应用HSQL的对象类,包含启动,封闭,连接。数据库默认不加密,用户为sa 密码空
?*/
public class HsqlUtil {
?
? ? public static final int PORT = 9002;
? ? public static final String DB_NAME = "adaptor"; ? ? ? //数据库文件名,同时也是本类中的数据库名
? ? public static final String DB_PATH = "./WebContent/database/";
? ? 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(HsqlUtil.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);
? ? ? ? ? ? ? ? System.out.println(DB_PATH+DB_NAME);
? ? ? ? ? ? }
? ? ? ? } catch (ClassNotFoundException ex) {
? ? ? ? ? ? Logger.getLogger(HsqlUtil.class.getName()).log(Level.SEVERE, null, ex);
? ? ? ? } catch (SQLException ex) {
? ? ? ? ? ? Logger.getLogger(HsqlUtil.class.getName()).log(Level.SEVERE, null, ex);
? ? ? ? }
? ? ? ? return conn;
? ? }
}
?
?
//public static List<SMS> getSMS()
//{
//List<SMS> list=new ArrayList<SMS>();
//HsqlUtil.mode = HsqlUtil.STAND_ALONE_MODE;
// ? ?HsqlUtil.startHSQL();
// ? ?Connection conn = HsqlUtil.getConnection();
// ? ?try {
// ? ?Statement statement =conn.createStatement();
// ? ? ? ?ResultSet rs=statement.executeQuery("select * from record");
// ? ? ? ?while(rs.next())
// ? ? ? ?{
// ? ? ? ?SMS sms=new SMS();
// ? ? ? ?sms.setId(rs.getString(1));
// ? ? ? ?sms.setMobliePhone(rs.getString(2));
// ? ? ? ?sms.setContnet(rs.getString(3));
// ? ? ? ?sms.setTime(rs.getString(4));
// ? ? ? ?list.add(sms);
// ? ? ? ?}
// ? ? ? ?statement.close();
// ? ?} catch (SQLException ex) {
// ? ? ? ?Logger.getLogger(HsqlUtil.class.getName()).log(Level.SEVERE, null, ex);
// ? ?}
// ? ?HsqlUtil.stopHSQL();
//return list;
//}
?
//public static void initDB()
//{
//HsqlUtil.mode = HsqlUtil.STAND_ALONE_MODE;
// ? ?HsqlUtil.startHSQL();
// ? ?Connection conn = HsqlUtil.getConnection();
// ? ?try {
// ? ? ? ?Statement statement =conn.createStatement();
// ? ? ? ?statement.executeUpdate("create table record(id varchar(64) not null primary key ," +
// ? ? ? ?"mobliePhone varchar(20),contnet varchar(20),time varchar(20))");
// ? ? ? ?statement.close();
// ? ?} catch (SQLException ex) {
// ? ? ? ?Logger.getLogger(HsqlUtil.class.getName()).log(Level.SEVERE, null, ex);
// ? ?}
// ? ?HsqlUtil.stopHSQL();
//}
?
//public static void dropDB()
//{
//HsqlUtil.mode = HsqlUtil.STAND_ALONE_MODE;
// ? ?HsqlUtil.startHSQL();
// ? ?Connection conn = HsqlUtil.getConnection();
// ? ?try {
// ? ? ? ?Statement statement =conn.createStatement();
// ? ? ? ?statement.executeUpdate("drop table record");
// ? ? ? ?statement.close();
// ? ?} catch (SQLException ex) {
// ? ? ? ?Logger.getLogger(HsqlUtil.class.getName()).log(Level.SEVERE, null, ex);
// ? ?}
// ? ?HsqlUtil.stopHSQL();
//}