0734w-月下竹音

          0734是來(lái)自家鄉(xiāng)的聲音

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          Blob字段

           從CSDN上看到的感覺(jué)挺有用的

                最近幾次碰到這個(gè)問(wèn)題,需求是將一個(gè)文件或者文件流存儲(chǔ)到Oracle數(shù)據(jù)庫(kù)里,
          Oracle8提供了Blob和Clob用來(lái)存儲(chǔ)二進(jìn)制大對(duì)象數(shù)據(jù),可是它和Java.sql.里面的Blob
          不兼容,經(jīng)常導(dǎo)致Blob字段無(wú)法鎖定或者操作失敗,總之我總結(jié)了一些經(jīng)驗(yàn)
          大家共享
          首先建立測(cè)試數(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;


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


          /**
          * @author 秋南(Ryan)
          * @email  guoyf@sinosoft.com.cn
          * @version 2002 1 14
          */
          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;//**將測(cè)試文件test.doc讀入此字節(jié)數(shù)組
            java.io.FileInputStream fin=null;
            java.io.FileOutputStream fout=null;
            oracle.jdbc.OracleResultSet ors=null;//**這里rs一定要用Oracle提供的
            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讀出的信息寫(xiě)

                                                                           //入該文 件,和源文件對(duì)比測(cè)試用
             fin=new java.io.FileInputStream(f1);
             fout=new java.io.FileOutputStream(f2);
             
             
             int flength=(int)f1.length();//**讀入文件的字節(jié)長(zhǎng)度
             System.out.println("file length::"+flength);
             a=new byte[flength];
             
             int i=0;int itotal=0;
             /**將文件讀入字節(jié)數(shù)組
             for (;itotal<flength;itotal=i+itotal )
             {
              
              i=fin.read(a,itotal,flength-itotal);
              
             }
             fin.close();
             
             System.out.println("read itotal::"+itotal);
            /**注意Oracle的 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ù)組寫(xiě)入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é)寫(xiě)入文件
                     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
            { /**關(guān)閉所有數(shù)據(jù)聯(lián)接
             stmt.close();
             db1.closeConn();
            }
            

            
           }
          }
              編譯運(yùn)行在TomCat下調(diào)試通過(guò)。
              需要注意的是Blob存取的過(guò)程,一般先存入和BLOB相關(guān)的控制數(shù)據(jù),如文件的名字,
              然后查詢定位BLOB字段,利用OracleBlob提供的方法:
              public int putBytes(long pos,byte bytes[])
              public byte[]  getBytes(long pos,byte bytes[])
              或者利用
              public OutputStream getBinaryOutputStream() throws SQLException
              public InputStream  getBinaryStream() throws SQLException
             因?yàn)槔幂斎胼敵隽骺倸w還是利用到字節(jié)數(shù)組緩沖流,所以就不舉例子了。

          posted on 2005-09-01 21:57 sparkwu 閱讀(520) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 五指山市| 中牟县| 德州市| 甘谷县| 新龙县| 通州区| 宜黄县| 阳曲县| 屯门区| 南丰县| 天长市| 大田县| 镇平县| 景东| 仪征市| 嘉鱼县| 崇信县| 武定县| 镇巴县| 苍山县| 平阳县| 恭城| 剑阁县| 吉安县| 景谷| 应城市| 凌源市| 黎平县| 九江县| 黔西县| 梨树县| 漯河市| 米泉市| 灵石县| 嘉鱼县| 巫溪县| 建阳市| 新龙县| 崇州市| 光山县| 山阳县|