Background Image
Java
2016.06.28 03:11

CUBRID에서 Java AddBatch 사용

조회 수 8365 추천 수 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();
        }
    }
}


List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
66 Linux CUBRID 사용 포트와 iptables(방화벽) 설정 정만영 2010.03.01 38341
65 Linux CUBRID 복제 설계 가이드 file 정만영 2010.03.03 25139
64 Windows 윈도우즈 환경에서 배치화일을 이용하여 백업 또는 데이터베이스 정리 수행하기 남재우 2010.03.03 27487
63 기타 CUBRID 복제 구성하기 - 단일 서버에 구성 방법 포함 남재우 2010.03.08 30004
62 기타 데이터 저장 구조와 테이블 재구성 file 손승일 2010.04.21 37492
61 Linux CUBRID DB와 Broker 분리방안 정만영 2010.04.28 28412
60 PHP IIS에서 PHP, CUBRID 사용하기 file 남재우 2010.04.19 30311
59 기타 트랜잭션과 LOCK 초급과정 정만영 2010.07.01 20760
58 기타 CUBRID 2008 R3.0 추가기능 file janus 2010.09.03 15455
57 기타 CUBRID Migration Toolkit 튜토리얼 file cubrid 2010.10.22 30477
56 기타 CUBRID 활용 - 질의 튜닝 사례 중심 file cubrid 2010.12.02 45962
55 기타 데이터베이스 생성하기 file 남재우 2011.03.07 29110
54 Install CUBRID 설치 안내 - LINUX 남재우 2011.03.08 30415
53 기타 CUBRID에서의 BLOB/CLOB 타입 사용법 cubebridge 2011.03.08 30453
52 PHP apache, PHP 설치하기 - LINUX file 남재우 2011.03.08 31308
51 PHP phpize 를 이용한 PHP 모듈 설치 - LINUX file 남재우 2011.03.08 23090
50 PHP MySQL,PHP 기반에서 CUBRID,PHP 기반으로 포팅하기(CUBRID 2008 R3.1) 손승일 2011.03.30 24446
49 Install CUBRID Windows버전 삭제 및 재설치 실패시 강제 삭제하는 방법입니다. admin 2011.07.14 29550
48 PHP apache, php rpm package 생성 시 필요한 rpm 스펙 file admin 2011.07.14 25236
47 튜닝 CUBRID 세미나 자료(개요 및 SQL 활용) file admin 2011.07.14 22771
Board Pagination Prev 1 2 3 4 5 6 7 8 9 Next
/ 9

Contact Cubrid

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