向数据表中插入大数据Blob类型实例及两种查看方式

22 篇文章 0 订阅
8 篇文章 0 订阅

插入前:

 插入成功:

 插入后:

方法一:手动查询

 可以看到图片插入是插入了,但是无法直接查看,需要在查询窗口手动输入查询语句:

-- 查看插入的Blob类型数据
SELECT photo FROM customers WHERE id =21;

代码:

//向数据表customers中插入Blob类型的字段
    @Test    
    public void testInsert() throws Exception {
        //获取连接
        Connection conn = JDBCUtils.getConnection();

        String sql = "insert into customers(name,email,birth,photo)values(?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);

// 填充占位符
        ps.setString(1, "孙悟空");
        ps.setString(2, "swk@126.com");
        ps.setDate(3, new Date(new java.util.Date().getTime()));
        // 操作Blob类型的变量
        FileInputStream fis = new FileInputStream("E:\\IdeaProjects\\JDBC\\src\\孙悟空.jpg ");
        ps.setBlob(4, fis);
//执行
        ps.execute();

        fis.close();
        JDBCUtils.closeResource(conn, ps);
        System.out.println("插入成功!");
    }

从数据表中读取大数据类型

 方法二:读取保存到本地后查看Blob类型数据

 这样就不会压缩画质啦,而且图片自适应

//查询数据表customers中Blob类型的字段
    @Test
    public void testQuery() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select id, name, email, birth, photo FROM customers WHERE id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 21);

            rs = ps.executeQuery();
            if (rs.next()) {
                //方式一:顺序不能颠倒,不灵活
                //        int id = rs.getInt(1);
                //        String name = rs.getString( 2);
                //        String email = rs.getString(3);
                //        Date birth = rs.getDate(4);

                //        方式二:相当于别名,可以颠倒顺序【推荐】
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String email = rs.getString("email");
                Date birth = rs.getDate("birth");

                Customers cu = new Customers(id, name, email, birth);
                System.out.println(cu);

                //  将Blob类型的字段下载下来,以文件的形式保存在本地
                Blob photo = rs.getBlob("photo");
                InputStream is = photo.getBinaryStream();
                FileOutputStream fos = new FileOutputStream("1.jpg");
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }

                is.close();
                fos.close();
                JDBCUtils.closeResource(conn, ps, rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

以上就是两种查看数据表中插入大数据Blob类型的方法。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值