gpt4 book ai didi

java - 如何获取preparedstatement中的batch数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:19:11 26 4
gpt4 key购买 nike

我想这一定很简单。为此必须有一些方法。这就是我想要的:-

PreparedStatement ps = ...
ps.addBatch ( );
ps.addBatch ( );
ps.addBatch ( );
logger.info ( "totalBatches: " + ps.someMethod() );
ps.executeBatch ( );

结果将是:总批处理:3;
如果没有这样的方法,那怎么办呢?

最佳答案

不支持此功能。但是您可以通过添加计数成员来包装 Statement 并覆盖 addBatch() 。如果使用 Apache Commons DBCP ,您可以从 DelegatingPreparedStatement 继承,否则您缺少 PreparedStatement 的包装器。所以我使用代理添加方法:

public class BatchCountingStatementHandler implements InvocationHandler {

public static BatchCountingStatement countBatches(PreparedStatement delegate) {
return Proxy.newProxyInstance(delegate.getClassLoader(), new Class[] {BatchCountingStatement.class}, new BatchCountingStatementHandler(delegate));
}

private final PreparedStatement delegate;
private int count = 0;

private BatchCountingStatementHandler(PreparedStatement delegate) {
this.delegate = delegate;
}

public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if ("getBatchCount".equals(method.getName())) {
return count;
}
try {
return method.invoke(delegate, args);
} finally {
if ("addBatch".equals(method.getName())) {
++count;
}
}
}

public static interface BatchCountingStatement extends PreparedStatement {
public int getBatchCount();
}
}

关于java - 如何获取preparedstatement中的batch数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3749501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com