HSQLDB issue: Starting the HSQL database from Java Code

后端 未结 2 1966
谎友^
谎友^ 2021-01-07 12:36

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:

相关标签:
2条回答
  • 2021-01-07 12:59

    What about something like that https://javajazzle.wordpress.com/2011/06/23/embedded-database-in-java-use-of-hsqldb/

    0 讨论(0)
  • 2021-01-07 13:00

    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;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题