// 加载MySql的驱动类
try {
Class.forName("com.mysql.jdbc.Driver"); // 注册驱动到DriverManager
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动程序类 ,加载驱动失败!");
e.printStackTrace();
}
// 连接MySql数据库
String url = "jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true";
// 配置拦截器
url += "&statementInterceptors=cn.java.database.jdbc.mysql.interceptors.FooOneStatementInterceptor";
String username = "root";
String password = "123456";
try {
connection = DriverManager.getConnection(url, username, password); // 连接数据库,并完成握手
if (connection instanceof com.mysql.jdbc.ConnectionImpl){
}
} catch (SQLException se) {
System.out.println("数据库连接失败!");
se.printStackTrace();
}
package cn.java.database.jdbc.mysql.interceptors;
import com.mysql.jdbc.*;
import java.sql.SQLException;
import java.util.Properties;
public class FooOneStatementInterceptor implements StatementInterceptor {
private Connection connection;
private Properties properties;
/**
* 初始化
*
* @param connection
* @param properties
* @throws SQLException
*/
@Override
public void init(Connection connection, Properties properties) throws SQLException {
// Connection
{
if (connection instanceof com.mysql.jdbc.ConnectionImpl) {
}
}
// Properties
{
System.out.println("properties.get(\"user\") = " + properties.get("user"));
System.out.println("properties.get(\"password\") = " + properties.get("password"));
}
this.connection = connection;
this.properties = properties;
}
/**
* 只在顶级执行
*
* @return
*/
@Override
public boolean executeTopLevelOnly() {
return false;
}
/**
* 执行请求前
* <p>
* 可以在这里篡改执行的sql语句
*
* @param sql
* @param interceptedStatement
* @param connection
* @return 如果返回的不是为null,那么不会发起远程的请求。而且不会执行"Interceptor链条"后面的Interceptor
* @throws SQLException
*/
@Override
public ResultSetInternalMethods preProcess(String sql, Statement interceptedStatement, Connection connection) throws SQLException {
System.out.println("--- cn.java.database.jdbc.mysql.interceptors.FooOneStatementInterceptor.preProcess ---");
if (interceptedStatement instanceof com.mysql.jdbc.PreparedStatement) {
String preparedSql = ((PreparedStatement) interceptedStatement).getPreparedSql();
System.out.println("preparedSql = " + preparedSql);
String asSql = ((PreparedStatement) interceptedStatement).asSql(true);
System.out.println("asSql = " + asSql);
} else if (interceptedStatement instanceof com.mysql.jdbc.StatementImpl) {
if (interceptedStatement instanceof com.mysql.jdbc.Statement) {
}
System.out.println("sql = " + sql);
}
if (connection instanceof com.mysql.jdbc.ConnectionImpl) {
if (connection instanceof com.mysql.jdbc.MySQLConnection) {
}
}
return null;
}
/**
* 执行请求后
* <p>
* 可以在这里篡改返回的结果集合
*
* @param sql
* @param interceptedStatement
* @param resultSetInternalMethods
* @param connection
* @return 返回的值会作为"后面的Interceptor"的入参
* @throws SQLException
*/
@Override
public ResultSetInternalMethods postProcess(String sql, Statement interceptedStatement, ResultSetInternalMethods resultSetInternalMethods, Connection connection) throws SQLException {
System.out.println("--- cn.java.database.jdbc.mysql.interceptors.FooOneStatementInterceptor.postProcess ---");
if (interceptedStatement instanceof com.mysql.jdbc.PreparedStatement) {
String preparedSql = ((PreparedStatement) interceptedStatement).getPreparedSql();
System.out.println("preparedSql = " + preparedSql);
String asSql = ((PreparedStatement) interceptedStatement).asSql(true);
System.out.println("asSql = " + asSql);
} else if (interceptedStatement instanceof com.mysql.jdbc.StatementImpl) {
if (interceptedStatement instanceof com.mysql.jdbc.Statement) {
}
System.out.println("sql = " + sql);
}
if (connection instanceof com.mysql.jdbc.ConnectionImpl) {
if (connection instanceof com.mysql.jdbc.MySQLConnection) {
}
}
return resultSetInternalMethods;
}
/**
* 销毁
*/
@Override
public void destroy() {
this.connection = null;
this.properties = null;
}
}