import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
/** java.sql.Connection proxy source*/
public abstract class ProxyConnectionBase implements Connection {
protected Connection conn;
public ProxyConnectionBase(Connection conn){
this.conn = conn;
}
/** {@inheritDoc} */
@Override
@SuppressWarnings("unchecked")
public final <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isInstance(conn)) {
return (T) conn;
} else {
throw new SQLException("`" + iface + "`I can't seem to cast to.");
}
}
/** {@inheritDoc} */
@Override
public final boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface.isInstance(conn);
}
/** {@inheritDoc} */
@Override
public Statement createStatement() throws SQLException {
return conn.createStatement();
}
/** {@inheritDoc} */
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return conn.prepareStatement(sql);
}
/** {@inheritDoc} */
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
return conn.prepareCall(sql);
}
/** {@inheritDoc} */
@Override
public String nativeSQL(String sql) throws SQLException {
return conn.nativeSQL(sql);
}
/** {@inheritDoc} */
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
conn.setAutoCommit(autoCommit);
}
/** {@inheritDoc} */
@Override
public boolean getAutoCommit() throws SQLException {
return conn.getAutoCommit();
}
/** {@inheritDoc} */
@Override
public void commit() throws SQLException {
conn.commit();
}
/** {@inheritDoc} */
@Override
public void rollback() throws SQLException {
conn.rollback();
}
/** {@inheritDoc} */
@Override
public void close() throws SQLException {
conn.close();
}
/** {@inheritDoc} */
@Override
public boolean isClosed() throws SQLException {
return conn.isClosed();
}
/** {@inheritDoc} */
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return conn.getMetaData();
}
/** {@inheritDoc} */
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
conn.setReadOnly(readOnly);
}
/** {@inheritDoc} */
@Override
public boolean isReadOnly() throws SQLException {
return conn.isReadOnly();
}
/** {@inheritDoc} */
@Override
public void setCatalog(String catalog) throws SQLException {
conn.setCatalog(catalog);
}
/** {@inheritDoc} */
@Override
public String getCatalog() throws SQLException {
return conn.getCatalog();
}
/** {@inheritDoc} */
@Override
public void setTransactionIsolation(int level) throws SQLException {
conn.setTransactionIsolation(level);
}
/** {@inheritDoc} */
@Override
public int getTransactionIsolation() throws SQLException {
return conn.getTransactionIsolation();
}
/** {@inheritDoc} */
@Override
public SQLWarning getWarnings() throws SQLException {
return conn.getWarnings();
}
/** {@inheritDoc} */
@Override
public void clearWarnings() throws SQLException {
conn.clearWarnings();
}
/** {@inheritDoc} */
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return conn.createStatement(resultSetType, resultSetConcurrency);
}
/** {@inheritDoc} */
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
return conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
/** {@inheritDoc} */
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return conn.prepareCall(sql, resultSetType, resultSetConcurrency);
}
/** {@inheritDoc} */
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
return conn.getTypeMap();
}
/** {@inheritDoc} */
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
conn.setTypeMap(map);
}
/** {@inheritDoc} */
@Override
public void setHoldability(int holdability) throws SQLException {
conn.setHoldability(holdability);
}
/** {@inheritDoc} */
@Override
public int getHoldability() throws SQLException {
return conn.getHoldability();
}
/** {@inheritDoc} */
@Override
public Savepoint setSavepoint() throws SQLException {
return conn.setSavepoint();
}
/** {@inheritDoc} */
@Override
public Savepoint setSavepoint(String name) throws SQLException {
return conn.setSavepoint(name);
}
/** {@inheritDoc} */
@Override
public void rollback(Savepoint savepoint) throws SQLException {
conn.rollback(savepoint);
}
/** {@inheritDoc} */
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
conn.releaseSavepoint(savepoint);
}
/** {@inheritDoc} */
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
}
/** {@inheritDoc} */
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
/** {@inheritDoc} */
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
/** {@inheritDoc} */
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
return conn.prepareStatement(sql, autoGeneratedKeys);
}
/** {@inheritDoc} */
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
return conn.prepareStatement(sql, columnIndexes);
}
/** {@inheritDoc} */
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
return conn.prepareStatement(sql, columnNames);
}
/** {@inheritDoc} */
@Override
public Clob createClob() throws SQLException {
return conn.createClob();
}
/** {@inheritDoc} */
@Override
public Blob createBlob() throws SQLException {
return conn.createBlob();
}
/** {@inheritDoc} */
@Override
public NClob createNClob() throws SQLException {
return conn.createNClob();
}
/** {@inheritDoc} */
@Override
public SQLXML createSQLXML() throws SQLException {
return conn.createSQLXML();
}
/** {@inheritDoc} */
@Override
public boolean isValid(int timeout) throws SQLException {
return conn.isValid(timeout);
}
/** {@inheritDoc} */
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
conn.setClientInfo(name, value);
}
/** {@inheritDoc} */
@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
conn.setClientInfo(properties);
}
/** {@inheritDoc} */
@Override
public String getClientInfo(String name) throws SQLException {
return conn.getClientInfo(name);
}
/** {@inheritDoc} */
@Override
public Properties getClientInfo() throws SQLException {
return conn.getClientInfo();
}
/** {@inheritDoc} */
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
return conn.createArrayOf(typeName, elements);
}
/** {@inheritDoc} */
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
return conn.createStruct(typeName, attributes);
}
/** {@inheritDoc} */
@Override
public void setSchema(String schema) throws SQLException {
conn.setSchema(schema);
}
/** {@inheritDoc} */
@Override
public String getSchema() throws SQLException {
return conn.getSchema();
}
/** {@inheritDoc} */
@Override
public void abort(Executor executor) throws SQLException {
conn.abort(executor);
}
/** {@inheritDoc} */
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
conn.setNetworkTimeout(executor, milliseconds);
}
/** {@inheritDoc} */
@Override
public int getNetworkTimeout() throws SQLException {
return conn.getNetworkTimeout();
}
}
You might think, but it's convenient. this. Everyone, please try to predict the usage.
That's right. This is the "Proxy Element" series that makes it easy to create a proxy.
Then, sql received from java.sql.Connection # prepareStatement
Let's create a proxy that outputs to the console.
/**Proxy that outputs SQL to the console*/
public class ProxyConnection extends ProxyConnectionBase{
public ProxyConnection(Connection arg0) {
super(arg0);
}
/** {@inheritDoc} */
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
System.out.println(sql);
return conn.prepareStatement(sql);
}
}
It's easy!
After that, java.sql.Connection connection = new ProxyConnection (connection);
You can use it just by wrapping it with.
By the way, using java.lang.reflect.Proxy
as in the following article,
You can also programmatically create a proxy.
Proxy for logging SQL and SQL results -Qiita