1. 程式人生 > >Oracle中BLOB和CLOB欄位的操作

Oracle中BLOB和CLOB欄位的操作

一、區別和定義

       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

CLOB
java 程式碼
  1. //獲得資料庫連線
  2.      Connection con = ConnectionFactory.getConnection();   
  3.      con.setAutoCommit(false);   
  4.      Statement st = con.createStatement();   
  5. //不需要“for update”
  6.      ResultSet rs = st.executeQuery(
    "select CLOBATTR from TESTCLOB where ID=1");   
  7. if (rs.next())   
  8.      {   
  9.          java.sql.Clob clob = rs.getClob("CLOBATTR");   
  10.          Reader inStream = clob.getCharacterStream();   
  11. char[] c = newchar[(int) clob.length()];   
  12.          inStream.read(c);   
  13. //data是讀出並需要返回的資料,型別是String
  14.          data =
    new String(c);   
  15.          inStream.close();   
  16.      }   
  17.      inStream.close();   
  18.      con.commit();   
  19.      con.close();   
BLOB
java 程式碼
  1. //獲得資料庫連線
  2.      Connection con = ConnectionFactory.getConnection();   
  3.      con.setAutoCommit(false);   
  4.      Statement st = con.createStatement();   
  5. //不需要“for update”
  6.      ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1");   
  7. if (rs.next())   
  8.      {   
  9.          java.sql.Blob blob = rs.getBlob("BLOBATTR");   
  10.          InputStream inStream = blob.getBinaryStream();   
  11. //data是讀出並需要返回的資料,型別是byte[]
  12.          data = newbyte[input.available()];   
  13.          inStream.read(data);   
  14.          inStream.close();   
  15.      }   
  16.      inStream.close();   
  17.      con.commit();   
  18.      con.close();   

2、 put

CLOB
java 程式碼
  1. //獲得資料庫連線
  2.      Connection con = ConnectionFactory.getConnection();   
  3.      con.setAutoCommit(false);   
  4.      Statement st = con.createStatement();   
  5. //插入一個空物件empty_clob()
  6.      st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");   
  7. //鎖定資料行進行更新,注意“for update”語句
  8.      ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");   
  9. if (rs.next())   
  10.      {   
  11. //得到java.sql.Clob物件後強制轉換為oracle.sql.CLOB
  12.          oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("CLOBATTR");   
  13.          Writer outStream = clob.getCharacterOutputStream();   
  14. //data是傳入的字串,定義:String data
  15. char[] c = data.toCharArray();   
  16.          outStream.write(c, 0, c.length);   
  17.      }   
  18.      outStream.flush();   
  19.      outStream.close();   
  20.      con.commit();   
  21.      con.close();   
BLOB java 程式碼
  1. //獲得資料庫連線
  2.      Connection con = ConnectionFactory.getConnection();   
  3.      con.setAutoCommit(false);   
  4.      Statement st = con.createStatement();   
  5. //插入一個空物件empty_blob()
  6.      st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");   
  7. //鎖定資料行進行更新,注意“for update”語句
  8.      ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");   
  9. if (rs.next())   
  10.      {   
  11. //得到java.sql.Blob物件後強制轉換為oracle.sql.BLOB
  12.          oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");   
  13.          OutputStream outStream = blob.getBinaryOutputStream();   
  14. //data是傳入的byte陣列,定義:byte[] data
  15.          outStream.write(data, 0, data.length);   
  16.      }   
  17.      outStream.flush();   
  18.      outStream.close();   
  19.      con.commit();   
  20.      con.close();