当前所在位置:珠峰网资料 >> 计算机 >> Oracle认证 >> 正文
向Oracle数据库blob图片字段写入图片流(三)
发布时间:2010/10/24 15:09:32 来源:www.xue.net 编辑:城市总裁吧

    下面的代码演示了如何使用该过程读取LOB字段中的数据:

    DECLARE

    lobloc   CLOB;

    buffer   VARCHAR2(2000);

    amount   NUMBER   :=   2;

    offset   NUMBER   :=   6;

    BEGIN

    SELECT   document   INTO   lobloc     --获取定位器

    FROM   lob_store

    WHERE   lob_id   =   100;

    dbms_lob.read(lobloc,amount,offset,buffer);--读取数据到缓冲区

    dbms_output.put_line(buffer);--显示缓冲区中的数据

    COMMIT;

    END;

    /

    资料三:java实现

    Java代码

    import   java.sql.*;

    import   java.io.*;

    import   java.util.*;

    //   Importing   the   Oracle   Jdbc   driver   package   makes   the   code   more   readable

    import   oracle.jdbc.driver.*;

    //needed   for   new   CLOB   and   BLOB   classes

    import   oracle.sql.*;

    public   class   LobExample

    {

    public   static   void   main   (String   args   [])

    throws   Exception

    {

    //   Register   the   Oracle   JDBC   driver

    DriverManager.registerDriver(new   oracle.jdbc.driver.OracleDriver());

    //   Connect   to   the   database

    //   You   can   put   a   database   name   after   the   @   sign   in   the   connection   URL.

    Connection   conn   =

    DriverManager.getConnection   ("jdbc:oracle:oci8:@",   "scott",   "tiger");

    //   It's   faster   when   auto   commit   is   off

    conn.setAutoCommit   (false);

    //   Create   a   Statement

    Statement   stmt   =   conn.createStatement   ();

    try

    {

    stmt.execute   ("drop   table   basic_lob_table");

    }

    catch   (SQLException   e)

    {

    //   An   exception   could   be   raised   here   if   the   table   did   not   exist   already.

    }

    //   Create   a   table   containing   a   BLOB   and   a   CLOB

    stmt.execute   ("create   table   basic_lob_table   (x   varchar2   (30),   b   blob,   c   clob)");

    //   Populate   the   table

    stmt.execute   ("insert   into   basic_lob_table   values   ('one',   '010101010101010101010101010101',   'onetwothreefour')");

    stmt.execute   ("insert   into   basic_lob_table   values   ('two',   '0202020202020202020202020202',   'twothreefourfivesix')");

    System.out.println   ("Dumping   lobs");

    //   Select   the   lobs

    ResultSet   rset   =   stmt.executeQuery   ("select   *   from   basic_lob_table");

    while   (rset.next   ())

    {

    //   Get   the   lobs

    BLOB   blob   =   ((OracleResultSet)rset).getBLOB   (2);

    CLOB   clob   =   ((OracleResultSet)rset).getCLOB   (3);

    //   Print   the   lob   contents

    dumpBlob   (conn,   blob);

    dumpClob   (conn,   clob);

    //   Change   the   lob   contents

    fillClob   (conn,   clob,   2000);

    fillBlob   (conn,   blob,   4000);

    }

    System.out.println   ("Dumping   lobs   again");

    rset   =   stmt.executeQuery   ("select   *   from   basic_lob_table");

    while   (rset.next   ())

    {

    //   Get   the   lobs

    BLOB   blob   =   ((OracleResultSet)rset).getBLOB   (2);

    CLOB   clob   =   ((OracleResultSet)rset).getCLOB   (3);

    //   Print   the   lobs   contents

    dumpBlob   (conn,   blob);

    dumpClob   (conn,   clob);

    }

    //   Close   all   resources

    rset.close();

    stmt.close();

    conn.close();

    }

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved