Java
2016.06.28 03:11

CUBRID에서 Java AddBatch 사용

조회 수 10782 추천 수 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
번호 분류 제목 글쓴이 날짜 조회 수
147 Linux 가상머신 환경에서 리눅스 및 큐브리드 설치 가이드 file 이경오 2015.07.14 15195
146 기타 CUBRID Migration방법(by unloaddb & loaddb) 주현 2015.07.01 24913
145 기타 콘솔에서 마이그레이션 하기. file 권호일 2015.06.19 15596
144 PHP CUBRID HA 환경에서 Apache-PHP 설정 가이드 이용미 2015.06.16 11949
143 기타 CUBRID isolation level & Dirty Read 정만영 2015.05.29 13329
142 Windows Windows 에 설치 된 JRE 설치 위치 확인 하기. file 성진 2015.05.28 13932
141 Java CUBRID와 MyBATIS 빠르게 연동하기 file 김승훈 2015.05.07 21033
140 Linux CUBRID Linux 부팅시 자동시작 서비스 설정 방법 정만영 2013.09.16 27041
139 PHP phpize를 이용한 PHP 모듈 연동 방법(CUBRID2008 R4.3 버전) file 이용미 2013.05.16 22128
138 PHP Apache, PHP 설치 방법 file 이용미 2013.05.16 42791
137 기타 HA 환경 구성 시 데이터 복제 지연이 발생하는 경우 ECHO(7) port를 확인하자. 손승일 2012.11.29 28218
136 ODBC/OLEDB VB에서 CUBRID ODBC/OLEDB 사용하는 방법 및 샘플코드. file seongjoon 2012.06.30 28218
135 Java CUBRID와 Oracle의 Clob 타입 사용 방법 비교. seongjoon 2012.06.30 56727
134 Install CUBRID기반으로 XE 운영하기 – 설치가이드 file cubebridge 2012.04.13 33209
133 기타 select .. for update 처리를 위한 Stored Procudure 등록 및 사용법 남재우 2011.12.10 26522
132 Install LINUX CUBRID 4.0 매니저 설치방법 file 정만영 2011.07.19 24942
131 Install CUBRID Ubuntu Launchpad Installation 방법 file 정만영 2011.07.19 31011
130 기타 자주 발생하는 큐브리드(cubrid) 에러 메시지 정리 admin 2011.07.14 84646
129 기타 [주의사항] CUBRID에서의 BLOB/CLOB 사용시 백업 및 복구에 대한 주의 점 admin 2011.07.14 57185
128 기타 CUBRID 매니저 R3.1에서 웹호스팅 서버의 CUBRID R2.1 접속하는 방법 (큐브리드 매니저에서 다른 버전의 큐브리드 서버 접속 방법) file admin 2011.07.14 33207
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