述】
Oracle的Blob字段比較特殊,他比long字段的性能要好不少,能夠用來保存例如圖片之類的二進制數據。
寫入Blob字段和寫入其它類型字段的方式很是不一樣,由於Blob自身有一個cursor,你必須使用cursor對
blob進行操做,於是你在寫入Blob以前,必須得到cursor才能進行寫入,那麼如何得到Blob的cursor呢?
這須要你先插入一個empty的blob,這將建立一個blob的cursor,而後你再把這個empty的blob的cursor
用select查詢出來,這樣經過兩步操做,你就得到了blob的cursor,能夠真正地寫入blob數據了。
【處理流程】java
-
- create table user_info
- (
- user_id number(10) primary key,
- name varchar2(20),
- image blob
- );
-
-
- insert into user_info values (1, 'Jacky', empty_blob());
-
-
- select image from user_info where user_id = ? for update;
-
-
- 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 = ?;
- package demo;
-
- import java.sql.*;
- import java.io.*;
-
- public class ReadBlob
- {
-
- static
- {
-
- package demo;
-
- import java.sql.*;
- import java.io.*;
-
- public class ReadBlob
- {
-
- static
- {
-
- try
- {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e)
- {
-
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args)
- {
- try
- {
-
- String url = "jdbc:oracle:thin:@localhost:1521:OracleDB";
- Connection conn = DriverManager.getConnection(url,"scott","tiger");
- conn.setAutoCommit(false);
-
-
- String sql = "select image from user_info where user_id = 1";
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
-
-
- 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>
- File file = new File("D://img.bmp");
- FileOutputStream fout = new FileOutputStream(file);
- fout.write(temp);
- in.close();
- fout.close();
- } catch (Exception e)
- {
-
- 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;
- 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
- {
-
- String url = "jdbc:oracle:thin:@localhost:1521:OracleDB";
- Connection conn = DriverManager.getConnection(url,"scott","tiger");
- conn.setAutoCommit(false);
-
-
-
- String sql = "insert into user_info values(?,?,empty_blob())";
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setInt(1, 1);
- ps.setString(2, "Lucy");
- ps.executeUpdate();
-
-
- 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);
- }
-
-
- 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();
-
-
- 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();
- }
- }
- }