Background Image
Java
2016.06.28 03:11

CUBRID에서 Java AddBatch 사용

조회 수 8360 추천 수 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. [linux] wget으로 제품 다운로드 시 "wget: unable to resolve host address ‘ftp.cubrid.org’" 해결방

    Date2021.07.02 CategoryInstall By큐브리드_김주현 Views2226
    Read More
  2. [10.2 path] - 생성된 view구문을 'show create view' 수행 시 정상적으로 출력되지 않던 이슈 patch

    Date2020.11.09 Category기타 By큐브리드_김주현 Views873
    Read More
  3. ERwin을 이용한 CUBRID 포워드 엔지니어링

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

    Date2020.07.13 Category기타 By민순 Views3445
    Read More
  5. <주의> 생성한 DB볼륨을 절대! 삭제하지 말자

    Date2019.09.30 CategoryLinux By큐브리드_김주현 Views1583
    Read More
  6. 큐브리드10.1 에서 윤초 지원 옵션 사용하기

    Date2017.09.13 Category기타 By최광일 Views1411
    Read More
  7. 따라하면 쉬운 compactdb 사용법

    Date2017.07.01 Category기타 By허서진 Views2522
    Read More
  8. 리눅스에서 top 명령어를 통한 CPU 점유율 확인 및 측정하기

    Date2017.06.02 CategoryLinux By정훈 Views80444
    Read More
  9. JDBC를 사용한 다중화 구성 SELECT Query 부하 분산 가이드

    Date2017.03.30 CategoryJava By윤준수 Views3939
    Read More
  10. 다중컬럼 조건에 대한 인라인뷰 처리방안

    Date2016.12.27 Category튜닝 By박동윤 Views6005
    Read More
  11. tomcat8.0(DBCP2)과 CUBRID 연동하기

    Date2016.07.01 CategoryJava By손승일 Views15570
    Read More
  12. LIMIT절을 사용하여 SQL문을 간결하게 작성하고, 부분범위 처리를 유도하자.

    Date2016.06.29 Category튜닝 By권호일 Views15485
    Read More
  13. CUBRID에서 Java AddBatch 사용

    Date2016.06.28 CategoryJava By엄기호 Views8360
    Read More
  14. PyCharm을 이용한 CUBRID, Django 연동 가이드

    Date2016.04.11 Category기타 By진우진 Views8148
    Read More
  15. CSQL 인터프리터 사용방법

    Date2016.03.03 Category기타 By정만영 Views16581
    Read More
  16. 데이터 확인에 정규표현식을 사용 해 보자.

    Date2016.03.01 Category기타 By성진 Views12728
    Read More
  17. 리소스를 제한(limits.conf) 하여 DB서버를 관리하자

    Date2015.12.31 CategoryLinux By주현 Views26604
    Read More
  18. CUBRID Migration Toolkit을 이용한 단계별 마이그레이션 진행 방법

    Date2015.12.15 Category기타 By진우진 Views9527
    Read More
  19. 알고 보면 쉬운 cubrid lockdb 유틸리티

    Date2015.12.08 Category기타 By김승훈 Views12055
    Read More
  20. 가상머신 환경에서 리눅스 및 큐브리드 설치 가이드

    Date2015.07.14 CategoryLinux By이경오 Views13392
    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