自己改过的BLOB文件的插入和读取JAVA代码

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值