you can proxy your Connection object:
public class ConnectionProxy {
public ConnectionProxy(Object anObject) {
super(anObject);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(target, args);
String methodName = method.getName();
if (methodName.equals("createStatement")) {
result = ProxyBuilder.createProxy(result, new StatementProxy(result));
}
return result;
}
}
in order to intercept any call to Statement.execute(String sql):
public class StatementProxy {
public StatementProxy(Object anObject) {
super(anObject);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
return method.invoke(proxy, args);
} catch (SQLException sqle) {
if (method.getName().contains("execute")) {
String sql = "";
if (args != null && args[0] != null) {
sql = args[0].toString();
}
saveToFile(arg);
}
throw sqle;
}
}
}
where ProxyBuilder is a simple helper class:
public final class ProxyBuilder {
public static Connection tracingConnection(Connection connection) {
return createProxy(connection, new ConnectionProxy(connection));
}
static <T> T createProxy(T anObject, InvocationHandler invocationHandler) {
return createProxy(anObject, invocationHandler, anObject.getClass().getInterfaces());
}
static <T> T createProxy(T anObject, InvocationHandler invocationHandler, Class... forcedInterfaces) {
return (T) Proxy.newProxyInstance(
anObject.getClass().getClassLoader(),
forcedInterfaces,
invocationHandler);
}
}
Of course, this is not your final production code but it is a good starting point.