我想以编程方式启动HSQLdb服务器。 我创建了一个类似于下面的runnable,然后在一个线程中启动它。
Runnable hsqlRunnable = new Runnable() {
public void run()
{
HsqlProperties props = new HsqlProperties();
props.setProperty("server.database.0", "file:C:\Documents and Settings\BThirup\Application Data\Rockwell Automation\FactoryTalk ProductionCentre\logs\ApplicationLog\mydb;shutdown=true;");
props.setProperty("server.dbname.0", "xdb");
org.hsqldb.Server server = new org.hsqldb.Server();
try {
server.setProperties(props);
} catch (Exception e) {
return;
}
server.start();
}
}
我收到错误: [Thread [HSQLDB Server @ 4db602,6,main]]:由于没有打开的数据库而关闭
任何人都可以帮助指出我出错的地方。
感谢您的帮助 巴拉
答案 0 :(得分:4)
您可以使用单独的类来管理HSQLdb服务器实例:
public class DBManager {
final String dbLocation = "c:\\temp\\"; // change it to your db location
org.hsqldb.server.Server sonicServer;
Connection dbConn = null;
public void startDBServer() {
HsqlProperties props = new HsqlProperties();
props.setProperty("server.database.0", "file:" + dbLocation + "mydb;");
props.setProperty("server.dbname.0", "xdb");
sonicServer = new org.hsqldb.Server();
try {
sonicServer.setProperties(props);
} catch (Exception e) {
return;
}
sonicServer.start();
}
public void stopDBServer() {
sonicServer.shutdown();
}
public Connection getDBConn() {
try {
Class.forName("org.hsqldb.jdbcDriver");
dbConn = DriverManager.getConnection(
"jdbc:hsqldb:hsql://localhost/xdb", "SA", "");
} catch (Exception e) {
e.printStackTrace();
}
return dbConn;
}
}
并将此类用作:
public class WFProcess extends Thread {
DBManager dbm = new DBManager();
public static void main(String[] args) {
(new WFProcess()).start();
}
public void run() {
dbm.startDBServer();
// some usefull server work here
Connection conn = dbm.getDBConn();
try {
Statement stmt = conn.createStatement();
stmt.executeQuery("CREATE TABLE IF NOT EXISTS answers (num INT IDENTITY, answer VARCHAR(250))");
stmt.executeQuery("INSERT INTO answers (answer) values ('this is a new answer')");
ResultSet rs = stmt.executeQuery("SELECT num, answer FROM answers");
while (rs.next()) {
System.out.println("Answer number: " + rs.getString("num")
+ "; answer text: " + rs.getString("answer"));
}
} catch (Exception e) {
e.printStackTrace();
}
// end of usefull server work
dbm.stopDBServer();
}
}
答案 1 :(得分:0)
正如您所评论的那样,server.setTrace(true)有助于排除故障。 server.setSilent(false)也是如此。
但是你不应该创建并启动Runnable来启动服务器。 Server类完成所有这些并启动必要的线程。
答案 2 :(得分:0)
对于HSQL 2.3.x,请参阅以下内容将HSQL作为WebServer运行(使用端口80进行网络的HSQL服务器。)