述】
Oracle的Blob字段比較特殊,他比long字段的性能要好很多,可以用來保存例如圖片之類的二進制數據。
寫入Blob字段和寫入其它類型字段的方式非常不同,因為Blob自身有一個cursor,你必須使用cursor對
blob進行操作,因而你在寫入Blob之前,必須獲得cursor才能進行寫入,那么如何獲得Blob的cursor呢?
這需要你先插入一個empty的blob,這將創建一個blob的cursor,然后你再把這個empty的blob的cursor
用select查詢出來,這樣通過兩步操作,你就獲得了blob的cursor,可以真正地寫入blob數據了。
【處理流程】
- package demo;
- import java.sql.*;
- import java.io.*;
- public class ReadBlob
- {
- //加載驅動程序
- static
- {
-
- //讀取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
//讀取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- <strong>//保證文件名唯一,你可以用主鍵+時間啊等等方法</strong>
- 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();
- }
- }
- }
//保證文件名唯一,你可以用主鍵+時間啊等等方法 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(); } } }
- package demo;
- import java.sql.*;
- import oracle.sql.BLOB;//▲此處的BLOB類全大寫, 而java.sql.Blob中的Blob非全大寫
- import java.io.*;
- public class WriteBlob
- {
- //加載驅動程序
- static
- {
- try
- {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- }
- catch(Exception e)
- {
- 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. 首先向表中插入空的Blob
- //★注意: 對於empty_blob()應放在SQL語句中直接賦值, 使用預置語句的方式賦值無法實現.
- String sql = "insert into user_info values(?,?,empty_blob())";
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setInt(1, 1);
- ps.setString(2, "Lucy");
- ps.executeUpdate();
- //3. 查詢Blob, 獲得Blob的Cursor
- sql = "select image from user_info where user_id = ?";
- ps = conn.prepareStatement(sql);
- ps.setInt(1, 1);
- ResultSet rs = ps.executeQuery();
- BLOB blob = null;
- if(rs.next())
- {
- blob = (BLOB)rs.getBlob(1);
- }
- //4. 使用字節流將待入庫的文件寫入到blob中
- File file = new File("D://iriver//sample1.bmp");
- FileInputStream fin = new FileInputStream(file);
- byte[] temp = new byte[fin.available()];
- fin.read(temp);
- OutputStream out = blob.getBinaryOutputStream();
- out.write(temp);
- fin.close();
- out.close();
- //5. 向數據庫中寫入數據
- sql = "update user_info set image = ? where user_id = ?";
- ps = conn.prepareStatement(sql);
- ps.setBlob(1, blob);
- ps.setInt(2, 1);
- ps.executeUpdate();
- conn.commit();
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
