public BlobTest() { con = getConnection(); try { stmt = con.createStatement(); } catch (SQLException e) { e.printStackTrace(); } }
public static void main(String[] args) { BlobTest bt = new BlobTest(); bt.insertBolb(); bt.getBlob(); bt.close(); }
private void insertBolb() { try { //insert an empty blob String sql1 = "insert into blobtest values ('001',empty_blob())"; stmt.executeUpdate(sql1);
//query the empty blob for update String sql2 = "select filedata from blobtest where fileid = '001' for update"; ResultSet rs = stmt.executeQuery(sql2);
//read the empty bolb while(rs.next()) { oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("filedata"); OutputStream os = blob.getBinaryOutputStream();
//read outter file InputStream is = new FileInputStream(new File("test.txt"));
//write inputstream to outputstream byte[] b = new byte[blob.getBufferSize()]; int len = 0; while((len = is.read(b)) != -1) { os.write(b, 0, len); } is.close(); os.flush(); os.close(); con.commit(); }
http://download.csdn.net/user/JadoNet
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.38.243:1521:orcl";
String username = "fm";
String password = "manager";
Connection con = null;
Statement stmt = null;
public BlobTest() {
con = getConnection();
try {
stmt = con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
BlobTest bt = new BlobTest();
bt.insertBolb();
bt.getBlob();
bt.close();
}
private void insertBolb() {
try {
//insert an empty blob
String sql1 = "insert into blobtest values ('001',empty_blob())";
stmt.executeUpdate(sql1);
//query the empty blob for update
String sql2 = "select filedata from blobtest where fileid = '001' for update";
ResultSet rs = stmt.executeQuery(sql2);
//read the empty bolb
while(rs.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("filedata");
OutputStream os = blob.getBinaryOutputStream();
//read outter file
InputStream is = new FileInputStream(new File("test.txt"));
//write inputstream to outputstream
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while((len = is.read(b)) != -1) {
os.write(b, 0, len);
}
is.close();
os.flush();
os.close();
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void getBlob() {
try {
//get blob from db
String sql = "select filedata from blobtest where fileid = '001'";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
//get inputstream from resultset
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("filedata");
InputStream is = blob.getBinaryStream();
//create fileoutputstream
OutputStream os = new FileOutputStream(new File("out.txt"));
//write to file
byte[] b = new byte[1024];
int len;
while((len = is.read(b)) != -1) {
os.write(b, 0, len);
}
os.flush();
os.close();
is.close();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private Connection getConnection() {
try {
Class.forName(driver);
con = DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
private void close() {
if(stmt != null) {
try {
stmt.close();
} catch(SQLException e) {
e.printStackTrace();
} finally {
if(con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}}