sourcecode

JDBC를 사용하여 MySQL 세션 변수를 올바르게 설명하는 방법

copyscript 2023. 8. 17. 21:46
반응형

JDBC를 사용하여 MySQL 세션 변수를 올바르게 설명하는 방법

저는 각 그룹별로 상위 N개의 행을 얻을 수 있는 깔끔한 솔루션을 시도하고 있습니다.솔루션은 두 개의 세션 변수를 사용합니다.@country_rank그리고.@current_country솔루션은 DB와 직접 작업할 때 잘 작동하지만 세션 변수를 제대로 전달하지 못해서인지 JDBC에 문제가 있습니다.

JDBC에서는 쿼리가 그룹당 N개의 항목뿐만 아니라 모든 항목을 검색합니다.

테이블 정의 및 샘플 테이블 항목은 다음과 같습니다.

   CREATE TABLE cities
   (
      city VARCHAR(80),
      country VARCHAR(80),
      population INT
   );
 
   INSERT INTO cities VALUES ('New York', 'United States', 8175133); 
   INSERT INTO cities VALUES ('Los Angeles', 'United States', 3792621); 
   INSERT INTO cities VALUES ('Chicago', 'United States', 2695598); 
 
   INSERT INTO cities VALUES ('Paris', 'France', 2181000);
   INSERT INTO cities VALUES ('Marseille', 'France', 808000);
   INSERT INTO cities VALUES ('Lyon', 'France', 422000);
 
   INSERT INTO cities VALUES ('London', 'United Kingdom',   7825300);
   INSERT INTO cities VALUES ('Birmingham', 'United Kingdom', 1016800);
   INSERT INTO cities VALUES ('Leeds', 'United Kingdom', 770800);

다음 쿼리는 인구별로 각 국가의 상위 2개 도시를 반환합니다.

SELECT city, country, population
   FROM
     (SELECT city, country, population, 
                  @country_rank := IF(@current_country = country, @country_rank + 1, 1) AS country_rank,
                  @current_country := country 
       FROM cities
       ORDER BY country, population DESC
     ) ranked
   WHERE country_rank <= 2;

데이터베이스 출력은 다음과 같습니다.

+-------------+----------------+------------+
| city        | country        | population |
+-------------+----------------+------------+
| Paris       | France         |    2181000 |
| Marseille   | France         |     808000 |
| London      | United Kingdom |    7825300 |
| Birmingham  | United Kingdom |    1016800 |
| New York    | United States  |    8175133 |
| Los Angeles | United States  |    3792621 |
+-------------+----------------+------------+
6 rows in set (0.00 sec)

제 JDBC 코드는 아래와 같습니다.세션 변수를 전달하기 위해 다양한 옵션을 시도했지만 아무 것도 작동하지 않는 것 같습니다(각 그룹에서 상위 2개 항목을 얻는 대신 모든 항목을 얻는다는 점에서).

public static void main(String[] args) throws Exception {
    String dbURL;
    dbURL = "jdbc:mysql://" + domain + ":3306/" + DBNAME + "?sessionVariables=country_rank,current_country";
    // Option 1: Throws  java.sql.SQLException: Unknown system variable error
    
    dbURL = "jdbc:mysql://" + domain + ":3306/" + DBNAME + "?country_rank,current_country";
    // Option 2: Effectively a no-op
    
    dbURL = "jdbc:mysql://" + domain + ":3306/" + DBNAME + "?country_rank=null&current_country=null";
    // Option 3: Effectively a no-op
    
    Connection conn = DriverManager.getConnection(dbURL,dbUserName, dbPassword);
    Statement stmt = conn.createStatement();
    
    String query = "SELECT city, country, population FROM ";
    query += "(SELECT city, country, population, ";
    query += "@country_rank := IF(@current_country = country, @country_rank + 1, 1) AS country_rank,";
    query += "@current_country := country FROM cities ";
    query += "ORDER BY country, population DESC ";
    query += ") ranked WHERE country_rank <= 2";
    
    System.out.println("Query: " + query);
    System.out.println("+++++++++++++++++++++++++++++++++++++++++\n");
    
    ResultSet rs = stmt.executeQuery(query);
    
    int count = 0;
    while(rs.next()) {
        String city = rs.getString(1);
        String country = rs.getString(2);
        Integer population = rs.getInt(3);
        
        System.out.println(++count + ": " + city + "|" + country + "|" + population);
    }
    rs.close();
    stmt.close();
    conn.close();
}

잘못된 JDBC 프로그램 출력은 다음과 같습니다.

1: Paris|France|2181000
2: Marseille|France|808000
3: Lyon|France|422000
4: London|United Kingdom|7825300
5: Birmingham|United Kingdom|1016800
6: Leeds|United Kingdom|770800
7: New York|United States|8175133
8: Los Angeles|United States|3792621
9: Chicago|United States|2695598

언급URL : https://stackoverflow.com/questions/66201538/correct-way-to-account-for-mysql-session-variables-using-jdbc

반응형