001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.hsqldb;
017:
018: import java.io.File;
019: import java.sql.Connection;
020: import java.sql.DriverManager;
021: import java.sql.PreparedStatement;
022: import java.sql.ResultSet;
023:
024: import org.hsqldb.Server;
025:
026: /**
027: * HSQLDB数据库引擎管理器,用于启动和停止HSQLDB数据库服务
028: * @author Winter Lau
029: */
030: public class HSQLEngine {
031:
032: private static HSQLEngine engine;
033: private static Server hsqldb;
034:
035: private HSQLEngine() {
036: }
037:
038: /**
039: * 获得一个数据库引擎的实例,需要一个数据路径的参数
040: * 该参数必须为物理存在的路径
041: * @param dataPath
042: * @param dbn 数据库名
043: * @throws ClassNotFoundException
044: * @throws NoSuchMethodException
045: * @throws SecurityException
046: */
047: public synchronized static HSQLEngine getEngine(String dataPath,
048: int port, String dbn) {
049: if (engine != null)
050: return engine;
051: HSQLEngine engine = new HSQLEngine();
052: if (!dataPath.endsWith(File.separator))
053: dataPath += File.separator;
054: hsqldb = new Server();
055: if (port > 0)
056: hsqldb.setPort(port);
057: if (dbn != null) {
058: hsqldb.setDatabaseName(0, dbn);
059: dataPath += dbn;
060: }
061: hsqldb.setDatabasePath(0, dataPath);
062:
063: hsqldb.setSilent(true);
064: hsqldb.setTrace(false);
065: return engine;
066: }
067:
068: public void start() {
069: //调用HSQLDB的服务入口
070: hsqldb.start();
071: }
072:
073: public void stop() {
074: hsqldb.stop();
075: int i = 0;
076: while (i < 10 && isRunning()) {
077: i++;
078: try {
079: Thread.sleep(500);
080: } catch (Exception e) {
081: }
082: }
083: }
084:
085: public boolean isRunning() {
086: try {
087: hsqldb.checkRunning(true);
088: return true;
089: } catch (RuntimeException e) {
090: return false;
091: }
092: }
093:
094: public String getDatabaseName() {
095: return hsqldb.getDatabaseName(0, false);
096: }
097:
098: public String getDataPath() {
099: return hsqldb.getDatabasePath(0, false);
100: }
101:
102: public int getPort() {
103: return hsqldb.getPort();
104: }
105:
106: public static void main(String[] args) throws Exception {
107: HSQLEngine engine = HSQLEngine
108: .getEngine("D:\\TEST", 9001, null);
109: engine.start();
110: try {
111: testCreateTable();
112: } finally {
113: engine.stop();
114: }
115: }
116:
117: public static void testCreateTable() throws Exception {
118: Class.forName("org.hsqldb.jdbcDriver");
119: Connection conn = DriverManager.getConnection(
120: "jdbc:hsqldb:hsql://localhost", "sa", "");
121: PreparedStatement ps = null;
122: ResultSet rs = null;
123: try {
124: //ps = conn.prepareStatement("create table dlog_bookmark (markid INTEGER,logid INTEGER,siteid INTEGER,userid INTEGER,marktype INTEGER,createTime DATE,markorder INTEGER);");
125: //ps.executeUpdate();
126:
127: ps = conn.prepareStatement("SELECT * FROM dlog_user");
128: rs = ps.executeQuery();
129: while (rs.next()) {
130: System.out.println(rs.getString("displayName"));
131: }
132: } finally {
133: if (rs != null)
134: rs.close();
135: if (ps != null)
136: ps.close();
137: if (conn != null)
138: conn.close();
139: }
140: }
141:
142: }
|