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. sprint最后冲刺-out to out

    摘要:团队合作.实现四则APP,上传代码到github. 1.之前我们队一直无法把代码上传到github.直到今天.找到了一种可以协助代码上github的软件msysgit. 经过:(一行行看) 我们 ...

  2. 关于HashMap和Hashtable的区别

    Hashtable的应用非常广泛,HashMap是新框架中用来代替Hashtable的类,也就是说建议使用HashMap,不要使用Hashtable.可能你觉得Hashtable很好用,为什么不用呢? ...

  3. mongoDB的配置和使用

    如何启动mongodb? mongod --dbpath C:\appStore\mongodata //数据库地址 再开一个cmder窗口 进入C:\Program Files\MongoDB\Se ...

  4. SQLSERVER最简单的同名数据库恢复过程.

    一. 冷备份恢复 1. net stop mssqlserver # 如果是安装的默认数据库实例 关闭 sqlserver的数据库 2. copy sqlserver的数据文件 主要是mdf 数据文件 ...

  5. number (2)编译错 (类的大小写错误) Filewriter cannot be resolved to a type

    没找到所使用的类所在的类定义,一般常见于使用了外部jar中的类,但有对应的import语句.比如,如果程序中使用了ArrayList这个类,但你程序类文件的最开始import部分如果没有import  ...

  6. timescale 时间尺度

    1 `timescale为模块指定参考时间单位 `timescale<reference_time_unit>/<time_precision> 2 module endmou ...

  7. IBM推出新一代云计算技术来解决多云管理

    IBM 云计算论坛在南京举行,推出了一项全新的开放式技术,使用户能够更加便捷地跨不同云计算基础架构来管理.迁移和整合应用. IBM 多云管理解决方案(Multicloud Manager)控制面板 据 ...

  8. multi_index_container 多索引容器

    multi_index_container是c++ boost库中的一个多索引的容器.因工作中用到了,特来测试试用. #include "stdafx.h" #include &q ...

  9. BZOJ4530[Bjoi2014]大融合——LCT维护子树信息

    题目描述 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够 联通的树上路过它的简单路径的数 ...

  10. POJ1019-Number Sequence-数数。。

    1 12 123 1234 把数按照这样的形式拍成一排,给一个序号求出那个序号对应的数. 当出现两位数.三位数时,要麻烦的处理一下. #include <cstdio> #include ...