打印本文 打印本文 关闭窗口 关闭窗口
Oracle中Blob字段的写入处理(一)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2217  更新时间:2009/4/22 22:10:35  文章录入:mintao  责任编辑:mintao
          sqlBuffer.append(keyValue);

                 //注意这里的”for update”

          sqlBuffer.append("'''' for update ");

 

          stmt = conn.createStatement();

          rs = (OracleResultSet) stmt.executeQuery(sqlBuffer.toString());

 

          if (!rs.next())

          {

              rs.close();

              stmt.close();

              throw new IllegalArgumentException(

                  "no record found for keyValue: ''''" + keyValue + "''''");

          }

          blob = rs.getBLOB(1);

          OutputStream outstream = blob.getBinaryOutputStream();

          int bufferSize = blob.getChunkSize();

          byte[] buffer = new byte[bufferSize];

          int bytesRead = -1;

          while ((bytesRead = instream.read(buffer)) != -1)

          {

              outstream.write(buffer, 0, bytesRead);

          }

          instream.close();

          outstream.close();

          rs.close();

          stmt.close();

      }

      catch (SQLException e)

      {

          throw e;

      }

      catch (IOException e)

      {

          throw e;

      }

 

      finally {

          conn.setAutoCommit(oldAutoCommit);

      }

  }

 

二、PL/SQL下

(例子表结构如一所示)

i.插入一空的Blob。

       …

       declare

              bufferBlob BLOB;

              data        RAW(…)

       INSERT  INTO Student VALUES(‘zhangsan’, 20 , empty_blob() );

       …

 

ii.更新该纪录的Blob。

       …

       SELECT Picture INTO bufferBlob FROM Student WHERE Name=’zhangsan’ FOR UPDATE;

       DBMS_LOB.OPEN(bufferBlob , dbms_lob.lob_readwrite);

       dbms_lob.write(bufferBlob , utl_raw.length(data) , data);

       …

(待续)

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口