4

我在 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);
    }

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

jdbc:h2:tcp://localhost/~/cdb

但收到这些错误消息:

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]

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

4

1 回答 1

13

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

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

于 2010-12-24T11:15:14.930 回答