I want to programmatically start HSQLdb server. I create a runnable like below and then start it in a thread.
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();
}
}
I get the error: [Thread[HSQLDB Server @4db602,6,main]]: Shutting down because there are no open databases
Can anyone help point out where I am going wrong.
Thanks for helping Bala
You can use separate class to manage your HSQLdb server instance:
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;
}
}
and use this class as that:
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With