欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-24 20:07:40
代码:复制代码 代码如下:package com.lwj.test.proxy; import java.lang.reflect.invocationhandler;i...

代码:

复制代码 代码如下:

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语句。