簡體   English   中英

如何使用 jdbc 在 oracle 10g 中插入大 clob 數據(> 4K 個字符)

[英]How to insert Large clob data(>4K characters) in oracle 10g using jdbc

你們能否告訴我如何使用 jdbc 插入 clob 數據。 我正在使用 oracle10g 數據庫。

我能夠使用以下 2 種方法插入長度 < 4000 的 clob 數據

1.

tempClob.length()<4000){
   pstmnt.setClob(colNumber, tempClob );
}

2.

tempClob.length()<4000){
   Reader reader =tempClob.getCharacterStream();
   pstmnt.setClob(colNumber, tempClob );
   pstmnt.setCharacterStream(colNumber, reader, new Long(tempClob.length()).intValue());
}

當 clob 數據的長度很大時,例如 abt 29k,這兩種方法都會失敗。

這適用於 oracle11g jdk5

tempClob.length()<4000){
    byte[] bytes = tempClob.getBytes();
    pstmnt.setCharacterStream(2, new StringReader(new String( bytes  )), bytes.length);
}

If you are using Oracle 11g you will need drivers from here: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

ojdbc5.jar 用於 JDK 1.5 或 ojdbc6.jar 用於 JDK 1.6

jar 需要包含在您的類路徑中。

令人驚訝的是,Oracle 11g 可以存儲 8 到 128 Tera 字節的 Clob。

這是我發現的一些代碼,我認為它可以滿足您的需求:

我有一個像這樣的 static 方法:

public class DBUtil {
public static CLOB getCLOB(String innStr, Connection conn) throws SQLException, 
                                                   IOException {
  CLOB tempClob = null;
    // If the temporary CLOB has not yet been created, create new
    tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);

    // Open the temporary CLOB in readwrite mode to enable writing
    tempClob.open(CLOB.MODE_READWRITE);
    // Get the output stream to write
    Writer tempClobWriter = tempClob.getCharacterOutputStream();
    // Write the data into the temporary CLOB
    tempClobWriter.write(innStr);

    // Flush and close the stream
    tempClobWriter.flush();
    tempClobWriter.close();

    // Close the temporary CLOB
    tempClob.close();
  return tempClob;
}
}

然后你可以像這樣使用它:

CLOB tempClob = DBUtil.getCLOB(longString, connection);
pstmt.setCLOB(colnumber, tempClob);

這要求整個 clob 內容都在一個字符串中,所以它全部加載到 memory 中,也許不適合你,但它一直在滿足我的簡單需求。

編輯:我可能應該注意到我將此代碼與 plsql 過程調用一起使用。 沒有直接用 sql 測試過

您是否嘗試檢查該字段的最大大小... SELECT LENGTH(clobfield) FROM table

看看這個字段是否可以容納大小..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM