小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

oracle Blob 文件讀寫

 dna26 2012-11-13

import java.io.*;
import java.sql.*;

import oracle.sql.*;

public class Tests {
 /**
  * 創(chuàng)建表后插入一條記錄
  *
  * @throws Exception
  */
 public static void createTable() throws Exception {
  Connection conn = null;
  PreparedStatement pstmt = null;

  String sql = "CREATE TABLE FORUM.systemp(blobid BLOB,clobid CLOB)";
  conn = PoolFactory.newInstance();
  // 添加一張表,再添加一條記錄
  pstmt = conn.prepareStatement(sql);
  pstmt.executeUpdate();
  sql = "insert into systemp (blobid,clobid) values(empty_blob(),empty_clob())";
  pstmt = conn.prepareStatement(sql);
  pstmt.executeUpdate();

  pstmt.close();
  conn.close();
 }

 /**
  * 更新值為空
  *
  * @throws Exception
  */
 private static void updateSystemp() throws Exception {
  Connection conn = null;
  PreparedStatement pstmt = null;

  conn = PoolFactory.newInstance();

  String sql = "update systemp set blobid=empty_blob(),clobid=empty_clob()";
  pstmt = conn.prepareStatement(sql);
  pstmt.executeUpdate();

  pstmt.close();
  conn.close();
 }

 /**
  *
  * @param content
  *            String
  * @return Clob
  * @throws Exception
  */
 public static Clob retClob(Connection conn, String content)
   throws Exception {
  return (Clob) retBClob(conn, false, false, content);
 }

 /**
  *
  * @param Connection
  *            conn 連接
  * @param isBlob
  *            boolean 需要實(shí)例化的類型,TRUE返回BLOB實(shí)例,F(xiàn)ALSE返回CLOB實(shí)例
  * @param isFile
  *            boolean 是否是文件
  * @param content
  *            String 需要轉(zhuǎn)化的內(nèi)容,或文件名
  * @return Object 把content進(jìn)行BLOB,CLOB實(shí)例化。
  * @throws Exception
  */
 public static Object retBClob(Connection conn, boolean isBlob,
   boolean isFile, String content) throws Exception {
  // updateSystemp();

  PreparedStatement pstmt = null;
  ResultSet rs = null;

  int columnIndex = 2;
  if (isBlob) {
   columnIndex = 1;
  }

  conn.setAutoCommit(false);
  try {
   pstmt = conn.prepareStatement("select * from systemp FOR UPDATE");
   rs = pstmt.executeQuery();
   if (rs.next()) {
    return fillContent(rs.getObject(columnIndex), isBlob, isFile,
      content);
   }
  } catch (SQLException ex) {
   throw new Exception(
     "please establish the systemp table!\n that one column is Blob and the other is Clob!",
     ex);
  } finally {
   if (rs != null)
    rs.close();
   if (pstmt != null)
    pstmt.close();
  }

  throw new Exception("please insert into one record to table systemp!");
 }

 /**
  *
  * @param fileName
  *            String 文件路徑,包含文件名
  * @return InputStream 讀入文件的IO流
  * @throws Exception
  */
 private static InputStream getFileStream(String fileName) throws Exception {
  File f = new File(fileName);
  InputStream fis = new FileInputStream(f);

  return fis;
 }

 private static Object fillContent(Object o, boolean isBlob, boolean isFile,
   String content) throws Exception {
  OutputStream out = null;
  InputStream fis = null;
  //根據(jù)isBlob創(chuàng)建相應(yīng)的輸出流
  out = distinctOutputStream(o, isBlob);
  //創(chuàng)建相應(yīng)的文件流或字符流
  fis = distinctInputStream(isFile, content);

  // int count = -1, total = 0;
  byte[] data = new byte[(int) fis.available()];
  fis.read(data);
  out.write(data);

  /*
   * byte[] data = new byte[blob.getBufferSize()]; 另一種實(shí)現(xiàn)方法,節(jié)省內(nèi)存 while
   * ((count = fin.read(data)) != -1) { total += count; out.write(data, 0,
   * count); }
   */
  fis.close();
  out.close();

  return o;
 }

 /**
  * 根據(jù)BLOB,CLOB創(chuàng)建相應(yīng)的輸出流
  *
  * @param o
  *            Object
  * @param isBlob
  *            boolean
  * @return OutputStream
  * @throws Exception
  */
 private static OutputStream distinctOutputStream(Object o, boolean isBlob)
   throws Exception {
  OutputStream out = null;
  if (isBlob) {
   out = ((BLOB) o).getBinaryOutputStream();
  } else {
   out = ((CLOB) o).getAsciiOutputStream();
  }
  return out;
 }

 /**
  * 是否為文件,是則返回文件流,否則返回STRING流
  *
  * @param isFile
  *            boolean
  * @param content
  *            String 文件名或STRING流
  * @return InputStream
  * @throws Exception
  */
 private static InputStream distinctInputStream(boolean isFile,
   String content) throws Exception {
  InputStream fis = null;
  if (isFile) {
   fis = getFileStream(content);
  } else {
   fis = new ByteArrayInputStream(content.getBytes());
   // fis = new StringInputStream(content);
  }
  return fis;
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }


public static String blobToString(Object obj) {
  StringBuffer sb = new StringBuffer();
  try {
   if (obj instanceof BLOB) {
    System.out.println("11111111");
    InputStream is = ((BLOB) obj).getBinaryStream();
    byte[] buff = new byte[2048];
    while (is.read(buff) != -1) {
     sb.append(new String(buff));
    }
    if (is != null)
     is.close();
   } else if (obj instanceof CLOB) {
    System.out.println("2222222");
    int i = 0;
    Reader reader = ((CLOB) obj).getCharacterStream();
    char[] b = new char[10000];// 每次獲取10K
    while ((i = reader.read(b)) != -1) {
     sb.append(b, 0, i);
    }
    if (reader != null)
     reader.close();
   } else
    return "";

  } catch (Exception e) {
  }
  System.out.println(sb.toString() + "1111");
  return sb.toString();

 }

}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多