<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 0, comments - 77, trackbacks - 0, articles - 356
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    package com.semovy.test;

    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.io.Reader;
    import java.sql.Blob;
    import java.sql.Clob;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;

    /**
     *
     * @author semovy 測試向oracle 讀,寫文件Blob 讀,寫大文本Clob
     */
    public class OracleBlobTest {

     private String driver = "oracle.jdbc.driver.OracleDriver";

     private String url = "jdbc:oracle:thin:@localhost:1521:teckotooling";

     private String user = "scott";

     private String pwd = "tiger";

     public OracleBlobTest() {
     }

     public static void main(String[] args) {
      OracleBlobTest obt = new OracleBlobTest();
      obt.writeBlob();
      obt.readBlob();
      obt.writeClob();
      obt.readClob();
     }

     /**
      * 讀二進制文件
      *
      */
     private void readBlob() {
      Connection conn = null;
      try {
       conn = getConnection();
       Statement stmt = conn.createStatement();
       ResultSet rs = stmt.executeQuery("select * from test where id=1");
       byte[] buffer = new byte[1024];
       OutputStream out = new FileOutputStream("d:/360安全衛士定1.exe");
       int tempLen = 0;
       int amount = 0;
       if (rs.next()) {
        Blob blob = rs.getBlob("BINARYCONTENT");
        InputStream in = blob.getBinaryStream();
        while ((tempLen = in.read(buffer)) != -1) {
         out.write(buffer, 0, tempLen);
         amount += tempLen;
         System.out.println("已經讀出并寫:" + amount + " 字節");
        }
        System.out.println("已經讀出并寫:完成");
        out.flush();
        out.close();
        in.close();
        rs.close();
        stmt.close();
       }
      } catch (ClassNotFoundException e) {
       System.out.println(e.getLocalizedMessage());
      } catch (SQLException e) {
       System.out.println(e.getLocalizedMessage());
      } catch (IOException e) {
       System.out.println(e.getLocalizedMessage());
      } finally {
       try {
        if (conn != null)
         conn.close();
       } catch (SQLException e) {
        System.out.println(e.getLocalizedMessage());
       }
      }
     }

     /**
      * 寫二進制文件
      *
      */
     private void writeBlob() {
      Connection conn = null;
      try {
       conn = getConnection();
       conn.setAutoCommit(false);
       String sql = null;
       Statement stmt = conn.createStatement();
       sql = "delete from test where id=1";
       stmt.executeUpdate(sql);
       sql = "insert into test(1,BINARYCONTENT,CLOBCONTENT) values(1,empty_blob(),empty_clob())";
       stmt.executeUpdate(sql);
       ResultSet rs = stmt.executeQuery("select * from test where id=1");
       if (rs.next()) {
        Blob blob = rs.getBlob("BINARYCONTENT");
        OutputStream out = ((oracle.sql.BLOB) blob).setBinaryStream(0);// 從0開始,否則寫出的文件有差錯
        int bufferSize = ((oracle.sql.BLOB) blob).getBufferSize();
        System.out.println("bufferSize :" + bufferSize);
        BufferedInputStream in = new BufferedInputStream(
          new FileInputStream("d:/360安全衛士定.exe"), bufferSize);
        byte[] b = new byte[bufferSize];
        int count = in.read(b, 0, bufferSize);
        int amount = 0;
        while (count != -1) {
         out.write(b, 0, count);
         amount += count;
         System.out.println("處理了 " + amount + " 字節");
         count = in.read(b, 0, bufferSize);
         System.out.println("處理了 " + amount + " 字節,成功");
        }
        out.close();
        out = null;
        in.close();
        conn.commit();
       }

      } catch (ClassNotFoundException e) {
       System.out.println(e.getLocalizedMessage());
      } catch (SQLException e) {
       try {
        conn.rollback();
       } catch (SQLException e1) {
        System.out.println(e1.getLocalizedMessage());
       }
       System.out.println(e.getLocalizedMessage());
      } catch (IOException e) {
       System.out.println(e.getLocalizedMessage());
      } finally {
       try {
        if (conn != null)
         conn.close();
       } catch (SQLException e) {
        System.out.println(e.getLocalizedMessage());
       }
      }
     }

     /**
      * 讀大文本
      *
      */
     private void readClob() {
      Connection conn = null;
      try {
       conn = getConnection();
       Statement stmt = conn.createStatement();
       ResultSet rs = stmt.executeQuery("select * from test where id=2");
       String tempStr = null;
       if (rs.next()) {
        Clob clob = rs.getClob("CLOBCONTENT");
        if (clob != null) {
         Reader in = clob.getCharacterStream();
         BufferedReader br = new BufferedReader(in);
         System.out.println("開始讀....");
         while ((tempStr = br.readLine()) != null) {
          System.out.println(tempStr);
         }
         System.out.println("讀完成....");
         in.close();
        }
        rs.close();
        stmt.close();
       }
      } catch (ClassNotFoundException e) {
       System.out.println(e.getLocalizedMessage());
      } catch (SQLException e) {
       System.out.println(e.getLocalizedMessage());
      } catch (IOException e) {
       System.out.println(e.getLocalizedMessage());
      } finally {
       try {
        if (conn != null)
         conn.close();
       } catch (SQLException e) {
        System.out.println(e.getLocalizedMessage());
       }
      }
     }

     /**
      * 寫大文本
      *
      */
     private void writeClob() {
      Connection conn = null;
      try {
       conn = getConnection();
       conn.setAutoCommit(false);
       String sql = null;
       Statement stmt = conn.createStatement();
       sql = "delete from test where id=2";
       stmt.executeUpdate(sql);
       sql = "insert into test values(2,empty_blob(),empty_clob())";
       stmt.executeUpdate(sql);
       ResultSet rs = stmt.executeQuery("select * from test where id=2");
       if (rs.next()) {
        Clob clob = rs.getClob("CLOBCONTENT");
        PrintWriter out = new PrintWriter(new BufferedWriter(
          ((oracle.sql.CLOB) clob).setCharacterStream(0)));
        BufferedReader in = new BufferedReader(new InputStreamReader(
          new FileInputStream(
            "d:/在北大校園BBS引起轟動的一篇文章請熱愛祖國的人轉發!!!!.mht")));
        String str = null;
        System.out.println("開始寫...");
        while ((str = in.readLine()) != null) {
         out.println(str);
         System.out.println(str);
        }
        in.close();
        out.close();
        rs.close();
        conn.commit();
       }

      } catch (ClassNotFoundException e) {
       System.out.println(e.getLocalizedMessage());
      } catch (SQLException e) {
       try {
        conn.rollback();
       } catch (SQLException e1) {
        System.out.println(e1.getLocalizedMessage());
       }
       System.out.println(e.getLocalizedMessage());
      } catch (IOException e) {
       System.out.println(e.getLocalizedMessage());
      } finally {
       try {
        if (conn != null)
         conn.close();
       } catch (SQLException e) {
        System.out.println(e.getLocalizedMessage());
       }
      }
     }

     private Connection getConnection() throws ClassNotFoundException,
       SQLException {
      Class.forName(driver);
      return DriverManager.getConnection(url, user, pwd);
     }

     /**
      *
      * @param rs
      * @throws SQLException
      */
     private void displayResultSet(ResultSet rs) throws SQLException {
      ResultSetMetaData rsmd = rs.getMetaData();
      int colnum = rsmd.getColumnCount();
      while (rs.next()) {
       for (int i = 0; i < colnum; i++) {
        if (i == colnum - 1)
         System.out.print(rsmd.getColumnLabel(i + 1) + ": "
           + rs.getObject(i + 1));
        else
         System.out.print(rsmd.getColumnLabel(i + 1) + ": "
           + rs.getObject(i + 1) + " , ");
       }
       System.out.println();
      }
     }
    }

    主站蜘蛛池模板: 亚洲国产aⅴ成人精品无吗| 亚洲国产成人久久| 少妇亚洲免费精品| 四虎永久免费地址在线网站| 亚洲人成网站在线在线观看| 最近高清国语中文在线观看免费| 亚洲白色白色永久观看| 可以免费看黄的网站| 亚洲三级在线播放| 成人免费在线视频| 午夜亚洲乱码伦小说区69堂| 亚洲av无码天堂一区二区三区| 一区二区三区在线免费观看视频 | 亚洲精品无码av天堂| jizz中国免费| 国产精品亚洲а∨无码播放| 日本免费人成视频在线观看| 亚洲成人免费网址| 成人av免费电影| 日韩久久无码免费毛片软件| 亚洲精品你懂的在线观看| 在线成人爽a毛片免费软件| 国产亚洲精品成人AA片| 免费国产成人高清在线观看麻豆| 一级做a爰片久久毛片免费看| 亚洲人成人无码网www电影首页| 3344免费播放观看视频| 亚洲国产成人久久一区二区三区| 亚洲国产精品无码久久九九| 暖暖免费日本在线中文| 亚洲 日韩经典 中文字幕| 亚洲精品国自产拍在线观看| 在线成人爽a毛片免费软件| 亚洲精品无码专区在线| 亚洲精品字幕在线观看| 99久久这里只精品国产免费| 人成午夜免费大片在线观看| 亚洲综合久久久久久中文字幕| 国产又大又黑又粗免费视频 | 久久精品国产亚洲AV麻豆不卡| 成人性生交视频免费观看|