mysql中写入blob类型的方法

1、BLOB类型
1、BLOB类型介绍
  • (1)BLOB类型的含义
    BLOB (binary large object),用来存储二进制大对象的字段类型。
    BLOB往往是一个大文件,典型的BLOB是一张图片、一个声音或一个视频文件,由于它们的尺寸,必须使用特殊的方式来处理(例如:上传、下载或者存放到一个数据库)。
    处理BLOB的主要思想就是让文件处理器(如数据库管理器)不去理会文件是什么,而是关心如何去处理它。但也有专家强调,这种处理大数据对象的方法是把双刃剑,它有可能引发一些问题,如存储的二进制文件过大,会使数据库的性能下降。

    (2)BLOB类型系列
    MySQL中,BLOB是个类型系列,共包括四种BLOB类型:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储文件的最大尺寸不同。

    字段类型 最大长度(字节)   存储需求
    TinyBlob 255   值的长度加上用于记录长度的1个字节(8位)
    Blob 65K值的长度加上用于记录长度的2个字节(16位)
    MediumBlob 16M值的长度加上用于记录长度的3个字节(24位)
    LongBlob 4G   值的长度加上用于记录长度的4个字节(32位)


    二进制文件的处理:
     1.创建blob。
     2.向blob中写入字节。
     3.将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操作进行文件的写入操作即可。


    代码如下
    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);
           }
           
    }

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值