Java对Oracle里Blob字段的处理

比较简单,直接贴代码吧,有问题留言

/**
 * Usage:Oracle Blob Field
 *
 * FileName   : Blob.java
 * PackageName: org.ahpo.db
 * Author     : Ahpo Yang
 * CreateDate : 2006-4-24 14:54:04
 */
package org.ahpo.db;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JBlob {

 String tableName = null; // 表名

 String primaryKey = null; // 表的主键名

 String primaryValue = null; // 表的主键值

 String fieldName = null; // 表的CLOB字段名

 String clobValue = null; // 表的CLOB字段值

 Connection conn = null; // 与oracle的连接

 /**
  *
  * 用于测试用
  *
  */
 public static void main(String[] args) {
  try {
   JBlob jb = new JBlob(getConnection(), "aa", "a", "aaaa", "c", "ccc");
   jb.writeBlob();
   jb.readBlob();
  } catch (Exception e) {
   System.out.println(e);
   e.printStackTrace();
  }
 }

 /**
  *
  * 构造方法
  *
  */
 public JBlob(Connection connection, String tableName, String primaryKey,
   String primaryValue, String fieldName, String clobValue) {
  this.conn = connection;
  this.tableName = tableName;
  this.primaryKey = primaryKey;
  this.primaryValue = primaryValue;
  this.fieldName = fieldName;
  this.clobValue = clobValue;
 }

 /**
  *
  * 构造方法,但不必传clobValue值 一般构造出的实例用来读Clob字段
  *
  */
 public JBlob(Connection connection, String tableName, String primaryKey,
   String primaryValue, String fieldName) {
  this.conn = connection;
  this.tableName = tableName;
  this.primaryKey = primaryKey;
  this.primaryValue = primaryValue;
  this.fieldName = fieldName;
 }

 /**
  * 读取Blob字段
  *
  * @return
  * @throws SQLException
  * @throws IOException
  */
 public String readBlob() throws SQLException, IOException {
  String strRet = "";
  Statement stmt = null;
  ResultSet rs = null;
  try {
   String strSql = "select " + fieldName + " from " + tableName
     + " where " + primaryKey + "=" + primaryValue;
   conn = getConnection();
   stmt = conn.createStatement();
   rs = stmt.executeQuery(strSql);
   byte b[] = null; // 保存从BLOB读出的字节

   if (rs.next()) {
    // System.out.println("ahpo = == 数据存在");
    java.sql.Blob blob = (java.sql.Blob) rs.getBlob(1);
    int filesize = (int) blob.length();
    b = blob.getBytes(1, filesize); // 从BLOB取出字节流数据
    ByteArrayInputStream bi = new ByteArrayInputStream(b);
    ObjectInputStream q = new ObjectInputStream(bi);
    try {
     strRet = (String) q.readObject();

    } catch (ClassNotFoundException ex) {
    } catch (IOException ex) {
    }

    bi.close();
    q.close();
   }

  } catch (SQLException e) {
   throw e;
  } catch (Exception e) {
   // System.out.println("ahpo===异常出现");
   // 异常处理
   try {
    conn.rollback();
   } catch (Exception ex) {
    ex.printStackTrace();
   }
   e.printStackTrace();

  } finally {
   // 释放连接
   if (rs != null) {
    try {
     rs.close();
    } catch (Exception ex) {
    }
   }
   if (stmt != null) {
    try {
     stmt.close();
    } catch (Exception ex) {
    }
   }
   if (conn != null) {
    try {
     conn.close();
    } catch (Exception ex) {
    }
   }
  }

  return strRet;

 }

 public void writeBlob() throws SQLException, IOException {
  // open a file, put the content of file into a bytes array
  try {
   File files = new File("d://rssxml//sample.xml");
   FileInputStream fis = new FileInputStream(files);

   byte[] fl = new byte[(int) files.length()];
   fis.read(fl);

   // use this bytes array to construct a InputStream
   ByteArrayInputStream kk = new ByteArrayInputStream(fl);

   // connect the oracle database
   if (conn == null) {
    conn = getConnection();
   }

   PreparedStatement ps = conn.prepareStatement("INSERT INTO "
     + tableName + "(" + fieldName + ") values (?) where "
     + primaryKey + "=" + primaryValue);

   // set parameters

   ps.setBinaryStream(1, kk, (int) files.length());

   // execute

   ps.executeUpdate();
   fis.close();
   ps.close();
  } catch (IOException e) {
   throw e;

  } catch (SQLException e) {
   throw e;

  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * 取得数据库连接
  *
  * @return
  * @throws SQLException
  * @throws ClassNotFoundException
  */
 public static Connection getConnection() throws SQLException,
   ClassNotFoundException {
  Class.forName("oracle.jdbc.OracleDriver");
  Connection conn = DriverManager.getConnection(
    "jdbc:oracle:thin:@192.168.1.18:1521:portal", "portal",
    "portal");
  return conn;
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值