CSDN博客

img gcg

如何在oracle里面操作BLOB

发表于2003/8/18 1:11:00  708人阅读

看了下面的程序就知道了

如果有问题请回:cgguo@163.net

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

相关文章推荐

img
取 消
img