6、JDBC-处理CLOB与BLOB
Blob 是一个二进制大型对象(文件),在MySQL中有四种 Blob 类型,区别是容量不同
| TinyBlob | 255B |
| Blob | 65KB |
| MediumBlob | 16MB |
| LongBlob | 4GB |
插入数据
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import java.io.*;
import java.sql.*;
import java.util.Properties; public class BlobTest { private Connection connection;
private ResultSet resultSet;
private PreparedStatement preparedStatement; @BeforeEach
public void start() throws Exception {
Properties properties = new Properties();
InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(in); String driver = properties.getProperty("driver");
String jdbcUrl = properties.getProperty("jdbcUrl");
String user = properties.getProperty("user");
String password = properties.getProperty("password"); Class.forName(driver); connection = DriverManager.getConnection(jdbcUrl, user, password);
} @AfterEach
public void end() throws Exception {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} /**
* 插入 BLOB 类型的数据必须使用 PreparedStatement:因为 BLOB 类型的数据时无法使用字符串拼写的。
* 可封装成 Blob 对象,也可直接使用IO流,调用 setBlob 或 setBinaryStream
*/
@Test
public void testInsertBlob() {
try {
String sql = "INSERT INTO blob_test (file, name) VALUES (?,?)";
preparedStatement = connection.prepareStatement(sql); Blob blob = connection.createBlob();
InputStream in = this.getClass().getClassLoader().getResourceAsStream("file.png");
OutputStream out = blob.setBinaryStream(1); byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
out.close(); preparedStatement.setBlob(1, blob);
// preparedStatement.setBlob(1, in);
// preparedStatement.setBinaryStream(1, in); preparedStatement.setString(2, "ABCDE");
preparedStatement.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}

读取数据
/**
* getBlob 方法读取到 Blob 对象,调用 Blob 的 getBinaryStream() 方法得到输入流
* 或者直接 getBinaryStream 得到 IO 流
*/
@Test
public void testReadBlob() {
try {
String sql = "SELECT id, file, name FROM blob_test WHERE id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 13);
resultSet = preparedStatement.executeQuery(); if (resultSet.next()) {
int id = resultSet.getInt(1);
Blob file = resultSet.getBlob(2);
String name = resultSet.getString(3); InputStream in = file.getBinaryStream();
// InputStream in = resultSet.getBinaryStream(2);
System.out.println(name + "\t" + in.available()); OutputStream out = new FileOutputStream("newfile.jpg"); byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

MySQL 中无 Clob 类型,在 Oracle 中才有,可以在 MySQL 用 text 或者 varchar 替换,它相当于String
与 Blob 类型对比,MySQL官方文档
| Clob Type | Blob Type | Storage Required |
|---|---|---|
| TINYTEXT | TINYBLOB | L + 1 bytes,其中 L < 2**8 (255 B) |
| TEXT | BLOB | L + 2 bytes,其中 L < 2**16 (64 K) |
| MEDIUMTEXT | MEDIUMBLOB | L + 3 bytes,其中 L < 2**24 (16 MB) |
| LONGTEXT | LONGBLOB | L + 4 bytes,其中 L < 2**32 (4 GB) |
插入数据
/**
* 就是插入字符串
*/
@Test
public void testInsertClob() {
try {
Clob myClob = connection.createClob();
Writer clobWriter = myClob.setCharacterStream(1);
String str = readFile("clob.txt", clobWriter);
myClob.setString(1, str);
System.out.println("Clob 的长度:" + myClob.length()); String sql = "INSERT INTO clob_test (file, name) VALUES(?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setClob(1, myClob);
// preparedStatement.setString(1,str);
preparedStatement.setString(2, "ABCDE");
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
} private String readFile(String fileName, Writer writerArg) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(this.getClass().getClassLoader().getResource(fileName).getPath()));
String nextLine = "";
StringBuffer sb = new StringBuffer();
while ((nextLine = br.readLine()) != null) {
writerArg.write(nextLine);
sb.append(nextLine);
}
return sb.toString();
}
读取数据
@Test
public void testReadClob() {
try {
String sql = "SELECT id, file, name FROM clob_test WHERE id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 3);
resultSet = preparedStatement.executeQuery(); if (resultSet.next()) {
int id = resultSet.getInt(1);
Clob file = resultSet.getClob(2);
String name = resultSet.getString(3); InputStream in = file.getAsciiStream();
System.out.println(name + "\t" + in.available()); StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
sb.append(new String(buffer));
}
in.close(); // String str = resultSet.getString(2);
System.out.println(sb);
}
} catch (Exception e) {
e.printStackTrace();
}
}

Oracle JDBC 官方文档
6、JDBC-处理CLOB与BLOB的更多相关文章
- JDBC(二)之JDBC处理CLOB和BLOB及事务与数据库元数据获取
前面大概介绍了JDBC连接数据库的过程,以及怎么操作数据库,今天给大家分享JDBC怎么处理CLOB和BLOB存储图片的事情,以及JDBC怎么去处理事务.怎么在插入数据的时候生成主键返回值 一.JDBC ...
- JDBC处理CLOB 和 BLOB大对象
在数据库中: clob用于存储大量的文本数据 可以使用字符流操作 clob用于存储大量的二进制数据 可以使用字节流操作 以mysql为例 先准备一张表: CREATE TABLE `t_user2` ...
- [转载]JDBC读写Oracle的CLOB、BLOB
JDBC读写Oracle10g的CLOB.BLOB http://lavasoft.blog.51cto.com/62575/321882/ 在Oracle中存取BLOB对象实现文件的上传和下载 ht ...
- Oracle jdbc 插入 clob blob
Oracle 使用 clob 与 blob 插入一些比较庞大的文本或者文件,JDBC 插入时 也比较简单 表结构 CREATE TABLE test_info ( user_id int NOT NU ...
- Sqoop处理Clob与Blob字段
[Author]: kwu Sqoop处理Clob与Blob字段,在Oracle中Clob为大文本.Blob存储二进制文件. 遇到这类字段导入hive或者hdfs须要特殊处理. 1.oracle中的測 ...
- oracle存储大文本clob、blob
oracle存储大文本clob.blob 1 package cn.itcast.web.oracle.util; 2 3 import java.sql.Connection; 4 import j ...
- Spring JDBC处理CLOB类型字段
以下示例将演示使用spring jdbc更新CLOB类型的字段值,即更新student表中的可用记录. student表的结构如下 - CREATE TABLE student( ID INT NOT ...
- 小峰mybatis(1) 处理clob,blob等。。
一.mybatis处理CLOB.BLOB类型数据 CLOB:大文本类型:小说啊等大文本的:对应数据库类型不一致,有long等: BLOB:二进制的,图片:电影.音乐等二进制的: 在mysql中: bl ...
- 查询数据库中含clob,blob的表
查询含clob,blob的表select distinct ('TABLE "' || a.OWNER || '"."' || a.TABLE_NAME || '&quo ...
随机推荐
- mysql外键关联
主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性 外键:是另一表的主键, 外键可以有重复的, 可以是空值,用来和其他表建立联系用的.所以说,如果谈到了外键,一定是至少涉及到两张表 ...
- 运行scrapy crawl (文件名)时显示invalid syntax和no modle 'win32api'解决方案
使用pycharm爬取知乎网站的时候,在terminal端输入scarpy crawl zhihu,提示语法错误,如下: 原因是python3.7中将async设为关键字,根据错误提示,找到manho ...
- spring-web-4.3.3与spring-webmvc-4.3.3的区别
spring-web-4.3.3 http(http协议的实现类)和web包(应用,上下文,会话,cookies,过滤器等等) spring-webmvc-4.3.3 主要是一些view层的核心封装, ...
- JavaScript表单提交不能清空type为hidden的input快速解决方案
http://stackoverflow.com/questions/2559616/javascript-true-form-reset-for-hidden-fields 把input type= ...
- [转帖]Windows7 结束更新 以及后期更新花费。
你不应该为Windows 7更新付费的三个原因 https://www.linuxidc.com/Linux/2019-02/156777.htm 对Windows 7的支持将在2020年1月结束,这 ...
- MES模块
基础数据管理:产品模型.工厂模型.工艺模型 仓储管理 成本管理 绩效管理 看板管理 日志管理 设备管理:工装管理.设计器具管理.设备保养管理.设备备件管理.设备采集管理.设备点检管理.设备故障管理.设 ...
- 玩弄 python 正则表达式
这里记录一个我常用的模型,每次久了不使用正则就会忘记. 记得最好玩的一句关于正则表达式的话就是 当你想到一件事情可以用正则表达式解决的时候 现在你就面临了两个问题了. python里面使用了re模块对 ...
- python之tkinter使用举例-Button
tkinter用于编写GUI界面,python3默认已经包含,直接使用. # GUI:tkinter使用举例 import tkinter # 实例化tkinter对象 top = tkinter.T ...
- BZOJ4448[Scoi2015]情报传递——主席树+LCA
题目描述 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有n名情报员.每名情报员口J-能有 若T名(可能没有)下线,除1名大头目外其余n-1名情报员有且仅有1名上线.奈特公司纪律森严 ...
- c# 对象存cookie
下载并引用Newtonsoft.Json.dll 对象转json,然后存cookies string xxx= Newtonsoft.Json.JsonConvert.SerializeObject( ...
