JDBC(8)—Blob

来源:互联网 发布:区域增长算法 编辑:程序博客网 时间:2024/06/03 22:56
  1. Blob
  2. LOB,即:Large Objects(大对象),是用来存储大量的二进制和文本数据的一种数据类型(一个lob字段可以存储多达四个G的数据)。LOB分为两种类型:内部LOB和外部LOB
  3. ——内部LOB将数据以字节流的形式存储在数据库的内部,因而,内部LOB的许多操作都可以参与事物,
    *也可以像处理普通数据一样对其进行备份和恢复操作。
  4. Oracle支持三种类型的内部LOB:
    BLOB(二进制数据)
    CLOB(单字节字符数据)
    NCLOB(多字节字符数据)
  5. ——CLOB和NCLOB适用于存储超长的文本数据,BLOB适用于存储大量的二进制数据,如图像、视频、音频文件等
  6. ——目前只支持一种外部LOB类型,在数据库内,该类型仅存储数据,在操作系统中的位置信息,而数据的实体以外部文件的形式存在操作系的文件系统中,因而,该类型所表示的数据是只读的,不参与事物,该类型可以帮助用户管理大量的有外部程序访问的文件。

  7. MySql BLOB类型:
    *MySql中,BLOB是一个二进制大型对象,是一个可以存储大量数据的容器,它能容纳大小不同的数据。

  8. MySql的四种BLOB类型:
    *TinyBlob:255B
    *Blob:65K
    *MediumBlob:16M
    *LongBlob:4G
  9. ——注意:实际生活中根据需要存进的数据大小定义不同的Blob类型,若存储的文件过大,数据库性能会降低。
  10. 实例:
public class Blob_9 {    /**     * 插入一张图片     * 插入Blob类型的数据必须使用PreparedStatement,因为Blob类型的数据无法用字符串拼写。     */    @Test    public void testWriteBlob(){        Connection conn = null;        PreparedStatement preparedstatement = null;        try {            conn = TestTools.getConnection();            String sql = "insert into customers(name,age,birth,address,picture)" +                    " values(?,?,?,?,?)";            preparedstatement = conn.prepareStatement(sql);            preparedstatement.setString(1, "王仲");            preparedstatement.setInt(2, 24);            preparedstatement.setDate(3, new Date(new java.util.Date().getTime()));            preparedstatement.setString(4, "上海市");            InputStream inputstream = new FileInputStream("Image/1.jpg");            preparedstatement.setBlob(5, inputstream);            preparedstatement.executeUpdate();        } catch (Exception e) {            e.printStackTrace();        }finally{            TestTools.release(preparedstatement, conn);        }    }    /**     * 从数据库读出一张图片     */    @Test    public void testReadBlob(){        Connection conn = null;        PreparedStatement ps = null;        ResultSet rs = null;        try {            conn = TestTools.getConnection();            String sql = "select id, name, age, birth, address, picture from " +                    "customers where id = ?";            ps = conn.prepareStatement(sql);            ps.setInt(1, 20);            rs  = ps.executeQuery();            if(rs.next()){                int id = rs.getInt(1);                String name = rs.getString(2);                int age = rs.getInt(3);                Date birth = rs.getDate(4);                String address = rs.getString(5);                System.out.println(id+", "+name+", "+age+", " +                        ""+birth+", "+address);                //接收图片                Blob picture = rs.getBlob(6);                InputStream in = picture.getBinaryStream();                OutputStream out = new FileOutputStream("Image/ongmao.jpg");                byte[] b = new byte[1024];                int len = 0;                while((len = in.read(b)) != -1){                    out.write(b, 0, len);                }                out.close();                in.close();            }        } catch (Exception e) {            e.printStackTrace();        }finally{            TestTools.release(rs, ps, conn);        }    }}