述】
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();
- }
- }
- }