(JAVA) 순수한 JDBC Multi를 실행하고 결과 얻기

RAW 데이터를 기반으로 통계를 생성하려면 데이터베이스에 연결해야 하지만 통계에 대한 SQL 쿼리당 하나의 연결을 사용하여 결과를 얻는 좋은 방법은 아닙니다.

MultiQuery는 단일 연결을 통해 원하는 통계 SQL 결과를 제공하는 데 사용되었습니다.

JAVA의 DB 구현 기술에는 순수 JDBC 사용, JDBCTemplate, Mybatis 및 JPA가 포함되지만 여기서는 순수 JDBC를 사용합니다.

다중 쿼리를 사용하는 방법이 작성되었습니다.

JDBC 연결 설정에 대한 정보

JDBC URL 설정 시 allowMultiQueries=true 값을 설정해야 합니다.

spring:
  datasource:
    localhost:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/TEST?characterEncoding=UTF-8&serverTimezone=UTC&allowMultiQueries=true
      username: root
      password: abcd1234

application.yml의 Spring Boot에서 JDBC 연결 구성을 처리하고 있었습니다.

jdbc url이 “allowMultiQueries=true”를 포함하는 것이 중요합니다.

ResultSet 유형의 getMoreResult() 사용

public void closeStatistics(String businessDay) throws SQLException {

    dataSource = getConnection();

    String() columnNames = {"totalCount", "notRpkMappedCount", "rpkMappedCount", "notSendedCount", "sendedCount", "clearCount", "errorCount"};
    String sql = "SELECT count(*) totalCount from bikeDrivingBatch where businessDay = '" + businessDay + "' and useYNull="Y";" +
            "SELECT count(*) notRpkMappedCount from bikeDrivingBatch where businessDay = '" + businessDay + "' and rpk is null and useYNull="Y";" +
            "SELECT count(*) rpkMappedCount from bikeDrivingBatch where businessDay = '" + businessDay + "' and rpk = 'Y' and useYNull="Y";" +
            "SELECT count(*) notSendedCount from bikeDrivingBatch where businessDay = '" + businessDay + "' and sendYN is null and useYNull="Y";" +
            "SELECT count(*) sendedCount from bikeDrivingBatch where businessDay = '" + businessDay + "' and sendYN = 'Y' and useYNull="Y";" +
            "SELECT count(*) clearCount from bikeDrivingBatch where businessDay = '" + businessDay + "' and sendYN = 'Y' and useYNull="Y" and recvMsgCode="00";" +
            "SELECT count(*) errorCount from bikeDrivingBatch where businessDay = '" + businessDay + "' and sendYN = 'Y' and useYNull="Y" and (recvMsgCode <>'00' or recvMsgCode is null);";

    log.info("sql = {}",sql);

    Connection con = null;
    Statement stmt = null;

    try {
        con = dataSource.getConnection();
        stmt = con.createStatement();
        boolean results = stmt.execute(sql);

        int rsCount = 0;
        log.info("rs = {}", results);
        log.info("rsCount = {}", rsCount);

        // Loop through the available result sets.
        do {
            if (results) {
                ResultSet rs = stmt.getResultSet();

                // Show data from the result set.
                System.out.println("RESULT SET #" + rsCount);
                while (rs.next()) {
                    String columnLabel = columnNames(rsCount);
                    System.out.println(rs.getString(columnLabel));
                }
                rsCount++;
            }
            System.out.println();
            results = stmt.getMoreResults();
        } while (results);

    } catch (SQLException e) {
        log.error("db error", e);
        throw e;
    } finally {
        close(con, stmt, null);
    }

}

일반적인 짧은 SQL 문을 수행할 때 ResultSet이라는 Statement 객체가 있는데, 위의 경우에는 여러 쿼리의 개수만큼 존재하며 각각의 결과 값이 포함되어 있다.

이때 getMoreResult()를 이용하여 while문을 처리하면 다음 문장에 대한 결과값을 확인할 수 있다.