如何在oracle里面操作BLOB

import Java.io.*;
import java.util.*;
import java.sql.*;
import Oracle.JDBC.driver.*;
import java.text.*;
//
// @author: guo chang guo
// @time: 2003 08 17
//
// this program shows how to put a very big bytes array into oracle table(long raw)
// you can not use PreparedStatement's method setBytes, because setBytes can not
// support very big bytes array, you must use PreparedStatement's method setBinaryStream.
// if you want to use this method, you must transfer your bytes array into inputstream,
// in this example, we use ByteArrayInputStream which can be constructed by a bytes array.
//
// @copyright(R) cvicse
//

//
//the definition of the oracle table :CREATE TABLE  bin_data (name VARCHAR(30),data long raw ).
//you can not define the table: CREATE TABLE  bin_data (name VARCHAR(30),data blob )
//

//
//we have test this program in oracle 9.2 the file size is 31M
//

public class test
{
 public static void main(String args[]) throws java.io.IOException,java.sql.SQLException
 {
  try
  {
            Class.forName("oracle.jdbc.driver.OracleDriver");
    }
    catch (java.lang.ClassNotFoundException e)
    {
            System.err.print("ClassNotFoundException: " + e.getMessage());
    }

  try
  {
   //open a file, put the content of file into a bytes array
   
   File files = new File("c://temp//JDK140.chm");
   FileInputStream fis=new FileInputStream(files);
    System.out.println(files.getName());
   System.out.println((int)files.length());

         byte[] fl = new byte[(int)files.length()];
   System.out.println(fl.length);
   fis.read(fl);
  
  //use this bytes array to construct a InputStream 
  
         ByteArrayInputStream  kk = new ByteArrayInputStream(fl) ;

  
  //connect the oracle database
  
       Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@eaiserver01:1521:orcl", "system", "admin");  
   PreparedStatement ps = conn.prepareStatement("INSERT INTO bin_data (name,data)" +"VALUES (?, ?)");

  
  //set parameters
  
   ps.setString(1,files.getName());
      ps.setBinaryStream(2, kk,(int)files.length());
  
  
  //execute
     
   ps.executeUpdate();
   fis.close();
   ps.close();
  
  // read from the table
  
   int bytesRead = 0;
   int byteSum = 0;
   byte[] buffer = new byte[8 * 1924];
   FileOutputStream fis2 = new FileOutputStream("c://temp//hi.chm");

  
   PreparedStatement ps2 = conn.prepareStatement(  "select data from bin_data");
   ResultSet rs = ps2.executeQuery();
   if (rs != null)
   {
    while(rs.next())
    {
     InputStream is = rs.getBinaryStream(1);
     while ((bytesRead = is.read(buffer)) != -1)
      {
               byteSum += bytesRead;
               fis2.write(buffer, 0, bytesRead);
      }
      fis2.close( );
    }
   rs.close( );
   }
   ps2.close( );
}
catch(Exception e)
  {
   System.out.println("errror :"+e.toString() );
   e.printStackTrace();
  
  }
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值