TECH
QUESTION
자주하는 질문답변 입니다.
Oracle
작성자 | 유건데이타 | 등록일 | 2015-05-15 |
제목 | JDBC를 이용해서 BLOB COLUMN 에 파일 INSERT하는 SAMPLE | ||
---|---|---|---|
jDBC를 이용해서 BLOB COLUMN 에 파일 INSERT하는 SAMPLE
==================================================== Purpose ------- JDBC를 이용해서 BLOB COLUMN 에 파일 INSERT하는 방법을 알아 봅니다. Explanation ----------- - 다음 sample은 Oracle DB v8.1.7에서 JDBC(V8.1.7)을 이용해서 TEST한 것입니다. Example -------- import java.sql.*; import java.io.*; import java.util.*; import oracle.jdbc.driver.*; //needed for new CLOB and BLOB classes import oracle.sql.*; public class Insert_File_BLOB { public static void main (String args []) throws Exception { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection ("jdbc:oracle:oci8:@krint-7", "scott", "tiger"); conn.setAutoCommit (false); Statement stmt = conn.createStatement (); // Drop the basic_lob_table try { stmt.execute ("drop table basic_lob_table"); } catch (SQLException e) { // An exception could be raised here if the table did not exist already // but we gleefully ignore it } // Create a table containing a BLOB and a CLOB stmt.execute ("create table basic_lob_table (x varchar2 (30), b blob, c clob)"); // Populate the table stmt.execute ("insert into basic_lob_table values ('one',empty_blob(), 'clob column data !!!')"); // Select the lobs OracleResultSet rset = (OracleResultSet)stmt.executeQuery("select b from basic_lob_table where x='one' for update"); while (rset.next ()) { // Get the lobs BLOB blob = ((OracleResultSet)rset).getBLOB (1); try { File file = new File("c:\\temp\\bokim.txt"); long fileLength = (long) file.length(); System.out.println("File Size : " + fileLength + " bytes"); FileInputStream instream = new FileInputStream(file); OutputStream outstream = blob.getBinaryOutputStream(); int size = blob.getBufferSize(); System.out.println("BufferSize: " + size + " bytes (#)\n"); byte[] buffer = new byte[size]; int length = -1; while ((length = instream.read(buffer)) != -1) { outstream.write(buffer, 0, length); System.out.print("#"); } System.out.println(); instream.close(); outstream.close(); System.out.println("\nUpdate Done."); } catch (java.io.FileNotFoundException fe) { System.err.println ("thrown: java.io.FileNotFoundException\n"); fe.printStackTrace (); System.err.println (fe.getMessage ()); } } stmt.close(); conn.commit(); conn.close(); } } 결과 ---- SQL> select dbms_lob.getlength(b) from basic_lob_table; no rows selected SQL> / DBMS_LOB.GETLENGTH(B) --------------------- 4328 |
Comment | |||
---|---|---|---|
등록된 코멘트가 없습니다. |