Oracle數(shù)據(jù)庫中BLOB字段的存取問題
以下的文章主要是對Oracle數(shù)據(jù)庫中BLOB字段的存取問題的介紹,我在經常會碰到Oracle數(shù)據(jù)庫中BLOB字段的存取這一問題,需求是將一個文件或者文件流存儲到Oracle數(shù)據(jù)庫里,Oracle8提供了Blob和Clob用來存儲二進制大對象數(shù)據(jù)。
可是它和Java.sql.里面的Blob不兼容,經常導致Blob字段無法鎖定或者操作失敗,總之我總結了一些經驗大家共享。
首先建立測試數(shù)據(jù)表
- drop table filelist;
- commit;
- CREATE TABLE SYSTEM.FILELIST (
- "FILENAME" VARCHAR2(50) NOT NULL,
- "FILESIZE" NUMBER(20) NULL,
- "FILEBODY" BLOB NULL,
- PRIMARY KEY("FILENAME"), UNIQUE("FILENAME")) ;
- commit;
測試過程,首先將硬盤文件讀入Oracle數(shù)據(jù)庫,然后再讀出到硬盤的另一個新文件里,原碼如下:
- import java.io.*;
- import java.util.*;
- import java.sql.*;
- import oracle.sql.*;
- import oracle.jdbc.driver.*;
- import java.text.*;
- public class test
- {
- public static void main(String args[]) throws java.io.IOException,java.sql.SQLException
- {
- dbBean db1=new dbBean();
- /**
*這里是我的數(shù)據(jù)聯(lián)接Bean
*大家可以用自己的連接Bean
- */
- byte a[]=null;
**將測試文件test.doc讀入此字節(jié)數(shù)組
- java.io.FileInputStream fin=null;
- java.io.FileOutputStream fout=null;
- oracle.jdbc.OracleResultSet ors=null;
**這里rs一定要用Oracle數(shù)據(jù)庫提供的
- oracle.jdbc.driver.OraclePreparedStatement opst=null;
**PreparedStatement用
Oracle提供的
- try
- {
- java.io.File f1=new java.io.File("c:/temp/test.doc");
- java.io.File f2=new java.io.File("c:/temp/testout.doc");
**從BLOB讀出的信息寫
//入該文件,和源文件對比測試用
- fin=new java.io.FileInputStream(f1);
- fout=new java.io.FileOutputStream(f2);
int flength=(int)f1.length();//**讀入文件的字節(jié)長度
- System.out.println("file length::"+flength);
- a=new byte[flength];
- int i=0;int itotal=0;
/**將文件讀入字節(jié)數(shù)組
- for (;itotal<flength;iitotal=i+itotal )
- {
- i=fin.read(a,itotal,flength-itotal);
- }
- fin.close();
- System.out.println("read itotal::"+itotal);
/**注意Oracle數(shù)據(jù)庫的 BLOB一定要用EMPTY_BLOB()初始化
- String mysql="insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";
- opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);
- opst.setString(1,"wordtemplate");
- opst.setInt (2,flength);
- opst.executeUpdate();
- opst.clearParameters();
/**插入其它數(shù)據(jù)后,定位BLOB字段
- mysql="select filebody from filelist where filename=?";
- opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);
- opst.setString(1,"wordtemplate");
- ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();
- if (ors.next())
- {
oracle.sql.BLOB blob=ors.getBLOB(1);/**得到BLOB字段
int j=blob.putBytes(1,a);/**將字節(jié)數(shù)組寫入BLOB字段
- System.out.println("j:"+j);
- db1.conn.commit();
- ors.close();
- }
- System.out.println("insert into ok");
byte b[]=null;/**保存從BLOB讀出的字節(jié)
- opst.clearParameters();
- mysql="select filebody from filelist where filename=?";
- opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);
- opst.setString(1,"wordtemplate");
- ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();
- if (ors.next())
- {
- oracle.sql.BLOB blob2=ors.getBLOB(1);
- System.out.println("blob2 length:"+blob2.length());
b=blob2.getBytes(1,flength);/**從BLOB取出字節(jié)流數(shù)據(jù)
- System.out.println("b length::"+b.length);
- db1.conn.commit();
- }
- ors.close();
/**將從BLOB讀出的字節(jié)寫入文件
- fout.write(b,0,b.length);
- fout.close();
- System.out.println("write itotal::"+b.length);
- }
- catch(Exception e)
- {
- System.out.println("errror :"+e.toString() );
- e.printStackTrace();
- }
- finally
{ /**關閉所有數(shù)據(jù)聯(lián)接
- stmt.close();
- db1.closeConn();
- }
- }
- }
編譯運行在TomCat下調試通過。
需要注意的是Blob存取的過程,一般先存入和BLOB相關的控制數(shù)據(jù),如文件的名字,然后查詢定位BLOB字段,利用Oracle數(shù)據(jù)庫Blob提供的方法:
- public int putBytes(long pos,byte bytes[])
- public byte[] getBytes(long pos,byte bytes[])
或者利用
- public OutputStream getBinaryOutputStream() throws SQLException
- public InputStream getBinaryStream() throws SQLException
因為利用輸入輸出流總歸還是利用到字節(jié)數(shù)組緩沖流,所以就不舉例子了。
【編輯推薦】
- Oracle 函數(shù)用法之decode解剖
- LTO歸檔解決方案容量、性能和功能介紹
- Oracle 函數(shù)用法之decode解剖
- Oracle字符集討論的經典版
- Oracle表空間的設置問題的描述