Oracle数据库BLOB字段的存取

述】
    Oracle的Blob字段比较特殊,他比long字段的性能要好很多,可以用来保存例如图片之类的二进制数据。
写入Blob字段和写入其它类型字段的方式非常不同,因为Blob自身有一个cursor,你必须使用cursor对
blob进行操作,因而你在写入Blob之前,必须获得cursor才能进行写入,那么如何获得Blob的cursor呢?
这需要你先插入一个empty的blob,这将创建一个blob的cursor,然后你再把这个empty的blob的cursor
用select查询出来,这样通过两步操作,你就获得了blob的cursor,可以真正地写入blob数据了。
【处理流程】

  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 = ?;  
--Oracle中的Lob类型示例表

create table user_info 
(
  user_id number(10) primary key,
  name varchar2(20),
  image blob
);

--1. 插入空blob: (如果在数据库中采用默认值方式对Blob字段赋值, 此步可省略)

   insert into user_info values (1, 'Jacky', empty_blob());

--2. 获得blob的cursor:
  
   select image from user_info where user_id = ? for update;

--3. 用cursor往数据库写数据:

   update user_info set image = ? where user_id = ?;

 

//读取Blob数据   
  1. package demo;   
  2.   
  3. import java.sql.*;   
  4. import java.io.*;   
  5.   
  6. public class ReadBlob   
  7. {   
  8.     //加载驱动程序   
  9.     static    
  10.     {   
  11.            
    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)s  
    //读取Blob数据
    package demo;
    
    import java.sql.*;
    import java.io.*;
    
    public class ReadBlob
    {
    	//加载驱动程序
    	static 
    	{
    		
    		try
    		{
    			Class.forName("oracle.jdbc.driver.OracleDriver");
    		} catch (ClassNotFoundException e)
    		{
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    	public static void main(String[] args)
    	{
    		try
    		{
    			//1. 建立连接
    			String url = "jdbc:oracle:thin:@localhost:1521:OracleDB";
    			Connection conn = DriverManager.getConnection(url,"scott","tiger");
    			conn.setAutoCommit(false);
    			
    			//2. 查询数据
    			String sql = "select image from user_info where user_id = 1";
    			Statement stmt = conn.createStatement();
    			ResultSet rs = stmt.executeQuery(sql);
    			
    			//3. 读取Blob类型数据
    			Blob blob = null;
    			if(rs.next())
    			{
    				blob = rs.getBlob(1);
    			}
    			byte[] temp = new byte[(int)blob.length()];
    			InputStream in = blob.getBinaryStream();
    			in.read(temp)s
    1.             <strong>//保证文件名唯一,你可以用主键+时间啊等等方法</strong>                 
    2.             File file = new File("D://img.bmp");  
    3.             FileOutputStream fout = new FileOutputStream(file);  
    4.             fout.write(temp);  
    5.             in.close();  
    6.             fout.close();  
    7.         } catch (Exception e)  
    8.         {  
    9.             // TODO Auto-generated catch block  
    10.             e.printStackTrace();  
    11.         }  
    12.     }  
    13. }  
    			//保证文件名唯一,你可以用主键+时间啊等等方法				
    			File file = new File("D://img.bmp");
    			FileOutputStream fout = new FileOutputStream(file);
    			fout.write(temp);
    			in.close();
    			fout.close();
    		} catch (Exception e)
    		{
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
//写Blob数据   
  1. package demo;   
  2.   
  3. import java.sql.*;   
  4. import oracle.sql.BLOB;//▲此处的BLOB类全大写, 而java.sql.Blob中的Blob非全大写   
  5. import java.io.*;   
  6.   
  7. public class WriteBlob   
  8. {   
  9.     //加载驱动程序   
  10.     static    
  11.     {   
  12.         try  
  13.         {   
  14.             Class.forName("oracle.jdbc.driver.OracleDriver");   
  15.         }   
  16.         catch(Exception e)   
  17.         {   
  18.             e.printStackTrace();   
  19.         }   
  20.     }   
  21.     public static void main(String[] args)   
  22.     {   
  23.         try  
  24.         {   
  25.             //1. 建立与数据库服务器的连接   
  26.             String url = "jdbc:oracle:thin:@localhost:1521:OracleDB";   
  27.             Connection conn = DriverManager.getConnection(url,"scott","tiger");   
  28.             conn.setAutoCommit(false);   
  29.                
  30.             //2. 首先向表中插入空的Blob   
  31.             //★注意: 对于empty_blob()应放在SQL语句中直接赋值, 使用预置语句的方式赋值无法实现.   
  32.             String sql = "insert into user_info values(?,?,empty_blob())";   
  33.             PreparedStatement ps = conn.prepareStatement(sql);   
  34.             ps.setInt(1, 1);   
  35.             ps.setString(2, "Lucy");   
  36.             ps.executeUpdate();   
  37.                
  38.             //3. 查询Blob, 获得Blob的Cursor   
  39.             sql = "select image from user_info where user_id = ?";   
  40.             ps = conn.prepareStatement(sql);   
  41.             ps.setInt(1, 1);   
  42.             ResultSet rs = ps.executeQuery();   
  43.             BLOB blob = null;   
  44.             if(rs.next())   
  45.             {   
  46.                 blob = (BLOB)rs.getBlob(1);   
  47.             }   
  48.                
  49.             //4. 使用字节流将待入库的文件写入到blob中   
  50.             File file = new File("D://iriver//sample1.bmp");   
  51.             FileInputStream fin = new FileInputStream(file);   
  52.             byte[] temp = new byte[fin.available()];   
  53.             fin.read(temp);   
  54.             OutputStream out = blob.getBinaryOutputStream();   
  55.             out.write(temp);   
  56.             fin.close();   
  57.             out.close();   
  58.                
  59.             //5. 向数据库中写入数据   
  60.             sql = "update user_info set image = ? where user_id = ?";   
  61.             ps = conn.prepareStatement(sql);   
  62.             ps.setBlob(1, blob);   
  63.             ps.setInt(2, 1);   
  64.             ps.executeUpdate();   
  65.                
  66.             conn.commit();   
  67.         } catch (Exception e)   
  68.         {   
  69.             e.printStackTrace();   
  70.         }   
  71.     }   
  72. }  
原文地址:https://www.cnblogs.com/wzh123/p/3936866.html