连接到Tomcat服务器上运行的HSQLDB

问题描述 投票:0回答:1

我有一个 Web 服务应用程序,其中包含在应用程序加载到数据库时启动 HSQLDB 的代码。应用程序中的所有内容都按预期运行,包括连接到数据库、执行过程、创建表和运行查询,没有任何问题。但是,我试图编写一个单独的应用程序来连接到同一数据库并检索数据,但无法建立连接。我已经阅读了文档,但我要么不完全理解它,要么忽略了一些东西。

WEB服务应用程序代码如下:

文件:HSQLServer.java(包含初始化HSQL SERVER的代码)

    private HsqlServer() {
        //initialze new server
        hsqlServer = new Server();

        hsqlServer.setLogWriter(null);
        hsqlServer.setSilent(true);

        // can use custom writer
        hsqlServer.setLogWriter(null); 
        hsqlServer.setErrWriter(null); 
    }

public void startServer(String dbName, String dbPath) {  
        try {
            HsqlProperties p = new HsqlProperties();

            //dbName: "hsqldb" dbPath: "file:/usr/share/tomcat/webapps/TicketingAPIWS/DB/hsqldb"
            p.setProperty("server.database.0", "file:" + dbPath);
            p.setProperty("server.dbname.0", dbName);

            //set properties for DB
            hsqlServer.setProperties(p);

            //start the database!
            hsqlServer.start();

            LogUtils.log(CustomLogger.LogType.DEBUG, Boolean.TRUE, String.format("startServer:HSQL Database Server Has been Started"));
        } catch(Exception ex) {
            LogUtils.logException(CustomLogger.LogType.SEVERE, Boolean.TRUE, "startServer:Exception due to", ex);
        }
    }

文件:从主 WS 应用程序中的侦听器调用:启动 HSQLDB 并创建连接以执行存储过程,该过程运行良好

public static void initDB(ServletContext sc) {
        Connection conn = null;
        
        ConfigMgr configMgr = new ConfigMgr(sc);
        LogUtils.setupConfigurationMgr(configMgr);

        try {           
            boolean serverStatus = startHsqlServer(getDBName(configMgr), getDBPath(configMgr));           
            LogUtils.log(CustomLogger.LogType.DEBUG, Boolean.TRUE, String.format("initDB: Hsql Server Status=[%s]",serverStatus));
            
            //check if Hsql server is running
            if(serverStatus) {
                //get connection from the connection pool
                conn = getDefaultDatabaseConnection(configMgr);

                //add today's date as newDay in system_config
                addToSysConfig(conn, "newDay", new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()));
            }

                LogUtils.log(CustomLogger.LogType.DEBUG, Boolean.TRUE, "initDB: Successfully initialized the database");
            } else {
                LogUtils.log(CustomLogger.LogType.SEVERE, Boolean.TRUE, "initDB: Failed to initialize the database");
            }
        } catch(Exception ex){
            LogUtils.logException(CustomLogger.LogType.SEVERE, Boolean.TRUE, "initDB: Exception due to", ex);
        } finally {
            closeConnections(conn);
        } 
    }

这是我的连接字符串:

url="jdbc:hsqldb:hsql://localhost/hsqldb;file:/usr/share/tomcat/webapps/TicketingAPIWS/DB/hsqldb"

外部JAVA ANT APP

现在我已经运行了数据库服务器。所以我尝试从我编写的外部应用程序访问 HSQLDB,如下所示:

public static void main(String[] args) {
        Connection conn1 = null;
        // Display versions
        //clntObj.displayAppVersion(args);
            
        try {
            
            Class.forName("org.hsqldb.jdbcDriver");    
            //jdbc:hsqldb:hsql://localhost/pgmcdb
            //conn1 = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/hsqldb;file:/usr/share/tomcat/webapps/TicketingAPIWS/DB/hsqldb;server.remote_open=true;", "SA", "");
            conn1 = DriverManager.getConnection("jdbc:hsqldb:file:/usr/share/tomcat/webapps/TicketingAPIWS/DB/hsqldb;server.remote_open=true;", "sa", "");

            System.out.println(conn1);
            Statement s = conn1.createStatement();
            ResultSet rs = s.executeQuery ("SELECT web_client_id FROM client_sign_on;");
            
            while(rs.next()) {
                System.out.println("Table_Name: " + rs.getString(1) );
            }
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(HsqlClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(HsqlClient.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(conn1 != null){
                try {
                    conn1.close();
                } catch (SQLException ex) {
                    Logger.getLogger(HsqlClient.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

给出了这个错误:

SEVERE: null
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: client_sign_on
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
    at hsqlclient.HsqlClient.main(HsqlClient.java:173)
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: client_sign_on
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.ParserDQL.readTableName(Unknown Source)
    at org.hsqldb.ParserDQL.readTableOrSubquery(Unknown Source)
    at org.hsqldb.ParserDQL.XreadTableReference(Unknown Source)
    at org.hsqldb.ParserDQL.XreadFromClause(Unknown Source)
    at org.hsqldb.ParserDQL.XreadTableExpression(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQuerySpecification(Unknown Source)
    at org.hsqldb.ParserDQL.XreadSimpleTable(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryPrimary(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryTerm(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryExpressionBody(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryExpression(Unknown Source)
    at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 3 more

表存在并且命名正确。我在数据库中有一条记录,我可以使用 SQL 工具访问它,一切似乎都按顺序进行。但是,我无法从 Web 服务应用程序外部访问数据库,并且找不到任何解释如何执行此操作的资源。

这是表格及其内容:

java hsqldb
1个回答
0
投票

服务器通过 Tomcat 启动后,日志消息应显示 HSQL 服务器已启动。您的代码显示您已为此数据库选择了 URL 名称

hsqldb

现在您可以使用以下 URL 从 Tomcat 外部连接到服务器:

jdbc:hsqldb:hsql://localhost/hsqldb

请注意,一旦您启动了具有给定数据库的服务器,您只能通过服务器访问数据库,并且不允许通过

jdbc:hsqldb:file/...
连接直接访问。

© www.soinside.com 2019 - 2024. All rights reserved.