oracle中blob、clob的读取和写入
在oracle数据库中,大类型字段(也称为大对象或lob,large object)用于存储大量的数据,如文本、图像、视频等。
oracle 提供了几种不同的大类型字段,主要包括:
1.clob(character large object):
- 存储大量的字符数据,可以存储多达4 gb的文本。
- 适用于需要存储大段文本信息的场景,如文档、日志记录等。
2.blob(binary large object):
- 存储大量的二进制数据,可以存储多达4 gb的二进制信息。
- 常用于存储图片、音频、视频等媒体文件。
3.nclob(national character large object):
- 类似于clob,但用于存储多字节字符集(如unicode字符集)的数据。
- 适用于需要存储多国语言文本的应用。
4.bfile(binary file):
- 存储外部文件的引用,而不是将文件内容直接存储在数据库中。
- bfile可以存储在数据库外部文件系统中,数据库只存储其路径和文件名。
基于sql和java的方式实现读取和插入这些大类型字段,同时将读取的数据转换为字符串类型。
基于sql的方式实现clob、blob的插入与读取
1. 插入大类型数据
插入 clob 数据
clob用于存储大段文本,可以通过简单的sql插入语句来插入数据:
insert into my_table (id, clob_column) values (1, 'this is a large text that can go up to 4 gb');
插入 blob 数据
blob用于存储二进制数据。由于直接通过sql插入blob数据较为复杂,通常会通过文件或其他方法插入数据。
假设我们要插入一段十六进制字符串代表的二进制数据:
insert into my_table (id, blob_column)
values (1, hextoraw('48656c6c6f20576f726c64')); -- 'hello world' in hexadecimal2. 读取大类型数据并转换为字符串
读取 clob 数据并转换为字符串
clob字段中的数据可以直接读取并视为字符串:
select clob_column from my_table where id = 1;
读取 blob 数据并转换为字符串 (utl_raw.cast_to_varchar2)
blob数据通常是二进制的,如果需要将其转换为字符串,可以使用sql中的utl_raw.cast_to_varchar2函数,将其转换为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中的数据,最大可提取4000字节。
也可以先使用dbms_lob.substr(blob_column) 来判断blob类型字段的数据长度,如果不超过 2000,可以直接使用utl_raw.cast_to_varchar2(blob_column)来将blob类型数据转为字符类型。
select utl_raw.cast_to_varchar2(blob_column) from my_table where id = 1;
基于java方式实现clob和blob的插入与读取
在java中,通过preparedstatement进行插入,通过resultset进行读取。
1. java 中插入 clob 和 blob 数据
插入 clob 数据
使用java的preparedstatement将字符串数据插入到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 数据
blob通常用于存储二进制数据,如图像或文件。
通过java的preparedstatement插入二进制数据:
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 数据并转换为字符串
读取 clob 数据并转换为字符串
读取clob数据并将其转换为字符串非常简单:
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 数据并转换为字符串
由于blob是二进制数据,需要先读取为字节数组,然后将其转换为字符串
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();
}
}
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论