Oracle中BLOB、CLOB的讀取和寫入方式
Oracle中BLOB、CLOB的讀取和寫入
在Oracle數(shù)據(jù)庫(kù)中,大類型字段(也稱為大對(duì)象或LOB,Large Object)用于存儲(chǔ)大量的數(shù)據(jù),如文本、圖像、視頻等。
Oracle 提供了幾種不同的大類型字段,主要包括:
1.CLOB(Character Large Object):
- 存儲(chǔ)大量的字符數(shù)據(jù),可以存儲(chǔ)多達(dá)4 GB的文本。
- 適用于需要存儲(chǔ)大段文本信息的場(chǎng)景,如文檔、日志記錄等。
2.BLOB(Binary Large Object):
- 存儲(chǔ)大量的二進(jìn)制數(shù)據(jù),可以存儲(chǔ)多達(dá)4 GB的二進(jìn)制信息。
- 常用于存儲(chǔ)圖片、音頻、視頻等媒體文件。
3.NCLOB(National Character Large Object):
- 類似于CLOB,但用于存儲(chǔ)多字節(jié)字符集(如Unicode字符集)的數(shù)據(jù)。
- 適用于需要存儲(chǔ)多國(guó)語(yǔ)言文本的應(yīng)用。
4.BFILE(Binary File):
- 存儲(chǔ)外部文件的引用,而不是將文件內(nèi)容直接存儲(chǔ)在數(shù)據(jù)庫(kù)中。
- BFILE可以存儲(chǔ)在數(shù)據(jù)庫(kù)外部文件系統(tǒng)中,數(shù)據(jù)庫(kù)只存儲(chǔ)其路徑和文件名。
基于SQL和Java的方式實(shí)現(xiàn)讀取和插入這些大類型字段,同時(shí)將讀取的數(shù)據(jù)轉(zhuǎn)換為字符串類型。
基于SQL的方式實(shí)現(xiàn)CLOB、BLOB的插入與讀取
1. 插入大類型數(shù)據(jù)
插入 CLOB 數(shù)據(jù)
CLOB用于存儲(chǔ)大段文本,可以通過簡(jiǎn)單的SQL插入語(yǔ)句來插入數(shù)據(jù):
INSERT INTO my_table (id, clob_column) VALUES (1, 'This is a large text that can go up to 4 GB');
插入 BLOB 數(shù)據(jù)
BLOB用于存儲(chǔ)二進(jìn)制數(shù)據(jù)。由于直接通過SQL插入BLOB數(shù)據(jù)較為復(fù)雜,通常會(huì)通過文件或其他方法插入數(shù)據(jù)。
假設(shè)我們要插入一段十六進(jìn)制字符串代表的二進(jìn)制數(shù)據(jù):
INSERT INTO my_table (id, blob_column) VALUES (1, hextoraw('48656C6C6F20576F726C64')); -- 'Hello World' in hexadecimal
2. 讀取大類型數(shù)據(jù)并轉(zhuǎn)換為字符串
讀取 CLOB 數(shù)據(jù)并轉(zhuǎn)換為字符串
CLOB字段中的數(shù)據(jù)可以直接讀取并視為字符串:
SELECT clob_column FROM my_table WHERE id = 1;
讀取 BLOB 數(shù)據(jù)并轉(zhuǎn)換為字符串 (UTL_RAW.CAST_TO_VARCHAR2
)
BLOB數(shù)據(jù)通常是二進(jìn)制的,如果需要將其轉(zhuǎn)換為字符串,可以使用SQL中的UTL_RAW.CAST_TO_VARCHAR2
函數(shù),將其轉(zhuǎn)換為VARCHAR2類型(注意BLOB大小不能超過VARCHAR2的限制[2000]
):
SELECT UTL_RAW.CAST_TO_VARCHAR2(dbms_lob.substr(blob_column, 4000, 1)) FROM my_table WHERE id = 1;
這里dbms_lob.substr
用于提取BLOB中的數(shù)據(jù),最大可提取4000字節(jié)。
也可以先使用dbms_lob.substr(blob_column)
來判斷BLOB類型字段的數(shù)據(jù)長(zhǎng)度,如果不超過 2000
,可以直接使用UTL_RAW.CAST_TO_VARCHAR2(blob_column)
來將BLOB類型數(shù)據(jù)轉(zhuǎn)為字符類型。
SELECT UTL_RAW.CAST_TO_VARCHAR2(blob_column) FROM my_table WHERE id = 1;
基于Java方式實(shí)現(xiàn)CLOB和BLOB的插入與讀取
在Java中,通過PreparedStatement
進(jìn)行插入,通過ResultSet
進(jìn)行讀取。
1. Java 中插入 CLOB 和 BLOB 數(shù)據(jù)
插入 CLOB 數(shù)據(jù)
使用Java的PreparedStatement
將字符串?dāng)?shù)據(jù)插入到CLOB字段:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class ClobInsertExample { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; try { conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "username", "password"); String sql = "INSERT INTO my_table (id, clob_column) VALUES (?, ?)"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 1); pstmt.setString(2, "This is a large text for CLOB field."); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }
插入 BLOB 數(shù)據(jù)
BLOB通常用于存儲(chǔ)二進(jìn)制數(shù)據(jù),如圖像或文件。
通過Java的PreparedStatement
插入二進(jìn)制數(shù)據(jù):
import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class BlobInsertExample { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; try { conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "username", "password"); String sql = "INSERT INTO my_table (id, blob_column) VALUES (?, ?)"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 1); FileInputStream inputStream = new FileInputStream("path/to/your/file.jpg"); pstmt.setBinaryStream(2, inputStream, inputStream.available()); pstmt.executeUpdate(); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }
2. Java 中讀取 CLOB 和 BLOB 數(shù)據(jù)并轉(zhuǎn)換為字符串
讀取 CLOB 數(shù)據(jù)并轉(zhuǎn)換為字符串
讀取CLOB數(shù)據(jù)并將其轉(zhuǎn)換為字符串非常簡(jiǎn)單:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class ClobReadExample { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "username", "password"); String sql = "SELECT clob_column FROM my_table WHERE id = ?"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 1); rs = pstmt.executeQuery(); if (rs.next()) { String clobData = rs.getString("clob_column"); System.out.println(clobData); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }
讀取 BLOB 數(shù)據(jù)并轉(zhuǎn)換為字符串
由于BLOB是二進(jìn)制數(shù)據(jù),需要先讀取為字節(jié)數(shù)組,然后將其轉(zhuǎn)換為字符串
import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class BlobReadExample { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "username", "password"); String sql = "SELECT blob_column FROM my_table WHERE id = ?"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 1); rs = pstmt.executeQuery(); if (rs.next()) { InputStream inputStream = rs.getBinaryStream("blob_column"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } byte[] imageBytes = outputStream.toByteArray(); String blobAsString = new String(imageBytes, "UTF-8"); System.out.println(blobAsString); inputStream.close(); outputStream.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Oracle數(shù)據(jù)更改后出錯(cuò)的解決方法
這篇文章主要介紹了Oracle數(shù)據(jù)更改后出錯(cuò)的解決方法,需要的朋友可以參考下2014-07-07Oracle與SQL Server在企業(yè)應(yīng)用的比較
Oracle與SQL Server在企業(yè)應(yīng)用的比較...2007-03-03Oracle排名函數(shù)(Rank)實(shí)例詳解
這篇文章主要介紹了Oracle排名函數(shù)(Rank)實(shí)例詳解,需要的朋友可以參考下2014-06-06Oracle如何實(shí)現(xiàn)like多個(gè)值的查詢
這篇文章主要給大家介紹了關(guān)于Oracle如何實(shí)現(xiàn)like多個(gè)值的查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08Oracle去重4種實(shí)現(xiàn)方式小結(jié)
這篇文章主要給大家介紹了關(guān)于Oracle去重4種實(shí)現(xiàn)方式的相關(guān)資料,在Oracle數(shù)據(jù)庫(kù)中有時(shí)候我們需要查詢多個(gè)列并去除重復(fù)值,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09