我像这样创建一个新的 PreparedStatement:
PreparedStatement newPs = origPrepStatement.getConnection().prepareStatement("EXPLAIN " + sql);
origPrepStatement 也是 Preparedstatement 并且它包含参数。我想将 origPrepStatement 的参数复制到 newPs 中。有没有办法做到这一点?
我像这样创建一个新的 PreparedStatement:
PreparedStatement newPs = origPrepStatement.getConnection().prepareStatement("EXPLAIN " + sql);
origPrepStatement 也是 Preparedstatement 并且它包含参数。我想将 origPrepStatement 的参数复制到 newPs 中。有没有办法做到这一点?
似乎没有简单的解决方案;在下面找到我笨拙的解决方案
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);
}
}