<%@ page language="java" import="oracle.jdbc.driver.*,javax.sql.*,java.sql.*,java.io.*,common.log.*" contentType="text/html; charset=euc-kr" %>
<%@ page import="oracle.sql.CLOB"%>
<%
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
oracle.sql.CLOB clob = null;
String contents = "";
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:test","admin","admin00"); //dev
con.setAutoCommit(false);
Log.logFileWrite("========== DB CONNECT ==========");
////////////// CLOB READ //////////////
String sbQuery = " SELECT CONTENTS \n" +
" FROM BOARD \n" +
" WHERE BOARD_SEQ = 1001 ";
stmt = con.prepareStatement(sbQuery);
rs = stmt.executeQuery();
Reader reader = null;
int rcnt=0;
char[] char_array;
if(rs.next()){
clob = ((OracleResultSet)rs).getCLOB("CONTENTS");
reader = clob.getCharacterStream();
char_array = new char [(int)(clob.length())];
rcnt = reader.read(char_array);
contents = (new String(char_array));
reader.close();
}
rs.close();
stmt.close();
Log.logFileWrite("========== CLOB_CONTENTS PRINT ==========");
Log.logFileWrite("\n" + contents);
////////////// CLOB WRITE //////////////
String filePath = "C:\\Temp\\clob.txt"; //내용이 많아서 txt로 만들어 읽어옴
BufferedReader in = new BufferedReader(new FileReader(filePath));
String temp = "";
while((contents = in.readLine()) != null){
temp += contents + "\n";
}
Log.logFileWrite("~~~~~~~~~~ UPDATE 할 CONTENTS의 내용 ~~~~~~~~~~");
Log.logFileWrite("CONTENTS \n" + temp);
String queryUpdate = " UPDATE BOARD \n" +
" SET CONTENTS = EMPTY_CLOB() \n" + //EMPTY_CLOB()로 공간 확보
" WHERE BOARD_SEQ = 1001 ";
stmt = con.prepareStatement(queryUpdate);
int cnt = stmt.executeUpdate();
stmt.close();
Log.logFileWrite("<========== cnt : " + cnt + " ==========>");
if(cnt > 0){ //정상적으로 업데이트 성공시(clob 공간 확보)
String querySelect = " SELECT CONTENTS \n" +
" FROM BOARD \n" +
" WHERE BOARD_SEQ = 1001 ";
stmt = con.prepareStatement(querySelect);
rs = stmt.executeQuery();
if(rs.next()){
clob = ((OracleResultSet)rs).getCLOB(1);
Writer writer = clob.getCharacterOutputStream();
//writer...
writer.write(temp);
writer.flush(); //flush를 해줘야 함
writer.close();
rs.close();
stmt.close();
}
//con.commit();
}
//con.commit(); //문제 없을 시 commit
}catch(Exception e) {
Log.logFileWrite("Error : " + e.toString());
con.rollback();
}finally {
if(con != null)
con.close();
out.println("<script>alert('log file view~~~');</script>");
out.println("<br><br>");
out.println(""+contents);
}
%>
CLOB 데이터 조작할 일이 있어(달랑 1개..ㅡㅡ;;) 간단하게 만든 것입니다..
제대로 할려면...컨넥션부터 예외처리까지 제대로 해야하지만...일회성이라...ㅡㅡ;;
중요한것은 안쪽의 로직으로 try ~ catch안의 로직을 잘 이해하는게~~^^
'프로그램 > Java & Jsp' 카테고리의 다른 글
throw & throws in Java (0) | 2012.03.12 |
---|---|
MultipartRequest를 이용하여 업로드구현하기 (0) | 2009.07.30 |
JSP 액션태그 - 간략정리 (0) | 2009.04.29 |
JSP의 액션 태그 알아보기 - 기타 액션 태그 (0) | 2009.04.29 |
JSP의 액션 태그 알아보기 - forward 태그 (0) | 2009.04.29 |