JavaからMySQLのテーブル数を取得するメソッド

  public static long getTableCount(String url, String user, String password,
      String driver) throws Exception {
    Connection conn = null;
    PreparedStatement stmt = null;
    long result = 0L;
    ResultSet resultSet = null;
    try {
      Class.forName(driver);
      conn = DriverManager.getConnection(url, user, password);
      String sql =
        "select count(*) as c from information_schema.tables where table_schema=database()";
      stmt = conn.prepareStatement(sql);
      resultSet = stmt.executeQuery();
      if (resultSet.next()) {
        result = resultSet.getLong(1);
      }
    } catch (SQLException se) {
      throw se;
    } catch (Exception e) {
      throw e;
    } finally {
      try {
        if (resultSet != null) {
          resultSet.close();
        }
      } catch (SQLException se3) {
        throw se3;
      }
      try {
        if (stmt != null) {
          stmt.close();
        }
      } catch (SQLException se2) {
        throw se2;
      }
      try {
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException se) {
        throw se;
      }
    }
    return result;
  }