java跟踪执行的sql语句示例分享

所属分类: 软件编程 / java 阅读数: 40
收藏 0 赞 0 分享

代码:

复制代码 代码如下:

package com.lwj.test.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;

public class DBManager {
    private final static ThreadLocal<Connection> conns = new ThreadLocal<Connection>();
    private static boolean show_sql = true;  

    public final static Connection getConnection() throws SQLException {  
        Connection conn = (Connection) conns.get();  
        if(conn ==null || conn.isClosed()){  
            // 这里使用我定义的一个简单的 ConnectionProvider 替代 dataSource 获取Connection  
            conn = ConnectionProvider.getConnection();  
            conns.set(conn);  
        }  
        return (show_sql && !Proxy.isProxyClass(conn.getClass()))?  
                      new _DebugConnection(conn).getConnection():conn;  
    }  

    /** 
     * 关闭连接 
     */ 
    public final static void closeConnection() {  
        Connection conn = (Connection) conns.get();  
        try {  
            if(conn != null && !conn.isClosed()){  
                conn.setAutoCommit(true);  
                conn.close();  
            }  
        } catch (SQLException e) {  
        }  
        conns.set(null);  
    }  

    /** 
     * 用于跟踪执行的SQL语句 
     */ 
    static class _DebugConnection implements InvocationHandler {  
        private Connection conn = null;

        public _DebugConnection(Connection conn) {  
            this.conn = conn;
        }
        public Connection getConnection() {  
            return (Connection) Proxy.newProxyInstance(conn.getClass().getClassLoader(),new Class[]{Connection.class}, this);
        }
        public Object invoke(Object proxy, Method m, Object[] args) throws Throwable   
        {  
            try   
            {  
                String method = m.getName();  
                if("prepareStatement".equals(method) || "createStatement".equals(method))  
                {
                    System.out.println(method);
                    System.out.println(args[0]);
                }  
                return m.invoke(conn, args);
            } catch (InvocationTargetException e) {  
                throw e.getTargetException();  
            }  
        }  
    }
}

package com.lwj.test.proxy;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionProvider {
 public static Connection getConnection()  
    {
  Connection connection = null;
       try{  

           Class.forName("oracle.jdbc.OracleDriver").newInstance();  
           connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.101:1521:orcl", "scott", "tiger");
       }catch(Exception e){  
       }
       return connection;
    }
}

package com.lwj.test.proxy;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestMain {

 public static void main( String[] args )  
    {  
            Connection conn = null;  
            Statement stmt = null;
            PreparedStatement pstmt = null;
            try 
            {  
                conn = DBManager.getConnection();

                stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);  
                stmt.executeUpdate( "insert into test1(id,name,card,age,address) values(9,'liuwj','1234567890988777',24,'hubeitianmen')" );  

                /*pstmt = conn.prepareStatement( "insert into test1(id,name,card,age,address) values(?,?,?,?,?)");
                pstmt.setString(1, "10");
                pstmt.setString(2, "liuwj2");
                pstmt.setString(3, "1234567890988777");
                pstmt.setString(4, "22");
                pstmt.setString(5, "123456");
                pstmt.execute();*/
            }catch(SQLException e){  

            }finally{  
                 try{  
                   if( pstmt != null ){  
                    pstmt.close();  
                    pstmt = null;      
                   }  
                 }catch(SQLException e){  

                 }   

                 DBManager.closeConnection();  
            }    
    }
}

论坛上看到用下列语句:

复制代码 代码如下:

pstmt = conn.prepareStatement( "insert into test1(id,name,card,age,address) values(?,?,?,?,?)");
pstmt.setString(1, "10");
pstmt.setString(2, "liuwj2");
pstmt.setString(3, "1234567890988777");
pstmt.setString(4, "22");
pstmt.setString(5, "123456");
pstmt.execute();

才能打印出sql语句。

更多精彩内容其他人还在看

java 中maven pom.xml文件教程详解

这篇文章主要介绍了java 中maven pom.xml文件教程详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合netty的实现方法

这篇文章主要介绍了spring boot整合netty的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Netty与Spring Boot的整合实现

这篇文章主要介绍了Netty与Spring Boot的整合的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring动态加载bean后调用实现方法解析

这篇文章主要介绍了Spring动态加载bean后调用实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现画图板上画一条直线

这篇文章主要为大家详细介绍了java实现画图板上画一条直线,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java通过python命令执行DataX任务的实例

今天小编就为大家分享一篇Java通过python命令执行DataX任务的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

springBoot集成redis的key,value序列化的相关问题

这篇文章主要介绍了springBoot集成redis的key,value序列化的相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java实现登录案例

这篇文章主要为大家详细介绍了java实现登录案例的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java解决请求跨域的两种方法

这篇文章主要为大家详细介绍了java解决请求跨域的两种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringBoot集成Beetl后统一处理页面异常的方法

这篇文章主要介绍了SpringBoot集成Beetl后统一处理页面异常的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多