Cette fonctionnalité n'est pas prise en charge. Mais vous pouvez envelopper la déclaration et remplacer addBatch() en ajoutant un membre de comptage. Si vous utilisez Apache Commons DBCP, vous pouvez hériter de DelegatingPreparedStatement
, sinon vous ne disposez pas d'un wrapper pour PreparedStatement
. J'ai donc utilisé un proxy pour ajouter la méthode:
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();
}
}
Je pense que Rakesh aimerait obtenir le nombre de lots avant l'exécution. Sinon, l'utilisation de executeBatch est OK. – rics