Java
2016.06.28 03:11

CUBRID에서 Java AddBatch 사용

조회 수 10836 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
대량 데이터를 저장하기 위해 자주 사용하는 AddBatch를 사용 합니다.
그러나 OutOfMemory가 발생 할 수 있으므로 적정하게 executeBatch()를 해줘야 합니다.

아래 간단한 예시( JAVA 소스 )

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelectAddBatchInsert {

   public static void main(String[] args) throws SQLException {

        Statement stmt = null;
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection conn = null;

       String url = "jdbc:CUBRID:localhost:33000:demodb:::";
       String userName = "dba";
       String passWord = "dba123";

        // Select Query
       StringBuffer sSql = new StringBuffer();
        sSql.append("   SELECT DISTINCT b.host_year as host_year, a.code as code, a.[name] as nm, to_char(b.game_date, 'YYYY-MM-DD') as game_dt  ");
        sSql.append("     FROM event a                              ");
        sSql.append("        , game b                               ");
        sSql.append("    WHERE a.code = b.event_code                ");

        // insert Query
        StringBuffer iSql = new StringBuffer();
        iSql.append(" INSERT INTO host_stat(host_year, code, [name], game_date, reg_sp) ");
        iSql.append(" VALUES( ?, ?, ?, cast(? as date), 'SelectAddBatchInsert') ");

        try {
            Class.forName("cubrid.jdbc.driver.CUBRIDDriver");
            conn = DriverManager.getConnection(url, userName, passWord);

            // I/F select
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sSql.toString());

            // Insert
            pstmt = conn.prepareStatement(iSql.toString());

            // Insert Count
            int Row_Count = 1;

            while(rs.next()) {

                String stHostYear    = rs.getString("host_year");
                String stCode    = rs.getString("code");
                String stNm    = rs.getString("nm");
                String stGameDt    = rs.getString("game_dt");

               pstmt.setString(1, stHostYear);
                pstmt.setString(2, stCode);
               pstmt.setString(3, stNm);
                pstmt.setString(4, stGameDt);

                // addBatch
                pstmt.addBatch();

                // Parameter Clear
                pstmt.clearParameters();

               // OutOfMemory : 1000 unit Commit
                if((rowCnt % 1000) == 0){
                    pstmt.executeBatch(); // Batch execute
                    pstmt.clearBatch(); // Batch Clear
                    conn.commit(); // connection commit
                }

                Row_Count++; 
            } // End While

            System.out.println("Insert Info Total Count : "+ (Row_Count- 1));   // Insert Total Count
            // Not Commit
            if ( Row_Count != 1 ){
                pstmt.executeBatch();
                conn.commit();
            }

            // connection close
            rs.close();
            conn.close();

            System.out.println(" SelectAddBatchInsert Info Total Count : "+ (rowCnt - 1));
        } catch ( Exception e ) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        } finally {
            if ( rs != null ) rs.close();
            if ( stmt != null ) stmt.close();
            if ( pstmt != null ) pstmt.close();
            if ( conn != null ) conn.close();
        }
    }
}


  1. SELECT ~ FOR UPDATE 대체 방법

    Date2009.12.30 CategoryJava By손승일 Views34109
    Read More
  2. 멀티미디어 데이터 관리를 위한 CUBRID GLO Method 활용

    Date2009.12.11 Category기타 Byjanus Views17802
    Read More
  3. QTADO를 이용하여 CUBRID접속하기

    Date2009.12.10 CategoryODBC/OLEDB Bycubebridge Views22583
    Read More
  4. CUBRID 개발 가이드

    Date2009.12.09 Category기타 Byjanus Views19790
    Read More
  5. 서비스 오픈을 위한 CUBRID 구성 가이드

    Date2009.11.28 Category기타 By손승일 Views25491
    Read More
  6. CUBRID R2.0 ISV(Independent Software Vendor)설치방법

    Date2009.11.20 CategoryInstall Bycubebridge Views29162
    Read More
  7. CUBRID 2008 R2.0의 CM변경사항

    Date2009.11.20 Category기타 Byjanus Views18696
    Read More
  8. CUBRID Java Stored Procedure와 DB Server Memory 관계

    Date2009.10.31 CategoryJava By손승일 Views42075
    Read More
  9. QuantumDB Eclipse Plugin을 사용하여 CUBRID연동하기

    Date2009.10.06 CategoryJava Bycubebridge Views30541
    Read More
  10. windows 환경에서 여러버젼의 CUBRID 설치하여 사용하기

    Date2009.09.16 CategoryWindows By남재우 Views27417
    Read More
  11. CUBRID HA introduction

    Date2009.08.28 Category기타 Byjanus Views22367
    Read More
  12. CUBRID2008 R2.0 실행계획 분석하기

    Date2009.08.27 Category기타 Bycubebridge Views23134
    Read More
  13. CUBRID 2008 + XE 설치 가이드 [R2.0]

    Date2009.08.27 Category기타 ByPrototype Views18098
    Read More
  14. CUBRID 설치 및 매니저 구동하기(CUBRID 2008 R2.0)

    Date2009.08.18 CategoryInstall ByCUBRID_DEV Views31560
    Read More
  15. DB의 스키마와 데이터를 파일로 내려받기

    Date2009.08.17 Category기타 Byseongjoon Views21362
    Read More
  16. CUBRID 주요 명령 요약 정리

    Date2009.08.17 Category기타 By정만영 Views18557
    Read More
  17. CUBRID Manager login 안내

    Date2009.08.17 Category기타 Byseongjoon Views17719
    Read More
  18. CUBRID2008_보안설정[R2.0]

    Date2009.08.17 Category기타 Byseongjoon Views17640
    Read More
  19. CUBRID2008R1.x to 2.0 마이그레이션 가이드 (32bit 기준)

    Date2009.08.17 CategoryLinux By정만영 Views28364
    Read More
  20. CUBRID Tutorial (Unix/Linux)

    Date2009.08.17 CategoryLinux By정만영 Views29144
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 Next
/ 9

Contact Cubrid

영업문의 070-4077-2112 / 기술문의 070-4077-2148 / 대표전화 070-4077-2110 / Email. contact_at_cubrid.com
Contact Sales

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

나눔고딕 사이트로 가기

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5