感謝㊣贊助鼓勵!

Google搜尋 »

分類

簡睿在噗浪»

有朋自遠方來

讀寫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="">

隨機文章

  1. 尋獲推推王貼文自動選用標籤狀況的元兇:標籤雲 
  2. [轉貼] 師大附中…稱霸全國! 
  3. [Plurk] 如何顯示12個噗浪朋友圖示?(結論:別人的是12個,自己的只會顯示8個) 
  4. Resin 3.0.18與其PHP引擎 
  5. [轉貼數位時代] Google與百度,正式決戰中國! 
  6. 備份驅動程式的軟體 
  7. 見微知著:觀《版本控制之道-使用Subversion》有感 (1)
  8. 移除ThinkPad的螢幕放大功能 
  9. 挺不賴的Windows Live Writer的圖片插件:Polaroid Picture (1)
  10. CallingID: 幫網路衝浪加上多一層的保護 

歷史熱門文章

  1. Plurk CSS樣式自訂修改全攻略 (30,962 點擊/2009-07-04)
  2. 把HTC Touch HD操作介面換成Spb Mobile Shell,美觀又便利! (20,275 點擊/2009-02-18)
  3. 強化閱讀第2噗:變更噗浪回應的字體與背景顏色 (17,547 點擊/2009-06-07)
  4. 另一套適用手機的影片轉檔工具:WinAVI 3GP/MP4/PSP/iPod Video Converter (16,754 點擊/2009-04-18)
  5. 強化HTC Touch HD的工具軟體 (14,973 點擊/2009-02-25)
  6. 如何手動輸入Plurk的表情圖示? (13,960 點擊/2008-09-29)
  7. 增加Spb Mobile Shell 3.0生活化桌面頁數的小技巧 (13,743 點擊/2009-04-27)
  8. 簡單放大Plurk輸入區的方法[修訂] (13,049 點擊/2008-09-29)
  9. 將Plurk回應區變成固定寬度 (12,874 點擊/2009-07-11)
  10. 將噗浪的視窗高度變大,加上分隔線,以方便閱讀 (12,515 點擊/2009-06-06)