본문 바로가기

JSP/BLOB-Text

BLOB-Text example

BLOB 자료형을 이용하여 텍스트 파일을 데이터베이스에 입/출
참고: http://micropilot.tistory.com/category/Java%20SE/JDBC%20Clob,%20Blob

<%@ page contentType="text/html;charset=KSC5601" import="java.sql.*" %>
<%
 Connection conn = null;
 PreparedStatement pstmt = null;

 String jdbc_driver = "oracle.jdbc.OracleDriver";
 String db_url = "jdbc:oracle:thin:@localhost:1521:ORA9I";
 
 try{
  Class.forName(jdbc_driver);
  conn = DriverManager.getConnection(db_url,"cwisky","cwisky");
  /*create table mytable (bfile BLOB, cfile CLOB) 으로 테이블을 생성한 후에....
  * 바이너리 데이터 컬럼에 바이너리 데이터를 저장한다.
  */
  pstmt = conn.prepareStatement("insert into mytable (bfile) values(?)");
  byte[] buf = "some data".getBytes();
  pstmt.setBytes(1, buf);
  pstmt.executeUpdate();
  pstmt.close();
  /**/
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
        while (rs.next()) {
            // 바이너리 데이터를 저장하고 있는 컬럼으로부터 데이터를 가져온다
            byte[] bytes = rs.getBytes("bfile");
   out.print(new String(bytes));
        }
  rs.close();
  stmt.close();
  conn.close();
 }
 catch(Exception e) {
  out.println(e);
 }
%>