您的位置:首页 >> 编程开发 >> Java >> 数据库 >> 正文
数据库 RSS
 

如何在oracle里面操作BLOB

http://www.rdxx.com 02年04月01日 11:20 互联网 我要投稿

关键词: Oracle , blob , 操作 , LOB

看了下面的程序就知道了

如果有问题请回: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();

共2页  1 2


 
 
标签: Oracle , blob , 操作 , LOB 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站