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


List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
26 Linux CUBRID서비스 시스템 재구동시 자동 구동/종료 방법 1 admin 2009.04.01 29804
25 기타 CUBRID 2008 + XE 설치 가이드 [R1.x] Prototype 2009.03.13 34326
24 Install AutoSet 4.3.2를 이용한 APACHE+PHP+CUBRID 2008 설치 가이드 seongjoon 2009.03.13 38974
23 Install APC_Install-tools 구성 및 설치 방법 file Prototype 2009.03.13 30614
22 기타 CUBRID 개발 로드맵 admin 2008.12.27 40321
21 Install Windows 에서 CUBRID 설치하기 file 남재우 2008.11.22 43644
20 Install UNIX/LINUX 에서 CUBRID 설치하기 남재우 2008.11.22 37529
19 Install CUBRID6.6 이상에서 CUBRID2008 로 데이터베이스 이전 하기 3 남재우 2008.11.21 26445
18 PHP LINUX에서 PHP 사용하기 file admin 2008.11.21 42495
17 PHP LINUX에서 PHP 사용하기 - phpize를 이용한 설치 1 admin 2008.11.21 37461
16 PHP windows 상에서 PHP 설치하기 file admin 2008.11.21 51804
15 기타 제로보드 XE 사용하기 file admin 2008.11.21 33068
14 기타 MD5 암호화 모듈 file admin 2008.11.21 38593
13 CCI/DB API embedded sql 사용법 admin 2008.11.21 29734
12 Java DBCP 사용법 1 admin 2008.11.21 48371
11 기타 csql(SQL 실행기) 사용법 1 admin 2008.11.21 26557
10 기타 보안 설정 admin 2008.11.21 22838
9 기타 CUBRID와 타 데이터베이스 기능 비교 9 admin 2008.11.21 36163
8 기타 시스템 카다로그 2 admin 2008.11.21 22333
7 Linux 복제 따라하기 admin 2008.11.21 29096
Board Pagination Prev 1 ... 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