登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

仙剑奇侠传的博客

羽化成仙

 
 
 

日志

 
 

JDBC for Oracle Blob操作  

2011-07-17 23:02:19|  分类: JDBC |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

oracle for jdbc thin的驱动3.0 不支持 *Statement 对象的setBinaryStream,要实现图片、word文档等写入到数据库的blob等列,需要借助于Clob、Blob等java对象。源码如下:

package com.wsysoft.lang;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OracleResultSet;
import oracle.sql.BLOB;

import java.sql.ResultSet;

import org.apache.openjpa.jdbc.sql.Result;

/**
 *
 * 显示对oracle数据库操作blob或clob字段 Copyright: Copyright (c)2011
 *
 * @author: wsysoft@gmail.com
 * @version: 1.0 Create at: 2011-7-17 下午09:17:05
 *
 *           Modification History: Date Author Version Description
 *           ------------------------------------------------------------------
 *           2011-7-17 wsysoft@gmail.com 1.0
 */

public class JDBCBlobOrClobDemo {

 /**
  * {方法的功能/动作描述}
  *
  * @param {引入参数名} {引入参数说明}
  * @return {返回参数名} {返回参数说明}
  * @exception {说明在某情况下,将发生什么异常}
  */

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  OracleDataSource ods = null;
  Connection conn = null;
  Statement stment = null;
  FileInputStream instream = null;
  ResultSet result = null;
  OutputStream outstream = null;
  try {
   ods = new OracleDataSource();
   ods.setURL("jdbc:oracle:thin:@localhost:1521:maximo");
   ods.setUser("java");
   ods.setPassword("java123456");
   conn = ods.getConnection();
   //
   /*
    * String ddl =
    * "CREATE TABLE MYTABLE(NAME VARCHAR2(30),IMAGE BLOB)";
    *
    * stment = conn.createStatement(); stment.execute(ddl);
    */

   stment = conn.createStatement();
   String ddl = "insert into mytable(name,image) values('仙剑奇侠',empty_blob())";
   stment.execute(ddl);
   conn.setAutoCommit(false);
   result = stment
     .executeQuery("select name,image from mytable where name='仙剑奇侠'  for update");
   Blob blob = null;
   if (result.next()) {
    blob = ((OracleResultSet) result).getBlob("image");
   }
   File binaryFile = new File("./honghong.jpg");
   System.out.println("honghong File length = " + binaryFile.length());
   instream = new FileInputStream(binaryFile);
   outstream = blob.setBinaryStream(1L);
   int size = ((BLOB) blob).getBufferSize();
   byte[] buffer = new byte[size];
   int length = -1;
   while ((length = instream.read(buffer)) != -1)
    outstream.write(buffer, 0, length);

  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    try {
     instream.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    outstream.close();
    conn.commit();
    if (!(conn.isClosed())) {
     conn.close();
    }
   } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    ods.close();
   } catch (SQLException e) {

    e.printStackTrace();
   }

  }

 }

}

  评论这张
 
阅读(447)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018