MySQL 中Blob类型数据的插入和读取

MySQL 中Blob类型数据的插入和读取在 MySQL 中 Blob 是一个二进制的对象 它是一个可以存储大量数据的容器 如图片 音乐等等 且能容纳不同大小的数据 在 MySQL 中有四种 Blob 类型 他们的区别就是可以容纳的信息量不容分别是以下四种 nbsp nbsp nbsp TinyBlob 类型 nbsp 最大能容纳 255B 的数据 nbsp nbsp nbsp Blob 类型 nbsp 最大能容纳 65KB 的 nbsp nbsp nbsp MediumBlob 类型 nbsp 最大能容纳 16MB 的数据 nbsp

在MySQL中Blob是一个二进制的对象,它是一个可以存储大量数据的容器(如图片,音乐等等),且能容纳不同大小的数据,在MySQL中有四种Blob类型,他们的区别就是可以容纳的信息量不容分别是以下四种:
      ①TinyBlob类型  最大能容纳255B的数据
      ②Blob类型  最大能容纳65KB的
      ③MediumBlob类型  最大能容纳16MB的数据
      ④LongBlob类型  最大能容纳4GB的数据
      而在我们实际使用的时候,可以根据自己的需求选择这几种类型,但是如果Blob中存储的文件的大小过大的话,会导致数据库的性能很差。下面具体介绍一下插入Blob类型的数据以及读取Blob类型的数据的方式:
1、插入Blob类型的数据
      插入Blob类型的数据时,需要注意必须要用PreparedStatement,因为Blob类型的数据是不能够用字符串来拼的,在传入了SQL语句后,就需要去调用PreparedStatement对象中的setBlob(int index , InputStream in)方法来设置传入的的参数,其中index表示Blob类型的数据所对应的占位符(?)的位置,而InputStream类型的in表示被插入文件的节点流。
2、读取Blob类型的数据
      读取Blob类型相对来说比较容易,当获取了查询的结果集之后,使用getBlob()方法读取到Blob对象,然后调用Blob的getBinaryStream()方法得到输入流,再使用IO操作进行文件的写入操作即可。
下面是具体例子来实现Blob类型数据的插入和读取操作:
现在本地数据库中有这样一个表animal,其中picture中存放的数据类型为MediumBlob类型
MySQL 中Blob类型数据的插入和读取



现在要向其中插入一个图片,具体代码如下
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.junit.Test; public class TestBlob { @Test public void getBlob(){//读取Blob数据 Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try { con = JDBCTools.getConnection(); String sql = "SELECT id,name,age,picture FROM animal WHERE id=5"; ps = con.prepareStatement(sql); rs = ps.executeQuery(); if(rs.next()){ int id = rs.getInt(1); String name = rs.getString(2); int age = rs.getInt(3); Blob picture = rs.getBlob(4);//得到Blob对象 //开始读入文件 InputStream in = picture.getBinaryStream(); OutputStream out = new FileOutputStream("cat.png"); byte[] buffer = new byte[1024]; int len = 0; while((len = in.read(buffer)) != -1){ out.write(buffer, 0, len); } } } catch (Exception e) { e.printStackTrace(); } } @Test public void insertBlob(){//插入Blob Connection con = null; PreparedStatement ps = null; try { con = JDBCTools.getConnection(); String sql = "INSERT INTO animal(name,age,picture) VALUES(?,?,?)"; ps = con.prepareStatement(sql); ps.setString(1, "TheCat"); ps.setInt(2, 8); InputStream in = new FileInputStream("J:/test1/TomCat.png");//生成被插入文件的节点流 //设置Blob ps.setBlob(3, in); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally{ JDBCTools.release(con, ps); } } class JDBCTools {//JDBC工具类 用来建立连接和释放连接 public static Connection getConnection() throws Exception{//连接数据库 String driverClass = null; String url = null; String user = null; String password = null; Properties properties = new Properties(); InputStream in = Review.class.getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(in); driverClass = properties.getProperty("driver"); url = properties.getProperty("jdbcurl"); user = properties.getProperty("user"); password = properties.getProperty("password"); Class.forName(driverClass); return DriverManager.getConnection(url, user, password); } public static void release(Connection con , Statement state){//关闭数据库连接 if(state != null){ try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(con != null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void release(ResultSet rs , Connection con , Statement state){//关闭数据库连接 if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(state != null){ try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if(con != null){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } public class Review { public Connection getConnection() throws Exception{//连接数据库 String driverClass = null; String url = null; String user = null; String password = null; Properties properties = new Properties(); InputStream in = Review.class.getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(in); driverClass = properties.getProperty("driver"); url = properties.getProperty("jdbcurl"); user = properties.getProperty("user"); password = properties.getProperty("password"); Class.forName(driverClass); return DriverManager.getConnection(url, user, password); } }
 完成上述插入操作后,结果如下
MySQL 中Blob类型数据的插入和读取
完成读取操作后  在工作区间会出现一个图片  如下
MySQL 中Blob类型数据的插入和读取

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/207862.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午12:55
下一篇 2026年3月19日 下午12:55


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号