Background Image
Java
2016.06.28 03:11

CUBRID에서 Java AddBatch 사용

조회 수 8359 추천 수 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. CUBRID Migration방법(by unloaddb & loaddb)

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

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

    Date2015.06.16 CategoryPHP By이용미 Views10664
    Read More
  4. CUBRID isolation level & Dirty Read

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Date2011.07.14 Category기타 Byadmin Views31507
    Read More
  20. 동시 접속자에 따른 파라미터 설정

    Date2011.07.14 Category기타 Byadmin Views25684
    Read More
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