시소당
import java.io.*;
import java.sql.*;
import oracle.jdbc.OracleResultSet;
import oracle.sql.*;
/**
*
* create table tbl_blob (
* id varchar2(30) primary key,
* filename varchar2(50) not null,
* filedat blob
* );
*
*/
public class BLOBTest {
public static final String DRIVER ="oracle.jdbc.driver.OracleDriver";
public static final String URL="jdbc:oracle:thin:@www.mycompany.com:1521:ORCL";
public static final String USER = "scott";
public static final String PASS = "tiger";
private Connection getConnection(){
Connection con = null;
try{
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, USER, PASS);
}catch(Exception e){
e.printStackTrace();
}
return con;
}
private void close(ResultSet rs, Statement stmt, Connection con ){
if(rs != null) { try{ rs.close();}catch(Exception e){}}
if(stmt != null) { try{ stmt.close();}catch(Exception e){}}
if(con != null) { try{ con.close();}catch(Exception e){}}
}
public void insertEx(String id, String fileName){
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String query1 = " INSERT INTO tbl_blob (id, filename, filedat) VALUES(?,?,empty_blob())";
String query2 = " SELECT filedat FROM tbl_blob WHERE id=? FOR UPDATE ";
try{
con = getConnection();
con.setAutoCommit(false);
stmt = con.prepareStatement(query1);
stmt.setString(1, id);
stmt.setString(2, fileName);
int insertResult = stmt.executeUpdate();
stmt.close();
stmt = null;
if(insertResult > 0){
stmt = con.prepareStatement(query2);
stmt.setString(1, id);
rs = stmt.executeQuery();
if(rs.next() ){
FileInputStream fin = new FileInputStream(fileName);
byte[] buffer = new byte[1024*8];
//10g용 코드
//Blob blob = rs.getBlob(1);
//OutputStream os = blob.setBinaryStream(0);
//9i용 코드
BLOB blob = ((OracleResultSet)rs).getBLOB(1);
OutputStream os = blob.getBinaryOutputStream();
while(true){
int count = fin.read(buffer);
if(count == -1){
break;
}
os.write(buffer);
}
os.close();
fin.close();
System.out.println("파일 입력 완료");
}
}
con.commit();
}catch(Exception e){
e.printStackTrace();
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
close(rs, stmt, con);
}
}
public void readEx(String id){
String query = "SELECT filename, filedat FROM tbl_blob where id= ? ";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
con = getConnection();
pstmt = con.prepareStatement(query);
pstmt.setString(1,id);
rs = pstmt.executeQuery();
if(rs.next()){
Blob blob = rs.getBlob(2);
InputStream in = blob.getBinaryStream();
FileOutputStream fos = new FileOutputStream(rs.getString(1));
byte[] buffer = new byte[1024*8];
while(true){
int count = in.read(buffer);
if(count == -1){
break;
}
fos.write(buffer,0, count);
}
fos.close();
in.close();
System.out.println("파일 기록 완료");
}
}catch(Exception e){
e.printStackTrace();
}finally{
close(rs, pstmt, con);
}
}
public static void main(String[] args){
BLOBTest bt = new BLOBTest();
//bt.insertEx("test1", "test.jpg");
bt.readEx("test1");
}
}
출처 : http://blog.naver.com/cookie_00?Redirect=Log&logNo=80032756447