首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> Oracle 資料庫 >> Oracle 資料庫基礎

JDBC 操作 Oracle LOB 欄位完整實作指南

     編輯:Oracle 資料庫基礎

LOB 類型概覽

  • BLOB: 二進位大物件,存儲圖片、影音、程式碼等非文字位元流,上限 4GB。
  • CLOB: 字元大物件,字集相關,存儲長文本、檔案內容等,上限 4GB。

LONG/LONG RAW 限單表一欄且上限 2GB,已逐漸被 LOB 取代。

測試表結構

CREATE TABLE test_clob (id INTEGER, content CLOB);
CREATE TABLE test_blob (id INTEGER, data BLOB);

CLOB 寫入(插入新列)

String sql = "INSERT INTO test_clob(id, content) VALUES (?, ?)";
conn.setAutoCommit(false);
CLOB clob = CLOB.createTemporary((OracleConnection)conn, true, CLOB.DURATION_SESSION);
clob.setString(1, "大量文字內容...");

OraclePreparedStatement pstmt = (OraclePreparedStatement)conn.prepareStatement(sql);
pstmt.setInt(1, 1);
pstmt.setClob(2, clob);
pstmt.executeUpdate();
pstmt.close();
conn.commit();

CLOB 更新(覆寫模式)

String sql = "UPDATE test_clob SET content = ? WHERE id = 1";
CLOB clob = CLOB.createTemporary((OracleConnection)conn, true, CLOB.DURATION_SESSION);
clob.setString(1, "更新後的內容...");

OraclePreparedStatement pstmt = (OraclePreparedStatement)conn.prepareStatement(sql);
pstmt.setClob(1, clob);
pstmt.executeUpdate();
pstmt.close();
conn.commit();

關鍵注意事項

  1. 必須匯入 oracle.sql.*、oracle.jdbc.driver.*
  2. 使用 OraclePreparedStatement 並呼叫 setClob/setBlob
  3. 暫時 LOB 建立於 Session 期間,交易提交後自動轉為持久
  4. 大量資料建議使用流式讀寫 (getAsciiStream/setBinaryStream) 避免記憶體溢位
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved