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();
        }
    }
}


  1. HA 환경 구성 시 데이터 복제 지연이 발생하는 경우 ECHO(7) port를 확인하자.

    Date2012.11.29 Category기타 By손승일 Views26445
    Read More
  2. ERwin을 이용한 CUBRID 포워드 엔지니어링

    Date2020.07.14 Category기타 By민순 Views2316
    Read More
  3. ERwin을 이용한 CUBRID 리버스 엔지니어링

    Date2020.07.13 Category기타 By민순 Views3462
    Read More
  4. DB의 스키마와 데이터를 파일로 내려받기

    Date2009.08.17 Category기타 Byseongjoon Views20009
    Read More
  5. DBCP 사용법

    Date2008.11.21 CategoryJava Byadmin Views48371
    Read More
  6. CUBRID주요 사용 함수 및 연산자 비교

    Date2009.07.28 Category기타 Bycubebridge Views23456
    Read More
  7. CUBRID인덱스 구성 전략

    Date2009.12.31 Category기타 By웁쓰 Views22193
    Read More
  8. CUBRID와 타DBMS의 python 사용방법 비교

    Date2009.05.19 Category기타 Bycubebridge Views29523
    Read More
  9. CUBRID와 타 데이터베이스 기능 비교

    Date2008.11.21 Category기타 Byadmin Views36163
    Read More
  10. CUBRID와 Oracle의 Clob 타입 사용 방법 비교.

    Date2012.06.30 CategoryJava Byseongjoon Views54561
    Read More
  11. CUBRID와 MyBATIS 빠르게 연동하기

    Date2015.05.07 CategoryJava By김승훈 Views19141
    Read More
  12. CUBRID에서의 BLOB/CLOB 타입 사용법

    Date2011.03.08 Category기타 Bycubebridge Views30453
    Read More
  13. CUBRID에서 log4sql을 사용하자

    Date2009.06.12 CategoryJava By정병주 Views44305
    Read More
  14. CUBRID에서 Java AddBatch 사용

    Date2016.06.28 CategoryJava By엄기호 Views8365
    Read More
  15. CUBRID서비스 시스템 재구동시 자동 구동/종료 방법

    Date2009.04.01 CategoryLinux Byadmin Views29805
    Read More
  16. CUBRID기반으로 XE 운영하기 – 설치가이드

    Date2012.04.13 CategoryInstall Bycubebridge Views30933
    Read More
  17. CUBRID_기술문서_CUBRID PHP 함수 unisql_ 계열 cubrid_로 변환하기

    Date2009.06.23 CategoryPHP By남재우 Views22355
    Read More
  18. CUBRID6.6 이상에서 CUBRID2008 로 데이터베이스 이전 하기

    Date2008.11.21 CategoryInstall By남재우 Views26445
    Read More
  19. CUBRID2008데이터타입, 함수와 힌트사용법 및 예약어

    Date2009.07.28 Category기타 Bycubebridge Views18844
    Read More
  20. CUBRID2008_보안설정[R2.0]

    Date2009.08.17 Category기타 Byseongjoon Views16272
    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