2011년 11월 4일 금요일
오라클 blob, long raw java 예제
오라클 blob 예제
***********************************************************
package test;
import oracle.sql.BLOB;
import java.io.*;
import java.sql.*;
public class BlobTest {
public static void blobInsertTest() throws SQLException, IOException {
ResultSet rs = null;
PreparedStatement pstmt = null;
Connection conn = null;
String query = "insert into test_blob(seq,file_name,lob_data) values(?,?,empty_blob())";
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:cano01", "icrsadmin", "icrs00");
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(query);
pstmt.setInt(1, 1); //seq
pstmt.setString(2, "file_name"); //file_name
pstmt.executeUpdate();
pstmt.close();
String query2 = "select lob_data from test_blob for update";
pstmt = conn.prepareStatement(query2);
rs = pstmt.executeQuery();
if (rs.next()) {
BLOB myblob = (BLOB) rs.getBlob(1);
OutputStream outstream = myblob.getBinaryOutputStream();
byte[] data = new String("insert blob data").getBytes();
outstream.write(data, 0, data.length);
outstream.close();
}
rs.close();
pstmt.close();
conn.commit();
conn.setAutoCommit(true);
} finally {
if (conn != null) conn.close();
}
}
public static void main(String[] args) throws SQLException, IOException {
blobInsertTest();
blobSelectTest();
}
private static void blobSelectTest() throws SQLException, IOException {
ResultSet rs = null;
PreparedStatement pstmt = null;
Connection conn = null;
String query = " select lob_data from test_blob";
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:cano01", "icrsadmin", "icrs00");
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
if (rs.next()) {
BLOB myblob = (BLOB) rs.getBlob("lob_data");
InputStream in = myblob.getBinaryStream();
byte[] buffer = new byte[1024];
StringBuffer sBuff = new StringBuffer();
while(in.read(buffer) != -1) {
sBuff.append(new String(buffer));
}
System.out.println("Result BLOB data 2 is "+sBuff.toString());
in.close();
}
} finally {
if (conn != null) conn.close();
}
}
}
**************************************************
오라클 LONG RAW 예제
**************************************************
package test;
import oracle.sql.BLOB;
import java.io.*;
import java.sql.*;
public class BlobTest {
public static void main(String[] args) throws Exception {
// insertData();
getImage("c:\test2.jpg");
}
private static void insertData() throws Exception {
PreparedStatement pstmt = null;
Connection conn = null;
String query = "INSERT into RWEB_REPORT(SEQUENCE_ID, FILENAME, DATAFILE, UPD_TIMESTAMP, FLAG) values(?, ?, ?, sysdate, ?)";
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:cano01", "icrsadmin", "icrs00");
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(query);
int idx = 0;
pstmt.setInt(++idx, 1); //seq
pstmt.setString(++idx, "file_name"); //file_name
File file = new File("C:\test.jpg");
pstmt.setBinaryStream(++idx,new FileInputStream(file),(int)file.length());
pstmt.setInt(++idx, 1); //seq
pstmt.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
} finally {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
}
private static void getImage(String fullName) throws SQLException, IOException {
PreparedStatement pst = null;
Connection conn = null;
InputStream gifdata = null;
try{ // Query the logo of the selected airline
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:cano01", "icrsadmin", "icrs00");
String query = "select DATAFILE FROM RWEB_REPORT where SEQUENCE_ID = 1";
pst = conn.prepareStatement(query);
ResultSet result = pst.executeQuery();
if( result.next() ) {
gifdata = result.getBinaryStream(1);
File gifFile = new File(fullName);
FileOutputStream file= new FileOutputStream(gifFile);
int chunk=0;
while( (chunk = gifdata.read()) != -1) {
file.write(chunk);
}
file.flush();
file.close();
}
}finally{
if (pst != null) pst.close();
if (conn != null) conn.close();
}
}
}
**************************************************
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기