Oracle中BLOB、CLOB的讀取和寫入方式
Oracle中BLOB、CLOB的讀取和寫入
在Oracle數(shù)據(jù)庫中,大類型字段(也稱為大對象或LOB,Large Object)用于存儲大量的數(shù)據(jù),如文本、圖像、視頻等。
Oracle 提供了幾種不同的大類型字段,主要包括:
1.CLOB(Character Large Object):
- 存儲大量的字符數(shù)據(jù),可以存儲多達4 GB的文本。
- 適用于需要存儲大段文本信息的場景,如文檔、日志記錄等。
2.BLOB(Binary Large Object):
- 存儲大量的二進制數(shù)據(jù),可以存儲多達4 GB的二進制信息。
- 常用于存儲圖片、音頻、視頻等媒體文件。
3.NCLOB(National Character Large Object):
- 類似于CLOB,但用于存儲多字節(jié)字符集(如Unicode字符集)的數(shù)據(jù)。
- 適用于需要存儲多國語言文本的應(yīng)用。
4.BFILE(Binary File):
- 存儲外部文件的引用,而不是將文件內(nèi)容直接存儲在數(shù)據(jù)庫中。
- BFILE可以存儲在數(shù)據(jù)庫外部文件系統(tǒng)中,數(shù)據(jù)庫只存儲其路徑和文件名。
基于SQL和Java的方式實現(xiàn)讀取和插入這些大類型字段,同時將讀取的數(shù)據(jù)轉(zhuǎn)換為字符串類型。
基于SQL的方式實現(xiàn)CLOB、BLOB的插入與讀取
1. 插入大類型數(shù)據(jù)
插入 CLOB 數(shù)據(jù)
CLOB用于存儲大段文本,可以通過簡單的SQL插入語句來插入數(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用于存儲二進制數(shù)據(jù)。由于直接通過SQL插入BLOB數(shù)據(jù)較為復(fù)雜,通常會通過文件或其他方法插入數(shù)據(jù)。
假設(shè)我們要插入一段十六進制字符串代表的二進制數(shù)據(jù):
INSERT INTO my_table (id, blob_column)
VALUES (1, hextoraw('48656C6C6F20576F726C64')); -- 'Hello World' in hexadecimal2. 讀取大類型數(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ù)通常是二進制的,如果需要將其轉(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ù)長度,如果不超過 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方式實現(xiàn)CLOB和BLOB的插入與讀取
在Java中,通過PreparedStatement進行插入,通過ResultSet進行讀取。
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通常用于存儲二進制數(shù)據(jù),如圖像或文件。
通過Java的PreparedStatement插入二進制數(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)換為字符串非常簡單:
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是二進制數(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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Oracle與SQL Server在企業(yè)應(yīng)用的比較
Oracle與SQL Server在企業(yè)應(yīng)用的比較...2007-03-03

