讀寫Oracle 10g的CLOB欄位

今天有個將超長字串寫入Oracle資料庫的需求,原先以為只要把欄位型態定義CLOB (Character Large OBject)再用setString填入即可,沒想到當字串長度超過8K時就會產生SQLException,哇,難道真的要用stream的方法來處理嗎?有點麻煩哩。還好,終於找到解決方法,但似乎只能用在Oracle10g上:

  1. Oracle 10g針對CLOB 形態做了加強,能直接用getStringsetString來讀寫CLOB欄位。
  • 字串長度最大32,765 Bytes
  • 必須使用Oracle 10gJDBC driver (ojdbc14.jar)
  • Varchar2最大只能設定到4000,因此有較長字串需處理時,可使用CLOB再限制長度為32765 (此長度應該對大部份應用都能適用)
  1. 存取超過32,765字串時的特殊處理:
    1. 使用擴充的setStringForClob method:
      opstmt = (OraclePreparedStatement)conn.prepareStatement(sql);
      // Use the new method to insert the CLOB data (for data greater or lesser than 32 KB)
      opstmt.setStringForClob(1,str);
      // Execute the OraclePreparedStatement
      opstmt.executeUpdate();
      
    2. 啟始Driver時設定參數
      // Load the database details into the variables.
      String url = "jdbc:oracle:thin:@localhost:1521:orcl";
      String user = "scott";
      String password = "tiger";
      // Create the properties object that holds all database details
      Properties props = new Properties();
      props.put("user", user );
      props.put("password", password);
      props.put("SetBigStringTryClob", "true");
      // Load the Oracle JDBC driver class.
      DriverManager.registerDriver(new OracleDriver()); 
      
      // Get the database connection
      Connection conn = DriverManager.getConnection( this.url, this.props );
      

##

您可能也會有興趣的類似文章

填寫回應

 

 

 

您可使用這些HTML標籤

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

隨機文章

  1. [Tools] 資料夾變身工具3:FolderIcons (2)
  2. 變化你的噗浪暱稱:Unicode我♥你-啦啦啦♪☺〠 (2)
  3. 更新TS-109韌體與安裝MLDonkey 
  4. [Xuite] 熱門日誌擴增到500個 
  5. HighLight:程式碼顯示的美化工具 
  6. [Xuite] 在Xuite中使用上傳的圖片 
  7. [AutoHotkey] 在檔案總管裡按滑鼠滾輪切換到上層資料夾 (3)
  8. PDFCreator: 產生PDF檔的印表驅動程式 (2)
  9. [Tools] Miranda-IM 外掛便利貼與鬧鈴 
  10. 如何轉換AutoHotkey的預設分隔字元:冒號 (2)

歷史熱門文章

  1. 如何手動輸入Plurk的表情圖示? (368,462 點擊/2008-09-29)
  2. 啟用&關閉中華電信ZyXEL P874的無線網路連線功能的步驟[修訂] (120,513 點擊/2011-06-01)
  3. Plurk CSS樣式自訂修改全攻略 (89,485 點擊/2009-07-04)
  4. 簡單放大Plurk輸入區的方法[修訂] (88,898 點擊/2008-09-29)
  5. 網路升級光世代50M/3M與設定ZyXEL P874 (72,211 點擊/2011-05-28)
  6. 輕鬆學會彈指神功-揭露AutoHotkey絕技 (50,936 點擊/2008-04-10)
  7. iPad新手入門技巧 (49,452 點擊/2011-02-05)
  8. iPad鍵盤輸入全攻略 (42,931 點擊/2011-01-01)
  9. 分享世博台灣館主題曲『台灣的心跳聲』MV(附歌詞+下載連結+新歌詞) (40,947 點擊/2010-04-27)
  10. 調整Windows 7環境: 加回工作列的「顯示桌面」圖示 (40,870 點擊/2010-01-02)