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);
            }
        }
    
    }
  • 相关阅读:
    IOS UIPickView+sqlite 选择中国所有城市案例
    IOS Sqlite用户界面增删改查案例
    IOS sqlite数据库增删改查
    IOS基于新浪微博开放平台微博APP
    ASIHTTPRequest 对GET POST 请求简单封装
    装饰器模式
    策略模式
    网站加入QQ聊天链接
    Spring AOP详解
    Maven搭建多模块企业级项目
  • 原文地址:https://www.cnblogs.com/lichone2010/p/3178670.html
Copyright © 2011-2022 走看看