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. 멀티미디어 데이터 관리를 위한 CUBRID GLO Method 활용

    Date2009.12.11 Category기타 Byjanus Views16191
    Read More
  2. QTADO를 이용하여 CUBRID접속하기

    Date2009.12.10 CategoryODBC/OLEDB Bycubebridge Views21489
    Read More
  3. CUBRID 개발 가이드

    Date2009.12.09 Category기타 Byjanus Views18368
    Read More
  4. 서비스 오픈을 위한 CUBRID 구성 가이드

    Date2009.11.28 Category기타 By손승일 Views23204
    Read More
  5. CUBRID R2.0 ISV(Independent Software Vendor)설치방법

    Date2009.11.20 CategoryInstall Bycubebridge Views27921
    Read More
  6. CUBRID 2008 R2.0의 CM변경사항

    Date2009.11.20 Category기타 Byjanus Views17045
    Read More
  7. CUBRID Java Stored Procedure와 DB Server Memory 관계

    Date2009.10.31 CategoryJava By손승일 Views40190
    Read More
  8. QuantumDB Eclipse Plugin을 사용하여 CUBRID연동하기

    Date2009.10.06 CategoryJava Bycubebridge Views28922
    Read More
  9. windows 환경에서 여러버젼의 CUBRID 설치하여 사용하기

    Date2009.09.16 CategoryWindows By남재우 Views26191
    Read More
  10. CUBRID HA introduction

    Date2009.08.28 Category기타 Byjanus Views20863
    Read More
  11. CUBRID2008 R2.0 실행계획 분석하기

    Date2009.08.27 Category기타 Bycubebridge Views21917
    Read More
  12. CUBRID 2008 + XE 설치 가이드 [R2.0]

    Date2009.08.27 Category기타 ByPrototype Views16573
    Read More
  13. CUBRID 설치 및 매니저 구동하기(CUBRID 2008 R2.0)

    Date2009.08.18 CategoryInstall ByCUBRID_DEV Views30053
    Read More
  14. DB의 스키마와 데이터를 파일로 내려받기

    Date2009.08.17 Category기타 Byseongjoon Views20009
    Read More
  15. CUBRID 주요 명령 요약 정리

    Date2009.08.17 Category기타 By정만영 Views17187
    Read More
  16. CUBRID Manager login 안내

    Date2009.08.17 Category기타 Byseongjoon Views16201
    Read More
  17. CUBRID2008_보안설정[R2.0]

    Date2009.08.17 Category기타 Byseongjoon Views16272
    Read More
  18. CUBRID2008R1.x to 2.0 마이그레이션 가이드 (32bit 기준)

    Date2009.08.17 CategoryLinux By정만영 Views27205
    Read More
  19. CUBRID Tutorial (Unix/Linux)

    Date2009.08.17 CategoryLinux By정만영 Views27885
    Read More
  20. CUBRID2008R2.0 Windows BACKUP 자동화 설정하기

    Date2009.08.17 CategoryWindows By정만영 Views27942
    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