Java
2016.06.28 03:11

CUBRID에서 Java AddBatch 사용

조회 수 10821 추천 수 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. 가상머신 환경에서 리눅스 및 큐브리드 설치 가이드

    Date2015.07.14 CategoryLinux By이경오 Views15219
    Read More
  2. CUBRID Migration방법(by unloaddb & loaddb)

    Date2015.07.01 Category기타 By주현 Views24950
    Read More
  3. 콘솔에서 마이그레이션 하기.

    Date2015.06.19 Category기타 By권호일 Views15616
    Read More
  4. CUBRID HA 환경에서 Apache-PHP 설정 가이드

    Date2015.06.16 CategoryPHP By이용미 Views11963
    Read More
  5. CUBRID isolation level & Dirty Read

    Date2015.05.29 Category기타 By정만영 Views13355
    Read More
  6. Windows 에 설치 된 JRE 설치 위치 확인 하기.

    Date2015.05.28 CategoryWindows By성진 Views13953
    Read More
  7. CUBRID와 MyBATIS 빠르게 연동하기

    Date2015.05.07 CategoryJava By김승훈 Views21058
    Read More
  8. CUBRID Linux 부팅시 자동시작 서비스 설정 방법

    Date2013.09.16 CategoryLinux By정만영 Views27059
    Read More
  9. phpize를 이용한 PHP 모듈 연동 방법(CUBRID2008 R4.3 버전)

    Date2013.05.16 CategoryPHP By이용미 Views22150
    Read More
  10. Apache, PHP 설치 방법

    Date2013.05.16 CategoryPHP By이용미 Views42822
    Read More
  11. HA 환경 구성 시 데이터 복제 지연이 발생하는 경우 ECHO(7) port를 확인하자.

    Date2012.11.29 Category기타 By손승일 Views28244
    Read More
  12. VB에서 CUBRID ODBC/OLEDB 사용하는 방법 및 샘플코드.

    Date2012.06.30 CategoryODBC/OLEDB Byseongjoon Views28231
    Read More
  13. CUBRID와 Oracle의 Clob 타입 사용 방법 비교.

    Date2012.06.30 CategoryJava Byseongjoon Views56754
    Read More
  14. CUBRID기반으로 XE 운영하기 – 설치가이드

    Date2012.04.13 CategoryInstall Bycubebridge Views33226
    Read More
  15. select .. for update 처리를 위한 Stored Procudure 등록 및 사용법

    Date2011.12.10 Category기타 By남재우 Views26536
    Read More
  16. LINUX CUBRID 4.0 매니저 설치방법

    Date2011.07.19 CategoryInstall By정만영 Views24963
    Read More
  17. CUBRID Ubuntu Launchpad Installation 방법

    Date2011.07.19 CategoryInstall By정만영 Views31029
    Read More
  18. 자주 발생하는 큐브리드(cubrid) 에러 메시지 정리

    Date2011.07.14 Category기타 Byadmin Views84696
    Read More
  19. [주의사항] CUBRID에서의 BLOB/CLOB 사용시 백업 및 복구에 대한 주의 점

    Date2011.07.14 Category기타 Byadmin Views57222
    Read More
  20. CUBRID 매니저 R3.1에서 웹호스팅 서버의 CUBRID R2.1 접속하는 방법 (큐브리드 매니저에서 다른 버전의 큐브리드 서버 접속 방법)

    Date2011.07.14 Category기타 Byadmin Views33219
    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