欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java跟蹤執(zhí)行的sql語句示例分享

 更新時間:2014年03月24日 10:34:57   作者:  
這篇文章主要介紹了java跟蹤執(zhí)行的sql語句示例分享,需要的朋友可以參考下

代碼:

復(fù)制代碼 代碼如下:

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;  
    }  

    /** 
     * 關(guān)閉連接 
     */ 
    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);  
    }  

    /** 
     * 用于跟蹤執(zhí)行的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();  
            }    
    }
}

論壇上看到用下列語句:

復(fù)制代碼 代碼如下:

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

相關(guān)文章

  • Java File類常用方法與文件過濾器詳解

    Java File類常用方法與文件過濾器詳解

    Java File類以抽象的方式代表文件名和目錄路徑名。該類主要用于文件和目錄的創(chuàng)建、文件的查找和文件的刪除等。File對象代表磁盤中實際存在的文件和目錄。本篇文章我們來講解File類的常用方法與文件過濾器
    2022-04-04
  • java文字轉(zhuǎn)語音的實現(xiàn)示例

    java文字轉(zhuǎn)語音的實現(xiàn)示例

    在Java中,我們可以使用第三方庫來實現(xiàn)文字轉(zhuǎn)語音的功能,本文主要介紹了java文字轉(zhuǎn)語音的實現(xiàn)示例,選擇jacob技術(shù)實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • 詳細(xì)全面解析Java泛型

    詳細(xì)全面解析Java泛型

    這篇文章主要介紹了詳細(xì)全面解析Java泛型,java泛型主要提高了Java 程序的類型安全,通過知道使用泛型定義的變量的類型限制,編譯器可以驗證類型假設(shè),消除源代碼中的許多強制類型轉(zhuǎn)換等多個有點,下面我們進(jìn)入文章了解更多的詳細(xì)內(nèi)容吧
    2022-02-02
  • EVCache緩存在Spring Boot中的實戰(zhàn)示例

    EVCache緩存在Spring Boot中的實戰(zhàn)示例

    這篇文章主要介紹了EVCache緩存在Spring Boot中的實戰(zhàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 消息隊列-kafka消費異常問題

    消息隊列-kafka消費異常問題

    這篇文章主要給大家介紹了關(guān)于kafka的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • java遍歷properties文件操作指南

    java遍歷properties文件操作指南

    在java項目開發(fā)過程中,使用properties文件作為配置基本上是必不可少的,有很多如系統(tǒng)配置信息,java如何遍歷properties文件呢,本文將詳細(xì)介紹,希望可以幫助到您
    2012-11-11
  • 基于SpringMVC攔截器實現(xiàn)接口耗時監(jiān)控功能

    基于SpringMVC攔截器實現(xiàn)接口耗時監(jiān)控功能

    本文呢主要介紹了基于SpringMVC攔截器實現(xiàn)的接口耗時監(jiān)控功能,統(tǒng)計接口的耗時情況屬于一個可以復(fù)用的功能點,因此這里直接使用 SpringMVC的HandlerInterceptor攔截器來實現(xiàn),需要的朋友可以參考下
    2024-02-02
  • Java中的函數(shù)式編程

    Java中的函數(shù)式編程

    這篇文章介紹Java中的函數(shù)式編程,函數(shù)式編程是一種編程范式,其中程序是通過應(yīng)用和組合函數(shù)來構(gòu)造的。它是一種聲明式編程范式,其中函數(shù)定義是表達(dá)式樹,每個表達(dá)式樹返回一個值,而不是一系列改變程序狀態(tài)的命令語句,具體情況請看下文,希望對你有所幫助
    2021-10-10
  • Spring?cloud負(fù)載均衡@LoadBalanced?&?LoadBalancerClient

    Spring?cloud負(fù)載均衡@LoadBalanced?&?LoadBalancerClient

    由于Spring?cloud2020之后移除了Ribbon,直接使用Spring?Cloud?LoadBalancer作為客戶端負(fù)載均衡組件,我們討論Spring負(fù)載均衡以Spring?Cloud2020之后版本為主,學(xué)習(xí)Spring?Cloud?LoadBalance
    2023-11-11
  • Java was started but returned exit code=13問題解決案例詳解

    Java was started but returned exit code=13問題解決案例詳解

    這篇文章主要介紹了Java was started but returned exit code=13問題解決案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09

最新評論