KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > hsqldb > HSQLEngine


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.hsqldb;
17
18 import java.io.File JavaDoc;
19 import java.sql.Connection JavaDoc;
20 import java.sql.DriverManager JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23
24 import org.hsqldb.Server;
25
26 /**
27  * HSQLDB数据库引擎管理器,用于启动和停止HSQLDB数据库服务
28  * @author Winter Lau
29  */

30 public class HSQLEngine {
31     
32     private static HSQLEngine engine;
33     private static Server hsqldb;
34     
35     private HSQLEngine(){}
36     /**
37      * 获得一个数据库引擎的实例,需要一个数据路径的参数
38      * 该参数必须为物理存在的路径
39      * @param dataPath
40      * @param dbn 数据库名
41      * @throws ClassNotFoundException
42      * @throws NoSuchMethodException
43      * @throws SecurityException
44      */

45     public synchronized static HSQLEngine getEngine(String JavaDoc dataPath, int port, String JavaDoc dbn)
46     {
47         if(engine!=null)
48             return engine;
49         HSQLEngine engine = new HSQLEngine();
50         if(!dataPath.endsWith(File.separator))
51             dataPath += File.separator;
52         hsqldb = new Server();
53         if(port>0)
54             hsqldb.setPort(port);
55         if (dbn != null){
56             hsqldb.setDatabaseName(0, dbn);
57             dataPath+=dbn;
58         }
59         hsqldb.setDatabasePath(0, dataPath);
60         
61         hsqldb.setSilent(true);
62         hsqldb.setTrace(false);
63         return engine;
64     }
65     
66     public void start(){
67         //调用HSQLDB的服务入口
68
hsqldb.start();
69     }
70     
71     public void stop(){
72         hsqldb.stop();
73         int i=0;
74         while(i<10 && isRunning()){
75             i++;
76             try{
77                 Thread.sleep(500);
78             }catch(Exception JavaDoc e){}
79         }
80     }
81     
82     public boolean isRunning(){
83         try{
84             hsqldb.checkRunning(true);
85             return true;
86         }catch(RuntimeException JavaDoc e){
87             return false;
88         }
89     }
90     
91     public String JavaDoc getDatabaseName() {
92         return hsqldb.getDatabaseName(0,false);
93     }
94
95     public String JavaDoc getDataPath() {
96         return hsqldb.getDatabasePath(0,false);
97     }
98
99     public int getPort() {
100         return hsqldb.getPort();
101     }
102     
103     public static void main(String JavaDoc[] args) throws Exception JavaDoc{
104         HSQLEngine engine = HSQLEngine.getEngine("D:\\TEST",9001,null);
105         engine.start();
106         try{
107             testCreateTable();
108         }finally{
109             engine.stop();
110         }
111     }
112     
113     public static void testCreateTable() throws Exception JavaDoc{
114         Class.forName("org.hsqldb.jdbcDriver");
115         Connection JavaDoc conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost","sa","");
116         PreparedStatement JavaDoc ps = null;
117         ResultSet JavaDoc rs = null;
118         try{
119             //ps = conn.prepareStatement("create table dlog_bookmark (markid INTEGER,logid INTEGER,siteid INTEGER,userid INTEGER,marktype INTEGER,createTime DATE,markorder INTEGER);");
120
//ps.executeUpdate();
121

122             ps = conn.prepareStatement("SELECT * FROM dlog_user");
123             rs = ps.executeQuery();
124             while(rs.next()){
125                 System.out.println(rs.getString("displayName"));
126             }
127         }finally{
128             if(rs!=null)
129                 rs.close();
130             if(ps!=null)
131                 ps.close();
132             if(conn!=null)
133                 conn.close();
134         }
135     }
136
137 }
Popular Tags