0

我需要用 Java ping H2 数据库。

为此,我向 ip_db:port 发送了一个 GET 请求:

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 数据库?

4

1 回答 1

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');
    }
于 2013-11-08T07:11:25.227 回答