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