找了不少 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 特地发出来大家欣赏一下