java跟蹤執(zhí)行的sql語(yǔ)句示例分享
代碼:
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()){
// 這里使用我定義的一個(gè)簡(jiǎn)單的 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語(yǔ)句
*/
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();
}
}
}
論壇上看到用下列語(yǔ)句:
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語(yǔ)句。
- java 中createStatement()方法的實(shí)例詳解
- Java的JDBC中Statement與CallableStatement對(duì)象實(shí)例
- 詳解Java的JDBC中Statement與PreparedStatement對(duì)象
- 在Java的Hibernate框架中使用SQL語(yǔ)句的簡(jiǎn)單介紹
- 詳解Java的MyBatis框架中SQL語(yǔ)句映射部分的編寫(xiě)
- java實(shí)現(xiàn)簡(jiǎn)單的給sql語(yǔ)句賦值的示例
- 詳解JAVA生成將圖片存入數(shù)據(jù)庫(kù)的sql語(yǔ)句實(shí)現(xiàn)方法
- java執(zhí)行SQL語(yǔ)句實(shí)現(xiàn)查詢(xún)的通用方法詳解
- 10種Java開(kāi)發(fā)者編寫(xiě)SQL語(yǔ)句時(shí)常見(jiàn)錯(cuò)誤
- Java使用Statement接口執(zhí)行SQL語(yǔ)句操作實(shí)例分析
相關(guān)文章
Java File類(lèi)常用方法與文件過(guò)濾器詳解
Java File類(lèi)以抽象的方式代表文件名和目錄路徑名。該類(lèi)主要用于文件和目錄的創(chuàng)建、文件的查找和文件的刪除等。File對(duì)象代表磁盤(pán)中實(shí)際存在的文件和目錄。本篇文章我們來(lái)講解File類(lèi)的常用方法與文件過(guò)濾器2022-04-04java文字轉(zhuǎn)語(yǔ)音的實(shí)現(xiàn)示例
在Java中,我們可以使用第三方庫(kù)來(lái)實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音的功能,本文主要介紹了java文字轉(zhuǎn)語(yǔ)音的實(shí)現(xiàn)示例,選擇jacob技術(shù)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03EVCache緩存在Spring Boot中的實(shí)戰(zhàn)示例
這篇文章主要介紹了EVCache緩存在Spring Boot中的實(shí)戰(zhàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12消息隊(duì)列-kafka消費(fèi)異常問(wèn)題
這篇文章主要給大家介紹了關(guān)于kafka的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07基于SpringMVC攔截器實(shí)現(xiàn)接口耗時(shí)監(jiān)控功能
本文呢主要介紹了基于SpringMVC攔截器實(shí)現(xiàn)的接口耗時(shí)監(jiān)控功能,統(tǒng)計(jì)接口的耗時(shí)情況屬于一個(gè)可以復(fù)用的功能點(diǎn),因此這里直接使用 SpringMVC的HandlerInterceptor攔截器來(lái)實(shí)現(xiàn),需要的朋友可以參考下2024-02-02Spring?cloud負(fù)載均衡@LoadBalanced?&?LoadBalancerClient
由于Spring?cloud2020之后移除了Ribbon,直接使用Spring?Cloud?LoadBalancer作為客戶(hù)端負(fù)載均衡組件,我們討論Spring負(fù)載均衡以Spring?Cloud2020之后版本為主,學(xué)習(xí)Spring?Cloud?LoadBalance2023-11-11Java was started but returned exit code=13問(wèn)題解決案例詳解
這篇文章主要介紹了Java was started but returned exit code=13問(wèn)題解決案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09