|
import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.*;
public class BOLSelect {
public static void main(String args[]) throws SQLException {
Connection con = null;
Statement stm = null;
ResultSet rs = null;
try {
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","test","test");
stm = con.createStatement();
// レコードをselectする。
rs = stm.executeQuery("select blob_id,blob_data from blob_sample where blob_id = 'B01'");
rs.next();
// 列「blob_data」のロケータからInputStreamを取得する。
oracle.sql.BLOB bmp = ((OracleResultSet)rs).getBLOB("blob_data");
InputStream in = bmp.getBinaryStream();
// バイナリデータサイズを取得する。
int size = bmp.getBufferSize();
byte [] buffer = new byte[size];
// 出力するファイル「TEST.BMP」をオープン
FileOutputStream out = new FileOutputStream("TEST.BMP");
int length = -1;
while ((length = in.read(buffer)) != -1) {
// 読み込んだ列「blob_data」をバイナリファイル「TEST.BMP」に書き込む
out.write(buffer,0,length);
}
in.close();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if ( rs != null ) rs.close();
if ( stm != null ) stm.close();
if ( con != null ) con.close();
}
}
} |