转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> ORACLE >> 正文
如何在oracle里面操作BLOB         ★★★★

如何在oracle里面操作BLOB

作者:闵涛 文章来源:闵涛的学习笔记 点击数:887 更新时间:2009/4/22 22:08:10

看了下面的程序就知道了

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


[系统软件]如何在Linux系统上进行快速磁带备份  [C语言系列]如何在Jbuilder9中使用SQLServer JDBC驱动
[VB.NET程序]如何在Visual Basic 6.0 中连接加密的Access数据库  [VB.NET程序]如何在vb 中用api函数代替winsock控件建立网络连接…
[Delphi程序]如何在delphi6中调试web程序  [Delphi程序]如何在Delphi中用代码来完成计算字段的创建
[Delphi程序]如何在 Listbox 上显示 In-place Tooltips  [Delphi程序]如何在Delphi应用程序中调用CHM文档
[Delphi程序]如何在Delphi中使用正则表达式  [Delphi程序]如何在delphi下和.net建立的webservice 进行协同工…
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台