java操作oracle的blob,clob資料

來源:互聯網
上載者:User

標籤:

一、區別和定義

       LONG: 可變長的字串資料,最長2G,LONG具有VARCHAR2列的特性,可以儲存長文本一個表中最多一個LONG列
  LONG RAW: 可變長位元據,最長2G
  CLOB:  字元大對象Clob 用來儲存單位元組的字元資料
  NCLOB: 用來儲存多位元組的字元資料
  BLOB: 用於儲存位元據
  BFILE: 儲存在檔案中的位元據,這個檔案中的資料只能被唯讀訪。但該檔案不包含在資料庫內。

        bfile欄位實際的檔案儲存體在檔案系統中,欄位中儲存的是檔案定位指標.bfile對oracle來說是唯讀,也不參與事務性控制和資料恢複. 
  
  CLOB,NCLOB,BLOB都是內部的LOB(Large Object)類型,最長4G,沒有LONG只能有一列的限制

  要儲存圖片、文字檔、Word檔案各自最好用哪種資料類型?
  --BLOB最好,LONG RAW也不錯,但Long是oracle將要廢棄的類型,因此建議用BLOB。

 

二、操作1、 get

BLOB

java 代碼
  1. //獲得資料庫連接       Connection con = ConnectionFactory.getConnection();       con.setAutoCommit(false);       Statement st = con.createStatement();       //不需要“for update”       ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1");       if (rs.next())       {           java.sql.Blob blob = rs.getBlob("BLOBATTR");           InputStream inStream = blob.getBinaryStream();           //data是讀出並需要返回的資料,類型是byte[]           data = new byte[input.available()];           inStream.read(data);           inStream.close();       }       inStream.close();       con.commit();       con.close();    

     

2、 putBLOBjava 代碼
  1. //獲得資料庫連接       Connection con = ConnectionFactory.getConnection();       con.setAutoCommit(false);       Statement st = con.createStatement();       //插入一個Null 物件empty_blob()       st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");       //鎖定資料行進行更新,注意“for update”語句       ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");       if (rs.next())       {           //得到java.sql.Blob對象後強制轉換為oracle.sql.BLOB           oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");           OutputStream outStream = blob.getBinaryOutputStream();           //data是傳入的byte數組,定義:byte[] data           outStream.write(data, 0, data.length);       }       outStream.flush();       outStream.close();       con.commit();       con.close();   

     

java操作oracle的blob,clob資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.