0

ユーザーがアップロードしたcsvファイルがあり、これをClobとしてOracleテーブルに保存する必要があります。

したがって、私はこのコードを持っています:

 MultipartHttpServletRequest mr = (MultipartHttpServletRequest) ctx.getRequest();
    final MultipartFile f = mr.getFile("datafile");
    final InputStream is = f.getInputStream();
     ...
   jdbc.getJdbcOperations().execute(sql, new PreparedStatementCallback<Integer>() {
     public Integer doInPreparedStatement(final PreparedStatement psInsert) throws SQLException,
                            DataAccessException {
     ...
    psInsert.setCharacterStream(1, new InputStreamReader(is));
    psInsert.executeUpdate();
   }
});

また、PreparedStatementのメソッドsetClobとsetAsciiStreamを使用してみました。また、このアプローチ(ファイルのサイズを設定する)も試しましたが、結果は同じです-

java.lang.AbstractMethodError
    org.apache.commons.dbcp.DelegatingPreparedStatement.setAsciiStream(DelegatingPreparedStatement.java:338)
    org.apache.commons.dbcp.DelegatingPreparedStatement.setAsciiStream(DelegatingPreparedStatement.java:338)
    org.apache.commons.dbcp.DelegatingPreparedStatement.setAsciiStream(DelegatingPreparedStatement.java:338)

基になるInputStreamはByteArrayInputStreamです(それが違いを生む可能性がある場合)

PS:テーブルには実際にCLOBフィールドがあります:

P_FILE CLOB NOT NULL,

UPD:Oracleで実装されたメソッドを実際に試したことはありません。それは機能します。唯一の問題は、OracleドライバーがPreparedStatementインターフェースにあるメソッドと比較してすべてのメソッドを実装しているわけではないことです。使用可能なメソッドを調べるクラスは、OraclePreparedStatement...です。

4

2 に答える 2

1

AbstractMethodError javadocから:

アプリケーションが抽象メソッドを呼び出そうとするとスローされます。通常、このエラーはコンパイラによってキャッチされます。このエラーは、現在実行中のメソッドが最後にコンパイルされてから一部のクラスの定義が非互換に変更された場合にのみ、実行時に発生する可能性があります。

すべてのクラスが最新であることを確認してください。私はあなたのプロジェクト全体をきれいにして再構築します。また、コンパイル時と実行時のクラスパスが同等であることを確認してください(ライブラリのバージョンなどに関する限り)。

于 2012-09-07T16:14:15.177 に答える
0

Sormulaでは、 TypeTranslatorを使用して、任意の型を簡単に読み書きできます。プロジェクトの org.sormula.examples.blob パッケージを参照してください。コードは CLOB の場合と同様です。

public class WidgetTanslator1 implements TypeTranslator<Widget>
{
    public void write(PreparedStatement preparedStatement, int parameterIndex, Widget parameter) throws Exception
    {
        // convert from domain object to bytes
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        try (ObjectOutputStream oos = new ObjectOutputStream(bos))
        {
            oos.writeObject(parameter);

            // convert bytes to jdbc blob
            preparedStatement.setBlob(parameterIndex, new SerialBlob(bos.toByteArray()));
        }
    }


    public Widget read(ResultSet resultSet, int parameterIndex) throws Exception
    {
        // convert from jdbc blob to bytes to domain object
        Blob blob = resultSet.getBlob(parameterIndex);
        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(blob.getBytes(1, (int)blob.length()))))
        {
            return (Widget)ois.readObject();
        }
    }
}

次のように CLOB フィールドに注釈を付けます。

@ImplicitType(translator=WidgetTanslator1.class)
Widget widget;
于 2012-09-08T14:59:20.987 に答える