작성자 : shin-gosoo(hchshin@chol.com)
작성일 : 2007.10.04
새창 보기
초보자를 위한 JDBC를 실습 3탄입니다. (톰캣 , My-sql 환경하에서)
이번 강의는 JDBC 프로그래밍중 대용량 데이터 입력방법에 대한 내용입니다.
이번 강의를 위해 사전에 설치할 것들
톰캣 설치(5.0.28)
Mysql 설치(5.0.x)
WTP 설치(1.5.3)
JDBC 환경설정 참조
목차
대용량 데이터 입력방법
실습예제 다운로드
1. 대용량 데이터 입력방법
Statement 문을 이용한 방법(ex2.jsp)
<%@ page language="java" contentType="text/html; charset=MS949" %> <%@ page import="java.sql.*" %> <%@ page import="java.text.*" %> <%@ page import="java.util.*" %> <%@ page import="org.javamaster.comm.db.*" %> <%! ... private void InsertByStatement() throws Exception { Connection con = null; Statement stmt = null; try{ con = ConnectionHelper.getConnection(); con.setAutoCommit(false); stmt = con.createStatement(); for (int i=1; i <= 3000; i++){ stmt.executeUpdate("insert into test (id, title, reg_date) values(NULL, '제목입니다-"+i+"', now()) "); } con.commit(); }catch(SQLException e){ if (con!=null) { try{ con.rollback(); } catch (Exception ignored){} } }finally{ if (stmt!=null) { try{ stmt.close(); } catch (Exception ignored){} } if (con!=null) { try{ con.setAutoCommit(false); } catch (Exception ignored){} try{ con.close(); } catch (Exception ignored){} } } } ... %>
위 예제는 3000건의 테스트 데이터를 DB에 넣기 위해 Statement를 이용하는 방법이다.
JDBC 프로그래밍에서 Statement 의 종류는 Statement, PreparedStatement, CallableStatement 이렇게 세종류가 있는데,
Statement 는 일반적인 Query를 전송하는 방식으로써 매번 파싱과정을 거쳐서 실행된다.
반면에 PreparedStatement 는 한번 실행된 SQL문은 미리 저장되어 있기 때문에 파싱과정 없이 바로 실행되므로,
자주 사용되는 SQL문이나 반복적인 SQL문은 PreparedStatement로 실행하는게 유리하다.
마지막으로 CallableStatement는 저장프로시저나 함수등을 호출할때 사용된다.(속도면에서는 가장 빠르다.^^)
CallableStatement는 자주 사용하지 않으므로 이번 예제에는 포함하지 않았다.
이번 예제는 3000건의 데이터를 반복적으로 넣기 때문에 트랜잭션처리를 하였다.(18, 27, 30, 37라인 참조)
반복적인 데이터 처리시 트랜잭션 처리를 하면 한건이라도 오류가 발생시 롤백되기 때문에 안정적일뿐만 아니라, 트랜잭션처리를 하면 커넥션의 autocommit을 false 시켜 놓고 마지막에 한번만 commit 시키기 때문에 속도도 몇배나 빨리 처리된다.
(Mysql에는 워낙 가볍고 좋은(?) DB라서 별차이가 나지 않지만 Oracle에서는 확실히 차이가 남)
PreparedStatement 문을 이용한 방법(ex2.jsp)
<%@ page language="java" contentType="text/html; charset=MS949" %> <%@ page import="java.sql.*" %> <%@ page import="java.text.*" %> <%@ page import="java.util.*" %> <%@ page import="org.javamaster.comm.db.*" %> <%! ... private void InsertByPrepareStatement() throws Exception { Connection con = null; PreparedStatement stmt = null; try{ con = ConnectionHelper.getConnection(); con.setAutoCommit(false); stmt = con.prepareStatement("insert into test (id, title, reg_date) values(NULL, ?, now()) "); for (int i=1; i <= 3000; i++){ stmt.setString(1,"제목입니다-"+i); stmt.executeUpdate(); } con.commit(); }catch(SQLException e){ if (con!=null) { try{ con.rollback(); } catch (Exception ignored){} } }finally{ if (stmt!=null) { try{ stmt.close(); } catch (Exception ignored){} } if (con!=null) { try{ con.setAutoCommit(false); } catch (Exception ignored){} try{ con.close(); } catch (Exception ignored){} } } } ... %>
위 예제는 3000건의 테스트 데이터를 DB에 넣기 위해 PreparedStatement를 이용하는 방법이다.
Statement 보다 빠른 속도를 보여준다.(Mysql에는 워낙 가볍고 좋은(?) DB라서 조금 빠르지만 Oracle에서는 확실히 차이가 남)
PreparedStatement 문을 Batch를 이용한 방법(ex2.jsp)
<%@ page language="java" contentType="text/html; charset=MS949" %> <%@ page import="java.sql.*" %> <%@ page import="java.text.*" %> <%@ page import="java.util.*" %> <%@ page import="org.javamaster.comm.db.*" %> <%! ... private void InsertByBatch() throws Exception { Connection con = null; PreparedStatement stmt = null; try{ con = ConnectionHelper.getConnection(); con.setAutoCommit(false); stmt = con.prepareStatement("insert into test (id, title, reg_date) values(NULL, ?, now()) "); for (int i=1; i <= 3000; i++){ stmt.setString(1,"제목입니다-"+i); stmt.addBatch(); } stmt.executeBatch(); con.commit(); }catch(SQLException e){ if (con!=null) { try{ con.rollback(); } catch (Exception ignored){} } }finally{ if (stmt!=null) { try{ stmt.close(); } catch (Exception ignored){} } if (con!=null) { try{ con.setAutoCommit(false); } catch (Exception ignored){} try{ con.close(); } catch (Exception ignored){} } } } ... %>
위 예제는 3000건의 테스트 데이터를 DB에 넣기 위해 PreparedStatement의 Batch를 이용하는 방법이다.
PreparedStatement 보다 빠른 속도를 보여준다.(Mysql에는 워낙 가볍고 좋은(?) DB라서 조금 빠르지만 Oracle에서는 확실히 차이가 남)