hikamen
2/25/2018 - 10:47 AM

使用JDBC测试数据库连通性

@Override
    public boolean testDBConnection(Connect connect) {
      boolean result = false;
      try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        // 返回数据库连接
        DriverManager.setLoginTimeout(1);// 设置连接超时
        Connection connection = DriverManager.getConnection(connect.getUrl(), connect.getUsername(), connect.getPassword());
        result = !connection.isClosed();// 判断是否关闭
        connection.close();// 关闭连接
      } catch (Exception e) {
        // TODO 记录日志
        e.printStackTrace();
      }
      return result;
    }