JDBC源代码片段 - 使用 mysql-connector-java-5.1.13.jar 中的拦截器

// 加载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;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值