반응형
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¤t_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
반응형
'sourcecode' 카테고리의 다른 글
C# 코드를 PowerShell 스크립트로 변환하는 방법은 무엇입니까? (0) | 2023.08.17 |
---|---|
jQuery를 사용하여 선택한 옵션에 추가 데이터 추가 (0) | 2023.08.17 |
Oracle SQL을 사용한 트랜잭션은 어떻게 사용합니까? (0) | 2023.08.17 |
화면 방향 변경 시 사용자 정의 보기의 상태 손실을 방지하는 방법 (0) | 2023.08.17 |
nodejs에서 MariaDB 준비 문 쿼리를 사용하는 방법은 무엇입니까? (0) | 2023.08.17 |