Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you can proxy your Connection object:</p> <pre><code>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; } } </code></pre> <p>in order to intercept any call to <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#execute(java.lang.String)" rel="nofollow noreferrer">Statement.execute(String sql)</a>:</p> <pre><code>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 &amp;&amp; args[0] != null) { sql = args[0].toString(); } saveToFile(arg); } throw sqle; } } } </code></pre> <p>where ProxyBuilder is a simple helper class:</p> <pre><code>public final class ProxyBuilder { public static Connection tracingConnection(Connection connection) { return createProxy(connection, new ConnectionProxy(connection)); } static &lt;T&gt; T createProxy(T anObject, InvocationHandler invocationHandler) { return createProxy(anObject, invocationHandler, anObject.getClass().getInterfaces()); } static &lt;T&gt; T createProxy(T anObject, InvocationHandler invocationHandler, Class... forcedInterfaces) { return (T) Proxy.newProxyInstance( anObject.getClass().getClassLoader(), forcedInterfaces, invocationHandler); } } </code></pre> <p>Of course, <strong>this is not your final production code</strong> but it is a good starting point.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload