3

我想以编程方式启动 HSQLdb 服务器。我创建了一个如下所示的可运行文件,然后在线程中启动它。

   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]]: Shutdown because there are no open databases

谁能帮忙指出我哪里出错了。

谢谢你帮助巴拉

4

3 回答 3

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();
    }
}
于 2014-09-03T10:05:17.910 回答
0

正如您所评论的, server.setTrace(true) 有助于进行故障排除。server.setSilent(false) 也是如此。

但是您不应该创建并启动 Runnable 来启动服务器。Server 类完成所有这些并启动必要的线程。

于 2012-04-25T22:21:10.223 回答
0

对于 HSQL 2.3.x,请参阅下文以将 HSQL 作为 WebServer 运行(使用端口 80 进行网络的 HSQL 服务器。)

https://stackoverflow.com/a/37784679/15789

于 2016-06-13T08:17:29.313 回答