Jdbc扩展---Jdbc处理二进制数据

 

Jdbc扩展---Jdbc处理二进制数据

一  使用目的

一般是向数据库库中插入图片和从数据库中读取图片

二  代码介绍

1.向数据库中插入图片

   public void insert() {

       Connection con = null;

       PreparedStatement st = null;

       ResultSet rs = null;

        try {

           // 获取连接

           con = DBManager.getConnection();

           // 定义sql语句

           String sql = "insert into testblob (image) value(?)";

           // 创建预处理对象

           st = con.prepareStatement(sql);

           // 为占位符赋值

           File f = new File("1.jpg");

           st.setBinaryStream(1, new FileInputStream(f), f.length());

           // 执行更新语句

           int result = st.executeUpdate();

           // 判断

           if (result > 0) {

              System.out.println("插入成功");

           } else {

              System.out.println("插入失败");

           }

       } catch (SQLException e) {

           e.printStackTrace();

       } catch (FileNotFoundException e) {

           e.printStackTrace();

       }finally{

           DBManager.release(con, st, rs);

}

    }

    注:想数据库中输入图片最重要的是:

        File f = new File("1.jpg");

       st.setBinaryStream(1, new FileInputStream(f), f.length());

2.从数据库中读取图片

    public void find() {

       Connection con = null;

       PreparedStatement st = null;

       ResultSet rs = null;

       try {

           // 获取连接

           con = DBManager.getConnection();

           // 定义sql语句

           String sql = "select image from testblob where id=1";

           // 创建预处理对象

           st = con.prepareStatement(sql);

           // 执行更新

           rs = st.executeQuery();

           // 判断

           if (rs.next()) {

              // 读取

              InputStream fis = rs.getBinaryStream("image");

              // 定义缓冲区,读取到缓冲区 中

              byte[] buff = new byte[1024];

              int len = 0;

              FileOutputStream fos = new FileOutputStream("3.jpg");

              while ((len = fis.read(buff)) > 0) {

                  fos.write(buff, 0, len);

              }

           }

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }finally{

           DBManager.release(con, st, rs);

}

}

注:最重要的部分:

    if (rs.next()) {

              // 读取

              InputStream fis = rs.getBinaryStream("image");

              // 定义缓冲区,读取到缓冲区 中

              byte[] buff = new byte[1024];

              int len = 0;

              FileOutputStream fos = new FileOutputStream("3.jpg");

              while ((len = fis.read(buff)) > 0) {

                  fos.write(buff, 0, len);

           }

    }

 

   注:配置文件在上一篇有附加

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值