• <ul id="mayc0"></ul>
    <ul id="mayc0"><center id="mayc0"></center></ul>
    <strike id="mayc0"><input id="mayc0"></input></strike>
    <ul id="mayc0"></ul>
  • 始創于2000年 股票代碼:831685
    咨詢熱線:0371-60135900 注冊有禮 登錄
    • 掛牌上市企業
    • 60秒人工響應
    • 99.99%連通率
    • 7*24h人工
    • 故障100倍補償
    您的位置: 網站首頁 > 幫助中心>文章內容

    Oracle讀取Blob數據-通過JDBC

    發布時間:  2012/8/26 16:09:48

    通過JDBC讀取Blob數據,演示代碼

    1.  
    2.import java.io.File;  3.import java.io.FileOutputStream;  4.import java.io.IOException;  5.import java.io.InputStream;  6.import java.io.OutputStream;  7.import java.sql.Connection;  8.import java.sql.DriverManager;  9.import java.sql.ResultSet;  10.import java.sql.Statement;  11. 
    12.public class JDBC {  13.//  public static void main(String[] args) throws Exception {   14.//      // 寫入BLOB數據   15.//     16.//      // 得到數據庫連接   17.//      Class.forName("Oracle.jdbc.driver.OracleDriver");   18.//   19.//      Connection con = DriverManager.getConnection(   20.//   21.//      "jdbc:Oracle:thin:@192.168.0.68:1521:myOracle", "my", "123");   22.//   23.//      // 處理事務   24.//   25.//      con.setAutoCommit(false);   26.//   27.//      Statement st = con.createStatement();   28.//   29.//      // 用for update方式鎖定數據行   30.//   31.//      ResultSet rs = st.executeQuery(   32.//   33.//      "select content from user_content where id = 2 for update");   34.//   35.//      if (rs.next()) {   36.//   37.//          // 得到java.sql.Blob對象,然后Cast為Oracle.sql.BLOB   38.//   39.//          Oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);   40.//   41.//          // 到數據庫的輸出流   42.//   43.//          OutputStream outStream = blob.getBinaryOutputStream();   44.//   45.//          // 這里用一個文件模擬輸入流   46.//   47.//          File file = new File("d:\\proxy.txt");   48.//   49.//          InputStream fin = new FileInputStream(file);   50.//   51.//          // 將輸入流寫到輸出流   52.//   53.//          byte[] b = new byte[blob.getBufferSize()];   54.//   55.//          int len = 0;   56.//   57.//          while ((len = fin.read(b)) != -1) {   58.//   59.//              outStream.write(b, 0, len);   60.//   61.//              // blob.putBytes(1,b);   62.//   63.//          }   64.//   65.//          // 依次關閉(注意順序)   66.//   67.//          fin.close();   68.//   69.//          outStream.flush();   70.//   71.//          outStream.close();   72.//   73.//          con.commit();   74.//   75.//          con.close();   76.//      }   77.//  }   78. 
    79.    public static void main(String[] args) throws Exception {  80.        // 讀取BLOB數據   81.         
    82.        Class.forName("Oracle.jdbc.driver.OracleDriver");  83. 
    84.        Connection con = DriverManager.getConnection( 
    85. 
    86.        "jdbc:Oracle:thin:@192.168.0.68:1521:myOracle", "my", "123");  87. 
    88.        con.setAutoCommit(false);  89. 
    90.        Statement st = con.createStatement(); 
    91. 
    92.        // 這里的SQL語句不再需要”for update”   93. 
    94.        ResultSet rs = st.executeQuery( 
    95. 
    96.        "select content from user_content where id = 2");  97. 
    98.        if (rs.next()) {  99. 
    100.            java.sql.Blob blob = rs.getBlob(1);  101. 
    102.            InputStream ins = blob.getBinaryStream(); 
    103. 
    104.            // 用文件模擬輸出流   105. 
    106.            File file = new File("d:\\output.txt");  107. 
    108.            OutputStream fout = new FileOutputStream(file);  109. 
    110.            // 下面將BLOB數據寫入文件   111. 
    112.            byte[] b = new byte[1024];  113. 
    114.            int len = 0;  115. 
    116.            while ((len = ins.read(b)) != -1) {  117. 
    118.                fout.write(b, 0, len);  119. 
    120.            } 
    121. 
    122.            // 依次關閉   123. 
    124.            fout.close(); 
    125. 
    126.            ins.close(); 
    127. 
    128.            con.commit(); 
    129. 
    130.            con.close(); 
    131.        } 
    132.    } 
    133.} 


     


    本文出自:億恩科技【www.vbseamall.com】

    服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經營性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經營性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經營性ICP/ISP證:贛B2-20080012
  • 服務器/云主機 24小時售后服務電話:0371-60135900
  • 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
  • 專注服務器托管17年
    掃掃關注-微信公眾號
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
      0
     
     
     
     

    0371-60135900
    7*24小時客服服務熱線