BLOB类型插入数据的设计思路和处理流程

【概述】  

    Oracle的Blob字段比较特殊,他比long字段的性能要好很多,可以用来保存例如图片之类的二进制数据。 

写入Blob字段和写入其它类型字段的方式非常不同,因为Blob自身有一个cursor,你必须使用cursor对 

blob进行操作,因而你在写入Blob之前,必须获得cursor才能进行写入,那么如何获得Blob的cursor呢? 

这需要你先插入一个empty的blob,这将创建一个blob的cursor,然后你再把这个empty的blob的cursor 

用select查询出来,这样通过两步操作,你就获得了blob的cursor,可以真正地写入blob数据了。 

【处理流程】  

Sql代码   收藏代码
  1. --Oracle中的Lob类型示例表  
  2.   
  3. create table user_info   
  4. (  
  5.   user_id number(10) primary key,  
  6.   name varchar2(20),  
  7.   image blob  
  8. );  
  9.   
  10. --1. 插入空blob: (如果在数据库中采用默认值方式对Blob字段赋值, 此步可省略)  
  11.   
  12.    insert into user_info values (1, 'Jacky', empty_blob());  
  13.   
  14. --2. 获得blob的cursor:  
  15.     
  16.    select image from user_info where user_id = ? for update;  
  17.   
  18. --3. 用cursor往数据库写数据:  
  19.   
  20.    update user_info set image = ? where user_id = ?;  


Java代码   收藏代码
  1. //读取Blob数据  
  2. package demo;  
  3.   
  4. import java.sql.*;  
  5. import java.io.*;  
  6.   
  7. public class ReadBlob  
  8. {  
  9.     //加载驱动程序  
  10.     static   
  11.     {  
  12.           
  13.         try  
  14.         {  
  15.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  16.         } catch (ClassNotFoundException e)  
  17.         {  
  18.             // TODO Auto-generated catch block  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22.       
  23.     public static void main(String[] args)  
  24.     {  
  25.         try  
  26.         {  
  27.             //1. 建立连接  
  28.             String url = "jdbc:oracle:thin:@localhost:1521:OracleDB";  
  29.             Connection conn = DriverManager.getConnection(url,"scott","tiger");  
  30.             conn.setAutoCommit(false);  
  31.               
  32.             //2. 查询数据  
  33.             String sql = "select image from user_info where user_id = 1";  
  34.             Statement stmt = conn.createStatement();  
  35.             ResultSet rs = stmt.executeQuery(sql);  
  36.               
  37.             //3. 读取Blob类型数据  
  38.             Blob blob = null;  
  39.             if(rs.next())  
  40.             {  
  41.                 blob = rs.getBlob(1);  
  42.             }  
  43.             byte[] temp = new byte[(int)blob.length()];  
  44.             InputStream in = blob.getBinaryStream();  
  45.             in.read(temp);  
  46.             File file = new File("D:\\img.bmp");  
  47.             FileOutputStream fout = new FileOutputStream(file);  
  48.             fout.write(temp);  
  49.             in.close();  
  50.             fout.close();  
  51.         } catch (Exception e)  
  52.         {  
  53.             // TODO Auto-generated catch block  
  54.             e.printStackTrace();  
  55.         }  
  56.     }  
  57. }  



Java代码   收藏代码
  1. //写Blob数据  
  2. package demo;  
  3.   
  4. import java.sql.*;  
  5. import oracle.sql.BLOB;//▲此处的BLOB类全大写, 而java.sql.Blob中的Blob非全大写  
  6. import java.io.*;  
  7.   
  8. public class WriteBlob  
  9. {  
  10.     //加载驱动程序  
  11.     static   
  12.     {  
  13.         try  
  14.         {  
  15.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  16.         }  
  17.         catch(Exception e)  
  18.         {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22.     public static void main(String[] args)  
  23.     {  
  24.         try  
  25.         {  
  26.             //1. 建立与数据库服务器的连接  
  27.             String url = "jdbc:oracle:thin:@localhost:1521:OracleDB";  
  28.             Connection conn = DriverManager.getConnection(url,"scott","tiger");  
  29.             conn.setAutoCommit(false);  
  30.               
  31.             //2. 首先向表中插入空的Blob  
  32.             //★注意: 对于empty_blob()应放在SQL语句中直接赋值, 使用预置语句的方式赋值无法实现.  
  33.             String sql = "insert into user_info values(?,?,empty_blob())";  
  34.             PreparedStatement ps = conn.prepareStatement(sql);  
  35.             ps.setInt(11);  
  36.             ps.setString(2"Lucy");  
  37.             ps.executeUpdate();  
  38.               
  39.             //3. 查询Blob, 获得Blob的Cursor  
  40.             sql = "select image from user_info where user_id = ?";  
  41.             ps = conn.prepareStatement(sql);  
  42.             ps.setInt(11);  
  43.             ResultSet rs = ps.executeQuery();  
  44.             BLOB blob = null;  
  45.             if(rs.next())  
  46.             {  
  47.                 blob = (BLOB)rs.getBlob(1);  
  48.             }  
  49.               
  50.             //4. 使用字节流将待入库的文件写入到blob中  
  51.             File file = new File("D:\\iriver\\sample1.bmp");  
  52.             FileInputStream fin = new FileInputStream(file);  
  53.             byte[] temp = new byte[fin.available()];  
  54.             fin.read(temp);  
  55.             OutputStream out = blob.getBinaryOutputStream();  
  56.             out.write(temp);  
  57.             fin.close();  
  58.             out.close();  
  59.               
  60.             //5. 向数据库中写入数据  
  61.             sql = "update user_info set image = ? where user_id = ?";  
  62.             ps = conn.prepareStatement(sql);  
  63.             ps.setBlob(1, blob);  
  64.             ps.setInt(21);  
  65.             ps.executeUpdate();  
  66.               
  67.             conn.commit();  
  68.         } catch (Exception e)  
  69.         {  
  70.             e.printStackTrace();  
  71.         }  
  72.     }  
  73. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值