when I have to run HSQLDB for my application i have to do it from command prompt, so I always double-click the Server.bat (batch file) to start the server which contain:
What about something like that https://javajazzle.wordpress.com/2011/06/23/embedded-database-in-java-use-of-hsqldb/
Sample code to start and stop the HSQL WebServer programmatically. Imports removed for brevity.
Keeping it simple, this code is not threadsafe. This is just sample code.
Also note that the in memory db will start automatically when it receives the first jdbc request. Also, shutdown the in memory db by executing the SQL command SHUTDOWN via jdbc.
Uses org.hsqldb.server.WebServer (i.e. the hsql db server uses http port 80), but you may use org.hsqldb.server.Server instead. You may call setPort on either to override the default port.
public class HsqlServer {
/**
* Start the hsql server locally, with an HTTP interface. Rightclick on this
* class in Eclipse, and run.
*
* @param args
*/
private static WebServer ws;
public static void main(String args[]) {
new HsqlServer().startDB();
}
public void stopDB() {
if (ws != null) {
try {
ws.checkRunning(true);
System.out.println("db is running. stopping now");
stopServer2();
}
catch(HsqlException hsqle) {
System.out.println("db is already stopped");
}
}
else {
System.out.println("DB not started. it is null");
}
}
private void stopServer2() {
ws.shutdownWithCatalogs(Database.CLOSEMODE_NORMAL);
}
public void startDB() {
// String[] argsToServer = { "--database.0",
// "file:" + HsqlServerConst.dbFileName, "-dbname.0",
// HsqlServerConst.dbName };
// WebServer.main(argsToServer);
if (ws != null) {
try {
ws.checkRunning(false);
System.out.println("check running is false");
startServer2();
} catch (HsqlException hsqle) {
// already running.
System.out.println("Server is already running.");
return;
}
} else {
// start the server, it is null
System.out.println("server is null, starting now");
startServer2();
}
}
private WebServer startServer2() {
ws = new WebServer();
ws.setDatabasePath(0, "file:" + HsqlServerConst.dbFileName);
ws.setDatabaseName(0, HsqlServerConst.dbName);
ws.start();
return ws;
}
}