我在 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”,然后服务器响应更改为:用户名或密码错误!