zoukankan      html  css  js  c++  java
  • jdbc读取数据库图片文件

    package 读取大文件.read;
    
    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    import org.junit.Test;
    
    import 配置方式.dbUtils.Connection2;
    /**
     * 读取数据库中的问件
     * @author lx
     *
     */
    
    public class ReadBlob {
        @Test
    
        public void readBlob() throws Exception {
            
    
            // sql
            String sql = "select * from stud where id=77";
            // 读取不接受参数可以直接用statment
    
            Statement st = Connection2.GetCon().createStatement();
    
            // 执行查询
            ResultSet rs = st.executeQuery(sql);
            // 确定只有一行
            if (rs.next()) {
                int id = rs.getInt("id");
                // 获取图片
                InputStream in = rs.getBinaryStream("img");// 数据库字段
                // 声明out
                OutputStream out = new FileOutputStream("q:/b.jpg");
    
                byte[] b = new byte[1024];
                int len = 0;
                while ((len = in.read(b)) != -1) {
                    out.write(b, 0, len);
    
                }
                out.close();
                // --------------------------
                // 显示文本
                InputStream in2 = rs.getAsciiStream("id");
    
                BufferedReader br = new BufferedReader(new InputStreamReader(in2));
    
                String line = null;
                while ((line = br.readLine()) != null) {
                    System.err.println(line);
    
                }
                br.close();
                in2.close();
    
            }
    
        }
    
    }
  • 相关阅读:
    matlab--“下标索引必须为正整数类型或逻辑类型”
    将中缀表达式转化为后缀表达式
    MATLAB那些常见的命令
    关于实现线程同步的几种方式
    关于http协议
    小白学习之activiti工作流入门
    小白- jquery 学习笔记
    小白-Javascript学习笔记
    小白-css笔记
    小白- html笔记
  • 原文地址:https://www.cnblogs.com/xiaweifeng/p/3689059.html
Copyright © 2011-2022 走看看