av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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

以下的文章主要是對Oracle數(shù)據(jù)庫中BLOB字段的存取問題的介紹,我在經(jīng)常會碰到Oracle數(shù)據(jù)庫中BLOB字段的存取這一問題,需求是將一個文件或者文件流存儲到Oracle數(shù)據(jù)庫里,Oracle8提供了Blob和Clob用來存儲二進(jìn)制大對象數(shù)據(jù)。

勃利網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,勃利網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為勃利上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的勃利做網(wǎng)站的公司定做!

可是它和Java.sql.里面的Blob不兼容,經(jīng)常導(dǎo)致Blob字段無法鎖定或者操作失敗,總之我總結(jié)了一些經(jīng)驗大家共享。

首先建立測試數(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
  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  

 

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

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

 

編譯運行在TomCat下調(diào)試通過。

需要注意的是Blob存取的過程,一般先存入和BLOB相關(guān)的控制數(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ù)組緩沖流,所以就不舉例子了。


分享題目:Oracle數(shù)據(jù)庫中BLOB字段的存取問題
文章地址:http://uogjgqi.cn/article/dhghcii.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流