自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Oracle數(shù)據(jù)庫中BLOB字段的存取問題

數(shù)據(jù)庫 Oracle
我們今天主要介紹的是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ù)表

 

  1. drop table filelist;  
  2. commit;  
  3. CREATE TABLE SYSTEM.FILELIST (  
  4. "FILENAME" VARCHAR2(50) NOT NULL,  
  5. "FILESIZE" NUMBER(20) NULL,  
  6. "FILEBODY" BLOB NULL,  
  7. PRIMARY KEY("FILENAME"), UNIQUE("FILENAME")) ;  
  8. commit;  

 

測試過程,首先將硬盤文件讀入Oracle數(shù)據(jù)庫,然后再讀出到硬盤的另一個新文件里,原碼如下:

 

  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.sql.*;  
  4. import oracle.sql.*;  
  5. import oracle.jdbc.driver.*;  
  6. import java.text.*;  
  7. public class test  
  8. {  
  9. public static void main(String args[]) throws java.io.IOException,java.sql.SQLException  
  10. {  
  11. dbBean db1=new dbBean();  
  12. /**  

 

*這里是我的數(shù)據(jù)聯(lián)接Bean

*大家可以用自己的連接Bean

 

  1. */  
  2. byte a[]=null;  

 

**將測試文件test.doc讀入此字節(jié)數(shù)組

 

  1. java.io.FileInputStream fin=null;  
  2. java.io.FileOutputStream fout=null;  
  3. oracle.jdbc.OracleResultSet ors=null;  

 

**這里rs一定要用Oracle數(shù)據(jù)庫提供的

 

  1. oracle.jdbc.driver.OraclePreparedStatement opst=null

**PreparedStatement用

Oracle提供的

 

  1. try  
  2. {  
  3. java.io.File f1=new java.io.File("c:/temp/test.doc");  
  4. java.io.File f2=new java.io.File("c:/temp/testout.doc");  

**從BLOB讀出的信息寫

//入該文件,和源文件對比測試用

 

  1. fin=new java.io.FileInputStream(f1);  
  2. fout=new java.io.FileOutputStream(f2);  

 

int flength=(int)f1.length();//**讀入文件的字節(jié)長度

 

  1. System.out.println("file length::"+flength);  
  2. a=new byte[flength];  
  3. int i=0;int itotal=0;  

 

/**將文件讀入字節(jié)數(shù)組

 

  1. for (;itotal<flength;iitotal=i+itotal )  
  2. {  
  3. i=fin.read(a,itotal,flength-itotal);  
  4. }  
  5. fin.close();  
  6. System.out.println("read itotal::"+itotal);  

 

/**注意Oracle數(shù)據(jù)庫的 BLOB一定要用EMPTY_BLOB()初始化

 

  1. String mysql="insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";  
  2. opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);  
  3. opst.setString(1,"wordtemplate");  
  4. opst.setInt (2,flength);  
  5. opst.executeUpdate();  
  6. opst.clearParameters();  

 

/**插入其它數(shù)據(jù)后,定位BLOB字段

 

  1. mysql="select filebody from filelist where filename=?";  
  2. opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);  
  3. opst.setString(1,"wordtemplate");  
  4. ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();  
  5. if (ors.next())  
  6. {  

 

oracle.sql.BLOB blob=ors.getBLOB(1);/**得到BLOB字段

int j=blob.putBytes(1,a);/**將字節(jié)數(shù)組寫入BLOB字段

 

  1. System.out.println("j:"+j);  
  2. db1.conn.commit();  
  3. ors.close();  
  4. }  
  5. System.out.println("insert into ok");  

 

byte b[]=null;/**保存從BLOB讀出的字節(jié)

 

  1. opst.clearParameters();  
  2. mysql="select filebody from filelist where filename=?";  
  3. opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);  
  4. opst.setString(1,"wordtemplate");  
  5. ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();  
  6. if (ors.next())  
  7. {  
  8. oracle.sql.BLOB blob2=ors.getBLOB(1);  
  9. System.out.println("blob2 length:"+blob2.length());  

 

b=blob2.getBytes(1,flength);/**從BLOB取出字節(jié)流數(shù)據(jù)

 

  1. System.out.println("b length::"+b.length);  
  2. db1.conn.commit();  
  3. }  
  4. ors.close();  

 

/**將從BLOB讀出的字節(jié)寫入文件

 

  1. fout.write(b,0,b.length);  
  2. fout.close();  
  3. System.out.println("write itotal::"+b.length);  
  4. }  
  5. catch(Exception e)  
  6. {  
  7. System.out.println("errror :"+e.toString() );  
  8. e.printStackTrace();  
  9. }  
  10. finally  

 

{ /**關閉所有數(shù)據(jù)聯(lián)接

 

  1. stmt.close();  
  2. db1.closeConn();  
  3. }  
  4. }  
  5. }  

 

編譯運行在TomCat下調試通過。

需要注意的是Blob存取的過程,一般先存入和BLOB相關的控制數(shù)據(jù),如文件的名字,然后查詢定位BLOB字段,利用Oracle數(shù)據(jù)庫Blob提供的方法:

 

  1. public int putBytes(long pos,byte bytes[])  
  2. public byte[] getBytes(long pos,byte bytes[])  

 

或者利用

 

  1. public OutputStream getBinaryOutputStream() throws SQLException  
  2. public InputStream getBinaryStream() throws SQLException  

 

因為利用輸入輸出流總歸還是利用到字節(jié)數(shù)組緩沖流,所以就不舉例子了。

【編輯推薦】

  1. Oracle 函數(shù)用法之decode解剖
  2. LTO歸檔解決方案容量、性能和功能介紹
  3. Oracle 函數(shù)用法之decode解剖
  4. Oracle字符集討論的經典版
  5. Oracle表空間的設置問題的描述

 

責任編輯:佚名 來源: 互聯(lián)網(wǎng)
相關推薦

2010-04-23 14:32:01

Oracle數(shù)據(jù)庫

2010-04-20 10:12:42

OraclePL

2011-04-11 13:09:56

數(shù)據(jù)庫

2011-07-19 14:48:36

處理blob字段

2011-05-26 14:18:49

Oracle數(shù)據(jù)庫字段屬性

2011-03-18 11:24:07

Oracle 數(shù)據(jù)庫時間問題

2011-08-18 18:34:00

Oracle數(shù)據(jù)庫創(chuàng)建自增字段

2010-04-14 15:58:17

Oracle程序開發(fā)

2010-04-23 16:18:36

Oracle存取

2010-05-07 17:56:10

Oracle數(shù)據(jù)庫安全

2023-11-16 17:12:33

數(shù)據(jù)庫oracle

2009-07-02 00:00:00

OOPOracle

2009-09-04 09:54:59

數(shù)據(jù)庫名

2010-04-15 10:51:52

2019-08-28 07:11:00

Oracle數(shù)據(jù)庫LOB

2010-10-26 08:54:52

BLOBOracle

2011-05-26 13:36:40

Oracle數(shù)據(jù)庫時間處理

2011-05-24 14:13:20

Oracle數(shù)據(jù)庫

2023-11-13 15:03:49

MySQL數(shù)據(jù)庫

2009-07-31 17:01:21

C#存取Access數(shù)
點贊
收藏

51CTO技術棧公眾號