gpt4 book ai didi

hsqldb - 以编程方式启动 hsqldb 数据库

转载 作者:行者123 更新时间:2023-12-01 12:54:58 26 4
gpt4 key购买 nike

我想以编程方式启动 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]]:关闭,因为没有打开的数据库

任何人都可以帮助指出我哪里出错了。

谢谢你的帮助
巴拉

最佳答案

您可以使用单独的类来管理您的 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();
}
}

关于hsqldb - 以编程方式启动 hsqldb 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10323798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com