zoukankan      html  css  js  c++  java
  • 向MySQL 中存储二进制数据文件

    package cn.itcast.demo;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    import org.junit.Test;
    
    import cn.itcast.utils.JdbcUtils;
    
    public class Demo2 {
    
        /**
         
         create table testblob
         (
             id int primary key auto_increment,
             image longblob
         );
         
         */
        
        //向 Mysql数据库中存储二进制文件
        @Test
        public void add(){
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            
            try{
                conn = JdbcUtils.getConnection();
                String sql = "insert into testblob(image) values(?)";
                st = conn.prepareStatement(sql);
                String path = Demo2.class.getClassLoader().getResource("01.jpg").getPath();
                st.setBinaryStream(1, new FileInputStream(path), (int) new File(path).length());
                int num = st.executeUpdate();
                if(num>0){
                    System.out.println("插入成功!!");
                }
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtils.release(conn, st, rs);
            }
        }
        
        @Test
        public void read(){
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            
            try{
                conn = JdbcUtils.getConnection();
                String sql = "select image from testblob where id=?";
                st = conn.prepareStatement(sql);
                st.setInt(1, 1);
                rs = st.executeQuery();
                if(rs.next()){
                    InputStream in = rs.getBinaryStream("image");
                    int len = 0;
                    byte buffer[] = new byte[1024];
                    
                    FileOutputStream out = new FileOutputStream("c:\1.jpg");
                    while((len=in.read(buffer))>0){
                        out.write(buffer,0, len);
                    }
                    in.close();
                    out.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtils.release(conn, st, rs);
            }
        }
    
    }
    package cn.itcast.demo;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    import org.junit.Test;
    
    import cn.itcast.utils.JdbcUtils;
    
    public class Demo2 {
    
        /**
         
         create table testblob
         (
             id int primary key auto_increment,
             image longblob
         );
         
         */
        
        //向 Mysql数据库中存储二进制文件
        @Test
        public void add(){
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            
            try{
                conn = JdbcUtils.getConnection();
                String sql = "insert into testblob(image) values(?)";
                st = conn.prepareStatement(sql);
                String path = Demo2.class.getClassLoader().getResource("01.jpg").getPath();
                st.setBinaryStream(1, new FileInputStream(path), (int) new File(path).length());
                int num = st.executeUpdate();
                if(num>0){
                    System.out.println("插入成功!!");
                }
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtils.release(conn, st, rs);
            }
        }
        
        @Test
        public void read(){
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            
            try{
                conn = JdbcUtils.getConnection();
                String sql = "select image from testblob where id=?";
                st = conn.prepareStatement(sql);
                st.setInt(1, 1);
                rs = st.executeQuery();
                if(rs.next()){
                    InputStream in = rs.getBinaryStream("image");
                    int len = 0;
                    byte buffer[] = new byte[1024];
                    
                    FileOutputStream out = new FileOutputStream("c:\1.jpg");
                    while((len=in.read(buffer))>0){
                        out.write(buffer,0, len);
                    }
                    in.close();
                    out.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtils.release(conn, st, rs);
            }
        }
    
    }
  • 相关阅读:
    关于 未能加载文件或程序集“ImageMagickNet”或它的某一个依赖项。试图加载格式不正确的程序 的解决办法
    Nhibernate中 ManyToOne 中lazy="proxy" 延迟不起作用的原因
    关于mysqlconnectornet6.3.4 MySqlDataAdapter 在空数据的情况下填充DataSet后tables[0] 找不到的问题
    JavaScript:constructor属性
    关于AspNetPager 采用URL分页时 执行两次绑定的解决办法
    WPF学习笔记(一)
    unity3d 屏幕坐标、鼠标位置、视口坐标和绘制GUI时使用的坐标
    FileUpLoad用法(二)上传文件到服务器的数据库
    ASP.Net 使用GridView模板删除一行的用法
    ASP.Net FileUpLoad 控件的用法(一)——上传到服务器文件夹下
  • 原文地址:https://www.cnblogs.com/lichone2010/p/3178670.html
Copyright © 2011-2022 走看看