Background Image
Java
2016.06.28 03:11

CUBRID에서 Java AddBatch 사용

조회 수 8339 추천 수 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 Windows CUBRID2008 R2.0 Tutorial (Windows) cubebridge 2009.08.17 27337
65 기타 CUBRID 2008 R2.0 추가 및 변경 기능 소개 janus 2009.08.15 20663
64 기타 CUBRID 2008 1.x 에서 CUBRID 2.0 64bit로 Migration 3 janus 2009.08.15 15860
63 기타 CUBRID 32bit VS 64bit janus 2009.08.15 17483
62 Java CUBRID JDBC에서 유니코드 사용하기 1 손승일 2009.08.15 27434
61 Java CUBRID Collection Data Type 사용하기 - JDBC 손승일 2009.08.15 24902
60 기타 CUBRID 스캔 이해하기 손승일 2009.08.15 15113
59 Java CUBRID GLO 사용하기 - JDBC 손승일 2009.08.15 23493
58 Install CUBRID 2008 + Textyle 설치 가이드 3 1 Prototype 2009.07.29 18283
57 기타 CUBRID2008 쿼리 작성예제 cubebridge 2009.07.28 24902
56 기타 CUBRID주요 사용 함수 및 연산자 비교 cubebridge 2009.07.28 23450
55 기타 CUBRID2008데이터타입, 함수와 힌트사용법 및 예약어 cubebridge 2009.07.28 18839
54 기타 CUBRID2008 실행계획 분석하기 file cubebridge 2009.07.28 15338
53 Windows eclipse에서 python 및 CUBRID broker_log_top 사용하기 cubebridge 2009.07.28 27271
52 Java JavaSP SampleCode(Pivot기능) cubebridge 2009.07.28 27553
51 Install CUBRID 설치 및 매니저 구동하기(CUBRID 2008 R1.x) CUBRID_DEV 2009.07.18 31530
50 기타 에러 메시지(error) 설명 및 조치 가이드 file CUBRID_DEV 2009.07.11 24420
49 PHP MySQL,PHP 기반에서 CUBRID,PHP 기반으로 포팅하기 시난 2009.07.02 32190
48 Java log4jdbc를 사용한 JDBC 로그 분석 시난 2009.07.02 36391
47 ODBC/OLEDB ODBC드라이버를 이용한 2개 이상의 Statement 사용방법. file seongjoon 2009.07.02 23643
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