找了不少 BLOB的資料 不過沒有幾個可以調試成功 把 自己改他們的代碼發出來 大家可看看
寫入ORACLE資料庫
package com;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.sql.BLOB;
public class Jdbctest {
public static void main(String[] args) throws SQLException, IOException {
dbBean db = new dbBean();//自己的串連類
Connection con = db.getConnect();
con.setAutoCommit(false);
PreparedStatement preStmt = con.prepareStatement("insert into testblob (id,blobs) values (?,?)");
preStmt.setInt(1, 1);
preStmt.setBlob(2, BLOB.empty_lob());
preStmt.executeUpdate();
preStmt.close();
preStmt = con.prepareStatement("select blobs from testblob where id=? for update");
preStmt.setInt(1, 1);
ResultSet rest = preStmt.executeQuery();
rest.next();
BLOB blobb = (BLOB) rest.getBlob(1);
FileInputStream instream = new FileInputStream("E:/test.txt");
OutputStream outstream = blobb.getBinaryOutputStream();
byte[] buf = new byte[1024000];
int len;
while ((len = instream.read(buf)) > 0) {
outstream.write(buf, 0, len);
}
instream.close();
outstream.close();
preStmt = con.prepareStatement("update testblob set blobs=? where id=?");
preStmt.setBlob(1, blobb);
preStmt.setInt(2, 1);
preStmt.executeUpdate();
preStmt.close();
con.commit();
con.close();
}
}
讀出來 寫入檔案
package com;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.sql.BLOB;
public class readfile {
public static void main(String[] args) throws SQLException, IOException
{
File out=new File("E:/outtest.txt");
java.io.FileOutputStream fout=new FileOutputStream(out);
byte b[]=null;
dbBean db = new dbBean();
Connection con = db.getConnect();
con.setAutoCommit(false);
PreparedStatement preStmt = con.prepareStatement("select blobs from testblob where id=? for update");
preStmt.setInt(1, 1);
ResultSet rest = preStmt.executeQuery();
if (rest.next())
{
oracle.sql.BLOB blob2=(BLOB)rest.getBlob(1);
System.out.println("blob2 length:"+blob2.length());
b=blob2.getBytes(1,(int)blob2.length());
}
fout.write(b,0,b.length);
}
}
在ORACLE9。0。2調試通過
很苦悶的事情 昨天 搞了一天 絲毫沒有什麼結果 今天上午老到公司 看了看 書 居然搞定之 比較HAPPY 特地發出來大家欣賞一下