資料一
//
grant create any directory to scott;
grant create any library to scott;
create or replace directory utllobdir as
create table bfile_tab (bfile_column BFILE);
create table utl_lob_test (blob_column BLOB);
set serveroutput on
然後執行下面語句就將d: oracle目錄下的Azul
表中的blob_column字段中了
declare
a_blob BLOB;
a_bfile BFILE := BFILENAME(
begin
insert into bfile_tab values (a_bfile)
returning bfile_column into a_bfile;
insert into utl_lob_test values (empty_blob())
returning blob_column into a_blob;
dbms_lob
dbms_lob
dbms_lob
commit;
end;
/
資料二
ORACLE中LOB字段的使用和維護
摘要
關鍵詞
中圖分類號
隨著社會的發展
為了便於讀者的理解
在ORACLE數據庫中
為了方便下文的敘述
CREATE TABLE view_sites_info
(
site_id NUMBER(
audio BLOB DEFAULT empty_blob()
document CLOB DEFAULT empty_clob()
video_file BFILE DEFAULT NULL
constraint PK_TAB_view_sites_info primary key (site_id)
);
ORACL提供了多種使用和維護LOB的方式
在Oracle中
DELCARE
AUDIO_INFO BLOB
BENGIN
SELECT audio INTO AUDIO_INFO FROM view_sites_info
WHERE site_id=
END;
/
存儲在AUDIO_INFO變量中的就是LOB定位器
DBMS_LOB包中主要提供了以下幾個過程供用戶對內部LOB字段進行維護
APPEND() 將源LOB中的內容加到目的LOB中
COPY() 從源LOB中復制數據到目的LOB
ERASE() 刪除LOB中全部或部分內容
TRIM() 將LOB值減少到指定的長度
WRITE() 向LOB 中寫入數據
COMPARE() 比較兩個同種數據類型的LOB的部分或全部值是否相同
GETLENGTH() 獲取LOB的長度
READ() 從LOB中讀出數據
下面我們以最為常用的讀和寫為例詳細介紹這些過程的用法
首先介紹一下寫過程
PROCEDURE WRITE (
lob_loc IN OUT BLOB
amount IN BINARY_INTEGER
offset IN INTEGER
buffer IN RAW);
PROCEDURE WRITE (
lob_loc IN OUT CLOB CHARACTER SET ANY_CS
amount IN BINARY_INTEGER
offset IN INTEGER
buffer IN VARCHAR
各參數的含義為
lob_loc
amount
offset
buffer
下面的代碼就是運用該過程向LOB字段寫入數據的示例
DECLARE
lobloc CLOB;
buffer VARCHAR
amount NUMBER :=
offset NUMBER :=
BEGIN
buffer :=
amount := length(buffer);
SELECT document INTO lobloc
FROM view_sites_info
WHERE site_id =
dbms_lob
COMMIT;
END;
/
需要特別指出的是
I
II
III
下面再來介紹一下讀過程
該過程的語法為
PROCEDURE READ (
lob_loc IN BLOB
amount IN OUT BINARY_INTEGER
offset IN INTEGER
buffer OUT RAW);
PROCEDURE READ (
lob_loc IN CLOB CHARACTER SET ANY_CS
amount IN OUT BINARY_INTEGER
offset IN INTEGER
buffer OUT VARCHAR
各參數的含義為
lob_loc
amount
offset
buffer
下面的代碼演示了如何使用該過程讀取LOB字段中的數據
DECLARE
lobloc CLOB;
buffer VARCHAR
amount NUMBER :=
offset NUMBER :=
BEGIN
SELECT document INTO lobloc
FROM lob_store
WHERE lob_id =
dbms_lob
dbms_output
COMMIT;
END;
/
資料三
Java代碼
import java
import java
import java
// Importing the Oracle Jdbc driver package makes the code more readable
import oracle
//needed for new CLOB and BLOB classes
import oracle
public class LobExample
{
public static void main (String args [])
throws Exception
{
// Register the Oracle JDBC driver
DriverManager
// Connect to the database
// You can put a database name after the @ sign in the connection URL
Connection conn =
DriverManager
// It
conn
// Create a Statement
Statement stmt = conn
try
{
stmt
}
catch (SQLException e)
{
// An exception could be raised here if the table did not exist already
}
// Create a table containing a BLOB and a CLOB
stmt
// Populate the table
stmt
stmt
System
// Select the lobs
ResultSet rset = stmt
while (rset
{
// Get the lobs
BLOB blob = ((OracleResultSet)rset)
CLOB clob = ((OracleResultSet)rset)
// Print the lob contents
dumpBlob (conn
dumpClob (conn
// Change the lob contents
fillClob (conn
fillBlob (conn
}
System
rset = stmt
while (rset
{
// Get the lobs
BLOB blob = ((OracleResultSet)rset)
CLOB clob = ((OracleResultSet)rset)
// Print the lobs contents
dumpBlob (conn
dumpClob (conn
}
// Close all resources
rset
stmt
conn
}
// Utility function to dump Clob contents
static void dumpClob (Connection conn
throws Exception
{
// get character stream to retrieve clob data
Reader instream = clob
// create temporary buffer for read
char[] buffer = new char[
// length of characters read
int length =
// fetch data
while ((length = instream
{
System
for (int i=
System
System
}
// Close input stream
instream
}
// Utility function to dump Blob contents
static void dumpBlob (Connection conn
throws Exception
{
// Get binary output stream to retrieve blob data
InputStream instream = blob
// Create temporary buffer for read
byte[] buffer = new byte[
// length of bytes read
int length =
// Fetch data
while ((length = instream
{
System
for (int i=
System
System
}
// Close input stream
instream
}
// Utility function to put data in a Clob
static void fillClob (Connection conn
throws Exception
{
Writer outstream = clob
int i =
int chunk =
while (i < length)
{
outstream
i += chunk;
if (length
chunk = (int) length
}
outstream
}
// Utility function to put data in a Blob
static void fillBlob (Connection conn
throws Exception
{
OutputStream outstream = blob
int i =
int chunk =
byte [] data = {
while (i < length)
{
data [
outstream
i += chunk;
if (length
chunk = (int) length
}
outstream
}
}
給你這個例子看看
資料四
比如
FileStream fs=File
long len=fs
byte[] tempBuff= new byte[len];
fs
fs
OracleConnection conn=new OracleConnection(
conn
OracleCommand cmd=conn
cmd
cmd
cmd
cmd
cmd
cmd
try
{
cmd
}
catch
{
//
}
From:http://tw.wingwit.com/Article/program/Oracle/201311/18734.html