我需要用Java ping H2数据库。
为了做到这一点,我正在向ip_db发送一个GET请求:port:
HttpUriRequest request = new HttpGet(url);
HttpResponse response = client.execute(request);
但我得到的只是ClientProtocolException。
当我尝试在Firefox中打开ip_db:port时,我得到以下信息(为了便于阅读而略微编辑):
90047BVersion mismatch, driver version is "0" but server version is "12"яяяя_їXorg.h2.jdbc.JdbcSQLException: Version mismatch, driver version is "0" but server version is "12" [90047-173]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
at org.h2.message.DbException.get(DbException.java:171)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:88)
at java.lang.Thread.run(Thread.java:724)
这是H2日志:
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: my_ip" [90067-173]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
at org.h2.message.DbException.get(DbException.java:160)
at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:407)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:295)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:105)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:90)
at org.h2.Driver.connect(Driver.java:73)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at org.h2.tools.CreateCluster.process(CreateCluster.java:137)
at org.h2.tools.CreateCluster.runTool(CreateCluster.java:85)
at org.h2.tools.CreateCluster.main(CreateCluster.java:52)
如何ping数据库需要做什么?
答案 0 :(得分:1)
你可以尝试这种方法
try {
Class.forName("org.h2.Driver");
conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
console.append("CONNECTED" + '\n');
if(conn.isValid(1000){
//DO WHATEVER FOR ALIVENESS
}
} catch (Exception e) {
console.append(e.getMessage() + '\n');
}