0

一部の jdbc コードを MySql から SQL Server に変換しています。しようとするとき

        query = "Update ReportSetup "
                    + "set N_ID=?, "
                    + "R_Default=?, "
                    + "R_Name=?, "
                    + "R_Module=? "
                    + " where R_ID = ?";
        }

        PreparedStatement stmt =
                (PreparedStatement) con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        stmt.setInt(1, ri.getNodeID());
        stmt.setInt(2, (ri.isDefault()) ? 1 : 0);
        stmt.setString(3, ri.getName());
        Object o = ri.getReportModule();
        stmt.setObject(4, o);

最後のステートメントstmt.setObject(4,o)は例外をスローします。

ri.getReportModule returns an instance of a class which implements Externalizable.

そのクラスのメソッド writeExternal() は次のように実装されます。

public final void writeExternal(final ObjectOutput objectOutput) throws IOException {
    for (int i=0; i<pdV.size(); i++) {
        PropertyDescriptor pd = pdV.elementAt(i);
        try {
            Method m = pd.getReadMethod();
            Object val = pd.getReadMethod().invoke(this);
            System.out.print("writing property " + i + ": " + pd.getName() + " = " + val);
            objectOutput.writeObject(val);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

問題のデータベース列は、null ではなく varbinary(max) として定義されています

コードは MySql を使用してうまく動作しますが、Sql Server で実行するにはどうすればよいかわかりません。

どんな提案でも大歓迎です

4

1 に答える 1

2

問題は、SQL Server がシリアライゼーションを保存できないことでした (externalizable を実装するときに行われたように)。.setObject() は失敗します。解決策は、setBinaryStream() を使用することです。

        // Sql Server can't do an stmt.setObject(4,o) "Conversion from UNKNOWN to UNKNOWN not supported"
        // Serialize the object to an output stream and then read it in again into the stmt.
        Object o = ri.getReportModule();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream objectOutput = new ObjectOutputStream(bos);
        objectOutput.writeObject(o);
        objectOutput.flush();
        InputStream objectInput = new ByteArrayInputStream(bos.toByteArray());
        stmt.setBinaryStream(4, objectInput);

乾杯クリスチャン

于 2013-09-02T22:00:12.587 に答える