3

次のような新しい PreparedStatement を作成します。

PreparedStatement newPs = origPrepStatement.getConnection().prepareStatement("EXPLAIN " + sql);

origPrepStatement も Preparedstatement であり、パラメーターが含まれています。origPrepStatement のパラメータを newPs にコピーしたいと思います。それを行うことはありますか?

4

1 に答える 1

1

簡単な解決策はないようです。以下の私の不器用な解決策を見つけてください

class PreparedStatementParameters implements InvocationHandler {
    Map<Integer, Object> map = new HashMap<>();
    PreparedStatement ps;

    PreparedStatementParameters(PreparedStatement ps) {
        this.ps = ps;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getName().startsWith("set")) {

        }
        return method.invoke(proxy, args);
    }

    public void copyParameters(PreparedStatement ps) throws SQLException {
        for (Map.Entry<Integer, Object> e : map.entrySet()) {
            ps.setObject(e.getKey(), e.getValue());
        }
    }
}

public class T2 {
    public static void main(String[] args) throws Exception {
        PreparedStatement ps1 = ...
    PreparedStatementParameters ps1params = new PreparedStatementParameters(ps1);
        PreparedStatement ps1Proxy = (PreparedStatement) Proxy.newProxyInstance(null,
                new Class[] { PreparedStatement.class }, new PreparedStatementParameters(ps1));
        ps1Proxy.setString(1, "test");
        ...
        PreparedStatement ps2 = ...
        ps1params.copyParameters(ps2);
    }
}
于 2013-03-21T12:48:52.773 に答える