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的更多相关文章

  1. JDBC(二)之JDBC处理CLOB和BLOB及事务与数据库元数据获取

    前面大概介绍了JDBC连接数据库的过程,以及怎么操作数据库,今天给大家分享JDBC怎么处理CLOB和BLOB存储图片的事情,以及JDBC怎么去处理事务.怎么在插入数据的时候生成主键返回值 一.JDBC ...

  2. JDBC处理CLOB 和 BLOB大对象

    在数据库中: clob用于存储大量的文本数据 可以使用字符流操作 clob用于存储大量的二进制数据 可以使用字节流操作 以mysql为例 先准备一张表: CREATE TABLE `t_user2` ...

  3. [转载]JDBC读写Oracle的CLOB、BLOB

    JDBC读写Oracle10g的CLOB.BLOB http://lavasoft.blog.51cto.com/62575/321882/ 在Oracle中存取BLOB对象实现文件的上传和下载 ht ...

  4. Oracle jdbc 插入 clob blob

    Oracle 使用 clob 与 blob 插入一些比较庞大的文本或者文件,JDBC 插入时 也比较简单 表结构 CREATE TABLE test_info ( user_id int NOT NU ...

  5. Sqoop处理Clob与Blob字段

    [Author]: kwu Sqoop处理Clob与Blob字段,在Oracle中Clob为大文本.Blob存储二进制文件. 遇到这类字段导入hive或者hdfs须要特殊处理. 1.oracle中的測 ...

  6. oracle存储大文本clob、blob

    oracle存储大文本clob.blob 1 package cn.itcast.web.oracle.util; 2 3 import java.sql.Connection; 4 import j ...

  7. Spring JDBC处理CLOB类型字段

    以下示例将演示使用spring jdbc更新CLOB类型的字段值,即更新student表中的可用记录. student表的结构如下 - CREATE TABLE student( ID INT NOT ...

  8. 小峰mybatis(1) 处理clob,blob等。。

    一.mybatis处理CLOB.BLOB类型数据 CLOB:大文本类型:小说啊等大文本的:对应数据库类型不一致,有long等: BLOB:二进制的,图片:电影.音乐等二进制的: 在mysql中: bl ...

  9. 查询数据库中含clob,blob的表

    查询含clob,blob的表select distinct ('TABLE "' || a.OWNER || '"."' || a.TABLE_NAME || '&quo ...

随机推荐

  1. Undertow的InMemorySessionManager

    https://github.com/undertow-io/undertow/blob/master/core/src/main/java/io/undertow/server/session/In ...

  2. Install alipay支付宝安全控件 on firefox in linux

    [root@rgqancy 下载]# ./aliedit.sh建议以非root账号安装支付宝安全控件请重启   firefox   使插件生效成功安装 支付宝安全控件请按任意键退出... what i ...

  3. laravel5 报错419,form 添加crrf_field 后让然失败,本地环境配置问题

    这个是因为laravel自带CSRF验证的问题 解决方法 方法一:去关掉laravel的csrf验证,但这个人不建议,方法也不写出来了. 方法二:把该接口写到api.php上就好了 方法三: 首先在页 ...

  4. python模块_多重继承的MRO

    MRO(Method Resolution Order):方法解析顺序.Python语言包含了很多优秀的特性,其中多重继承就是其中之一,但是多重继承会引发很多问题,比如二义性,Python中一切皆引用 ...

  5. mysql学习笔记三 —— 数据恢复与备份

    要点: 1.存储引擎2.导入导出3.备份与恢复 查看当前数据库中的所有表use db1:show tables: 1.存储引擎 不同的发动机(引擎)适用的汽车类型不一样. 存储和处理的不同方式.不同的 ...

  6. Maven Archetype简介以及搭建

    为什么会写这篇文章,因为公司先在构建项目骨架都是用的 maven archetype ,身为一个上进的渣渣猿,自己还是有必要了解下这个东西的. Archetype介绍 Archetype 是一个 Ma ...

  7. node upgrade bug & node-sass

    node upgrade bug & node-sass bug solution rebuild $ npm rebuild node-sass OK

  8. postgres(pgAdmin) 客户端保存密码

    pgAdmin 大象客户端保存密码后连接服务器,删除掉当前连接,建立一个新的连接不用输入密码也能连接上,其实是客户端保存了密码,让人误以为是空密码可登录.可以通过右键连接,选择重载服务配置,再次连接就 ...

  9. Network of Schools POJ - 1236(强连通+缩点)

    题目大意 有N个学校,这些学校之间用一些单向边连接,若学校A连接到学校B(B不一定连接到A),那么给学校A发一套软件,则学校B也可以获得.现给出学校之间的连接关系,求出至少给几个学校分发软件,才能使得 ...

  10. Dining POJ - 3281

    题意: f个食物,d杯饮料,每个牛都有想吃的食物和想喝的饮料,但食物和饮料每个只有一份 求最多能满足多少头牛.... 解析: 一道简单的无源汇拆点最大流   无源汇的一个最大流,先建立超级源s和超级汇 ...