欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java读写oracle的blob字段示例

程序员文章站 2024-03-31 11:17:46
复制代码 代码如下:package com.wanmei.meishu; import java.io.fileinputstream;import java.io.fi...

复制代码 代码如下:

package com.wanmei.meishu;

import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.filereader;
import java.io.inputstream;
import java.io.outputstream;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.properties;

import oracle.sql.blob;

public class blobutil {

 private static blobutil bu;
 private string env;
 public static blobutil getinstance(string env) {
  bu = new blobutil();
  bu.env = env;
  return bu;
 }

 /**
  * <p>得到数据库链接</p>
  * @return
  * @throws exception
  */
 private connection getconnection() throws exception {
  string driver = "oracle.jdbc.driver.oracledriver";
  class.forname(driver);
  string env = this.env;
  properties pro = new properties();
  // 读取classes 目录下的配置文件  
  pro.load(new filereader(class.class.getresource("/config.properties").getfile()));
  string host = pro.getproperty(env + ".host");
  string database = pro.getproperty(env + ".database");
  string username = pro.getproperty(env + ".username");
  string password = pro.getproperty(env + ".password");
  string port = pro.getproperty(env + ".port");
  string url = "jdbc:oracle:thin:@" + host + ":" + port +":" + database;
  return drivermanager.getconnection(url, username, password);
 }

 /**
  * <p>传入项目,任务,附件名称,文件路径,写入数据库</p>
  * @param projectid
  * @param taskid
  * @param filename
  * @param file
  * @return 返回是否成功
  */
 public boolean write(string projectid, string taskid, string filename, string file) {
  connection conn = null;
  resultset rs = null;
  preparedstatement ps = null;
  blob blob = null;
  boolean flag = false;
  try {
   conn = getconnection();
   conn.setautocommit(false);

   string sql = "insert into ps_zp_prj_wbs_blob blb(zp_prj_id, zz_seq_num, zz_file_name, zz_image_blob) values(?, ? ,? ,empty_blob())";
   ps = conn.preparestatement(sql);
   ps.setstring(1, projectid);
   ps.setstring(2, taskid);
   ps.setstring(3, filename);
   ps.executeupdate();
   sql = "select zz_image_blob from ps_zp_prj_wbs_blob where zp_prj_id = ? and zz_seq_num = ? and zz_file_name = ? for update";
   ps = conn.preparestatement(sql);
   ps.setstring(1, projectid);
   ps.setstring(2, taskid);
   ps.setstring(3, filename);
   rs = ps.executequery();
   if(rs.next()) {
    blob = (blob) rs.getblob(1);
   }
   inputstream in = new fileinputstream(file);
   outputstream out = blob.setbinarystream(1l);
   byte[] buffer = new byte[1024];
   int length = -1;
            while ((length = in.read(buffer)) != -1){
                out.write(buffer, 0, length);
            }
            in.close();
            out.close();
            conn.commit();
            conn.setautocommit(true);
            flag = true; 
  }
  catch(exception e) {
   if(conn != null) {
    try {
     conn.rollback();
    } catch (sqlexception e1) {
     e1.printstacktrace();
    }
   }
  }
  finally {
   try {
    rs.close();
    ps.close();
    conn.close();
   }
   catch(exception e) {
    e.printstacktrace();
   }
  }
  return flag;
 }

 /**
  * <p>根据项目id,任务id,文件名读取数据库blob字段文件,写入指定的文件路径</p>
  * @param projectid
  * @param taskid
  * @param filename
  * @param file
  * @return 返回是否成功
  */
 public boolean read(string projectid, string taskid, string filename, string file) {
  connection conn = null;
  resultset rs = null;
  preparedstatement ps = null;
  blob blob = null;
  boolean flag = false;
  try {
   conn = getconnection();
   string sql = "select zz_image_blob from ps_zp_prj_wbs_blob where zp_prj_id = ? and zz_seq_num = ? and zz_file_name = ?";
   ps = conn.preparestatement(sql);
   ps.setstring(1, projectid);
   ps.setstring(2, taskid);
   ps.setstring(3, filename);
   rs = ps.executequery();
   if(rs.next()) {
    blob = (blob) rs.getblob(1);
   }
   inputstream in = blob.getbinarystream();
   byte[] buf = new byte[1024];
   int bytesin = 0;
   fileoutputstream out = new fileoutputstream(file);
   while ((bytesin = in.read(buf, 0, 1024)) != -1) {
    out.write(buf, 0, bytesin);
   }
            in.close();
            out.close();
            flag = true;
  }
  catch(exception e) {
   e.printstacktrace();
  }
  finally {
   try {
    rs.close();
    ps.close();
    conn.close();
   }
   catch(exception e) {
    e.printstacktrace();
   }
  }
  return flag;
 }

 public static void main(string[] args) {
  blobutil bu = blobutil.getinstance("msdev");
  system.out.println(bu.write("cb", "001", "image1", "d:\\61e44b02jw1dw4xbp2zo6j.jpg"));
  system.out.println(bu.read("cb", "001", "image1", "d:\\2.jpg"));
 }

}