h2混合模式连接问题

idi*_*ius 4 h2

我在servlet上下文监听器中启动h2数据库:

public void contextInitialized(ServletContextEvent sce) {
     org.h2.Driver.load();
     String apprealPath = sce.getServletContext().getRealPath("\\");
     String h2Url = "jdbc:h2:file:" + apprealPath + "DB\\cdb;AUTO_SERVER=true";
     LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 
        StatusPrinter.print(lc); 
     logger.debug("h2 url : " + h2Url);
     try {
   conn = DriverManager.getConnection(h2Url, "sa", "sa");
  } catch (SQLException e) {
   e.printStackTrace();
  }
  logger.debug("h2 database started in embedded mode");
        sce.getServletContext().setAttribute("connection", conn);
    }
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用dbvisualizer使用以下url连接到h2:

jdbc:h2:tcp://localhost/~/cdb
Run Code Online (Sandbox Code Playgroud)

但得到这些错误消息:

An error occurred while establishing the connection:
   Type: org.h2.jdbc.JdbcSQLException   Error Code: 90067   SQL State: 90067
Message:
   Connection is broken: "Connection refused: connect" [90067-148]
Run Code Online (Sandbox Code Playgroud)

我试图用"172.17.33.181:58524"替换localhost(我在cdb.lock.db中找到它)重新连接用户"sa"密码"sa",然后服务器响应更改为:用户名或密码错误!

Tho*_*ler 13

自动混合模式下,您不需要(也不能)使用jdbc:h2:tcp://localhost.只需在任何地方使用相同的URL,这意味着jdbc:h2:file:...DB\\cdb;AUTO_SERVER=true.

您可以使用相同的数据库URL,而不管数据库是否已打开.不支持显式客户端/服务器连接(使用jdbc:h2:tcp://或ssl://).