Bolb存储照片

import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.sql.*;   
  
  
public class BlobTest {   
    public static void main(String args[]){   
        Connection conn = null;   
        Statement stmt = null;   
        PreparedStatement ps = null;   
        ResultSet rs = null;   
           
        try {   
            Class.forName("oracle.jdbc.driver.OracleDriver");                      
            //获取数据库对象   
            conn =  DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","用户名","密码"); 
            conn.setAutoCommit(false);  
            
            //表存在则删除它   
            String sql = "DROP TABLE IF EXISTS blobtest";//MySql
        
            sql = "declare num number; begin"+
            " select count(1) into num from user_tables where table_name = upper('blobtest');"+
            " if num > 0 then execute immediate 'drop table blobtest' ; end if;"+
            " end;";//Orcl
            
            stmt = conn.createStatement();   
            System.out.println(sql);   
            stmt.executeUpdate(sql);   
            //创建表   
            sql = "CREATE TABLE blobtest(" +    
            "b_id INT NOT NULL ,"+   
            "b_title VARCHAR(50),"+   
            "b_text Blob"  +
            ")";   
                
            System.out.println(sql);   
            stmt.executeUpdate(sql);   
            System.out.println("创建数据表成功!");   
               
            sql="INSERT INTO blobtest(B_ID,b_title,b_text)VALUES(?,?,?)";   
            ps = conn.prepareStatement(sql);   
            //插入图片  
            File file = new File("d://1.JPG");   
            InputStream inputStream = new FileInputStream(file);
               
            //以前的时候直接使用setBinaryStream就可以保存图片,但   
            //数据库的字符集换成 gbk后就不能用了,一直提示参数类型不匹配,在网上查了一下,   
            //直接向数据库中存byte可解决   
            //ps.setString(1, file.getName());   
            //ps.setBinaryStream(2,inputStream,file.length());   
            //ps.executeUpdate();   
            try {
                ps.setString(1, "1");   
                ps.setString(2, file.getName());   
                //新建一byte数组   
                byte[] buf=new byte[inputStream.available()];   
                //将文件读入到byte[]中   
                inputStream.read(buf);   
                ps.setBytes(3, buf);   
                ps.executeUpdate();   
                System.out.println("插入成功!");   
            } catch (IOException e1) {   
                System.out.println("保存图片到数据库成功!");   
                e1.printStackTrace();   
            }   
            conn.commit();  
               
               
            //读取数据   
            sql = "SELECT b_title,b_text FROM blobtest";   
            ps = conn.prepareStatement(sql);   
            rs = ps.executeQuery();   
            while(rs.next()){   
                System.out.println("图片名: "+rs.getString("b_title"));   
                Blob blob = rs.getBlob("b_text");   
                File file2 = new File("d://2.jpg");   
                OutputStream outputStream = new FileOutputStream(file2);   
                try {   
                    outputStream.write(blob.getBytes(1,(int)blob.length()));   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
                //打印出来的为对象   
                System.out.println("图片内容: "+ blob.getBinaryStream());   
                   
            }   
        } catch (Exception e) {   
            e.printStackTrace();     
        }finally{   
            
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }   
        }   
           
    }   
  
} 

 

 

 

第二种:

import java.sql.*;  
import oracle.sql.*;  
import java.io.*;  

import oracle.jdbc.driver.OracleResultSet;  

public class WriteBlob {  
    public static void main(String[] args) {  
        try {  
            // 连接数据库
            Class.forName("oracle.jdbc.driver.OracleDriver");                      
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","用户名","密码");  
            conn.setAutoCommit(false);  
            
            //获取源照片数据  
            BLOB blob = null;  
            String fileName = "d://wenpaiyi//1.JPG";  
            File f = new File(fileName);  
            
            //先插入一条记录,用于后面获得一个Blob对象  
            PreparedStatement pstmt = conn.prepareStatement("insert into blobtest(b_id,b_text) values(?,empty_blob())");  
            pstmt.setString(1,"1");  
            pstmt.executeUpdate();  
            pstmt.close();  
            
            // 获得Blob对象(由于Blob是接口,不能实例化,所以只好用这种曲折方法获得)  
            pstmt = conn.prepareStatement("SELECT b_text FROM blobtest WHERE trim(b_id)=? ");  
            pstmt.setString(1,"1");  
            ResultSet rset = pstmt.executeQuery();  
            if (rset.next()){ 
                blob = (BLOB)rset.getBlob(1);  
            }
            pstmt.close();  
            if (blob == null){  
                System.out.println("blob is null");  
                conn.close();  
                return ;  
            }  
            
            // 填充Blob值,用于提交到数据库  
            FileInputStream fin = new FileInputStream(f);  
            System.out.println("file size = " + fin.available());  
            OutputStream out = blob.getBinaryOutputStream();  
            byte[] data = new byte[(int)fin.available()];  
            // 获取  
            fin.read(data);  
            out.write(data);  
            // 关闭资源    
            fin.close();  
            out.close(); 
            
            // 插入数据库  
            pstmt = conn.prepareStatement("update blobtest set b_text=? where b_id=?");  
            pstmt.setBlob(1,blob);  
            pstmt.setString(2,"1");  
            pstmt.executeUpdate();  
            pstmt.close();  
            // 提交  
            conn.commit(); 
            
            //---------------------------------------------------读取-------------------------------------
            String readSql = "SELECT b_text FROM blobtest WHERE trim(b_id)=? ";  
            PreparedStatement ps = conn.prepareStatement(readSql);  
            ps.setString(1, "1");  
            ResultSet rs = ps.executeQuery();  
            while (rs.next()) {  
                Blob image = rs.getBlob("b_text");  
                DataOutputStream dos =   
                    // 在FileOutputStream中指定文件输出路径  
                    new DataOutputStream(new FileOutputStream(new File("d://wenpaiyi//2.JPG")));  
                InputStream fis = image.getBinaryStream();  
                int out1;  
                byte[] outByte = new byte [100];  
                // 将blob对象输入流写入本地输出流中  
                while ((out1 = fis.read(outByte)) != -1) {  
                    dos.write(outByte);  
                }  
                fis.close();  
                dos.flush();  
                dos.close();  
            }  
            rs.close();  
            ps.close();  
            
            //---------------------------------------------------读取------------------------------------- 
            pstmt = conn.prepareStatement("SELECT b_text FROM blobtest WHERE trim(b_id)=? ");  
            pstmt.setString(1,"1");  
            rset = pstmt.executeQuery();  
            if (rset.next()){
                blob = (BLOB)((OracleResultSet)rset).getBLOB(1);  
            }
            pstmt.close();  
            if (blob == null){  
                System.out.println("blob2 is null");  
                conn.close();  
                return ;  
            }  
            // 输出到磁盘  
            // 获得  
            FileOutputStream fout = new FileOutputStream(new File("d://wenpaiyi//3.JPG"));  
            InputStream in = blob.getBinaryStream();  //asciiStreamValue()
            
            int out2 ;
            byte [] outbyte = new byte [100];
            
            while((out2 = in.read(outbyte)) != -1){
                fout.write(outbyte);
            }
            in.close();
            fout.flush();
            fout.close();
            
            conn.close();  
            // 打开  
            //Process pro = Runtime.getRuntime().exec("cmd /c start D:\\2.jpg");  
        }catch   (SQLException   e)   {  
            System.err.println(e.getMessage());  
            e.printStackTrace();  
        }catch(IOException   e)   {  
            System.err.println(e.getMessage());  
        }catch(ClassNotFoundException   e){  
            e.printStackTrace();  
        }  
    }  
}

 

转载于:https://www.cnblogs.com/zdf159/p/7511471.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值