熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

JDBC存取ORACLE大型數據對象LOB幾種情況的示范類

2022-06-13   來源: Oracle 

  import javaio*;
  import javautil*;
  import javasql*;
  
  public class LobPros
  {
  
    /**
    * ORACLE驅動程序
    */
    private static final String DRIVER = oraclejdbcdriverOracleDriver;
  
    /**
    * ORACLE連接用URL
    */
    private static final String URL = jdbc:oracle:thin:@test::orac;
  
    /**
    * 用戶名
    */
    private static final String USER = user;
  
    /**
    * 密碼
    */
    private static final String PASSWORD = pswd;
  
    /**
    * 數據庫連接
    */
    private static Connection conn = null;
  
    /**
    * SQL語句對象
    */
    private static Statement stmt = null;
  
    /**
    * @roseuid EDAEBC
    */
    public LobPros()
    {
  
    }
  
    /**
    * 往數據庫中插入一個新的CLOB對象
    *
    * @param infile 數據文件
    * @throws javalangException
    * @roseuid EDAABC
    */
    public static void clobInsert(String infile) throws Exception
    {
      /* 設定不自動提交 */
      boolean defaultCommit = conngetAutoCommit();
      connsetAutoCommit(false);
  
      try {
        /* 插入一個空的CLOB對象 */
        stmtexecuteUpdate(INSERT INTO TEST_CLOB VALUES ( EMPTY_CLOB()));
        /* 查詢此CLOB對象並鎖定 */
        ResultSet rs = stmtexecuteQuery(SELECT CLOBCOL FROM TEST_CLOB WHERE ID= FOR UPDATE);
        while (rsnext()) {
          /* 取出此CLOB對象 */
          oraclesqlCLOB clob = (oraclesqlCLOB)rsgetClob(CLOBCOL);
          /* 向CLOB對象中寫入數據 */
          BufferedWriter out = new BufferedWriter(clobgetCharacterOutputStream());
          BufferedReader in = new BufferedReader(new FileReader(infile));
          int c;
          while ((c=inread())!=) {
            outwrite(c);
          }
          inclose();
          outclose();
        }
        /* 正式提交 */
        mit();
      } catch (Exception ex) {
        /* 出錯回滾 */
        connrollback();
        throw ex;
      }
  
      /* 恢復原提交狀態 */
      connsetAutoCommit(defaultCommit);
    }
  
    /**
    * 修改CLOB對象(是在原CLOB對象基礎上進行覆蓋式的修改)
    *
    * @param infile 數據文件
    * @throws javalangException
    * @roseuid EDAB
    */
    public static void clobModify(String infile) throws Exception
    {
      /* 設定不自動提交 */
      boolean defaultCommit = conngetAutoCommit();
      connsetAutoCommit(false);
  
      try {
        /* 查詢CLOB對象並鎖定 */
        ResultSet rs = stmtexecuteQuery(SELECT CLOBCOL FROM TEST_CLOB WHERE ID= FOR UPDATE);
        while (rsnext()) {
          /* 獲取此CLOB對象 */
          oraclesqlCLOB clob = (oraclesqlCLOB)rsgetClob(CLOBCOL);
          /* 進行覆蓋式修改 */
          BufferedWriter out = new BufferedWriter(clobgetCharacterOutputStream());
          BufferedReader in = new BufferedReader(new FileReader(infile));
          int c;
          while ((c=inread())!=) {
            outwrite(c);
          }
          inclose();
          outclose();
        }
        /* 正式提交 */
        mit();
      } catch (Exception ex) {
        /* 出錯回滾 */
        connrollback();
        throw ex;
      }
  
      /* 恢復原提交狀態 */
      connsetAutoCommit(defaultCommit);
    }
  
    /**
    * 替換CLOB對象(將原CLOB對象清除換成一個全新的CLOB對象)
    *
    * @param infile 數據文件
    * @throws javalangException
    * @roseuid EDABFE
    */
    public static void clobReplace(String infile) throws Exception
    {
      /* 設定不自動提交 */
      boolean defaultCommit = conngetAutoCommit();
      connsetAutoCommit(false);
  
      try {
        /* 清空原CLOB對象 */
        stmtexecuteUpdate(UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID=);
        /* 查詢CLOB對象並鎖定 */
        ResultSet rs = stmtexecuteQuery(SELECT CLOBCOL FROM TEST_CLOB WHERE ID= FOR UPDATE);
        while (rsnext()) {
          /* 獲取此CLOB對象 */
          oraclesqlCLOB clob = (oraclesqlCLOB)rsgetClob(CLOBCOL);
          /* 更新數據 */
          BufferedWriter out = new BufferedWriter(clobgetCharacterOutputStream());
          BufferedReader in = new BufferedReader(new FileReader(infile));
          int c;
          while ((c=inread())!=) {
            outwrite(c);
          }
          inclose();
          outclose();
        }
        /* 正式提交 */
        mit();
      } catch (Exception ex) {
        /* 出錯回滾 */
        connrollback();
        throw ex;
      }
  
      /* 恢復原提交狀態 */
      connsetAutoCommit(defaultCommit);
    }
  
    /**
    * CLOB對象讀取
    *
    * @param outfile 輸出文件名
    * @throws javalangException
    * @roseuid EDAD
    */
    public static void clobRead(String outfile) throws Exception
    {
      /* 設定不自動提交 */
      boolean defaultCommit = conngetAutoCommit();
      connsetAutoCommit(false);
  
      try {
        /* 查詢CLOB對象 */
        ResultSet rs = stmtexecuteQuery(SELECT * FROM TEST_CLOB WHERE ID=);
        while (rsnext()) {
          /* 獲取CLOB對象 */
          oraclesqlCLOB clob = (oraclesqlCLOB)rsgetClob(CLOBCOL);
          /* 以字符形式輸出 */
          BufferedReader in = new BufferedReader(clobgetCharacterStream());
          BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
          int c;
          while ((c=inread())!=) {
            outwrite(c);
          }
          outclose();
          inclose();
        }
      } catch (Exception ex) {
        connrollback();
        throw ex;
      }
  
      /* 恢復原提交狀態 */
      connsetAutoCommit(defaultCommit);
    }
  
    /**
    * 向數據庫中插入一個新的BLOB對象
    *
    * @param infile 數據文件
    * @throws javalangException
    * @roseuid EDAEF
    */
    public static void blobInsert(String infile) throws Exception
    {
      /* 設定不自動提交 */
      boolean defaultCommit = conngetAutoCommit();
      connsetAutoCommit(fal
From:http://tw.wingwit.com/Article/program/Oracle/201311/18837.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.