2

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 データの長さが約 29k のように大きい場合、これらの方法は両方とも失敗します。

4

4 に答える 4

1

これはoracle11g jdk5で動作します

tempClob.length()<4000){
    byte[] bytes = tempClob.getBytes();
    pstmnt.setCharacterStream(2, new StringReader(new String( bytes  )), bytes.length);
}
于 2013-05-21T10:32:53.373 に答える
0

Oracle 11g を使用している場合は、ここからドライバーが必要になります: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

JDK 1.5 の場合は ojdbc5.jar、JDK 1.6 の場合は ojdbc6.jar

jar をクラスパスに含める必要があります。

驚くべきことに、Oracle 11g は 8 から 128 テラバイトの Clob を格納できます。

于 2011-07-13T19:32:11.540 に答える
0

これは、私が見つけたいくつかのコードで、あなたが望むことをすると思います:

次のような静的メソッドがあります。

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 コンテンツ全体を文字列にする必要があるため、すべてがメモリに読み込まれます。おそらくあなたには適していませんが、私の単純なニーズには合っています。

編集: plsql プロシージャ コールでこのコードを使用したことに注意してください。SQLで直接テストしていません

于 2011-07-19T09:09:53.693 に答える
-2

フィールドの最大サイズを確認しようとしましたか... SELECT LENGTH(clobfield) FROM table

フィールドがサイズに対応できるかどうかを確認してください..

于 2011-07-13T19:32:22.603 に答える