:
复制代码代码如下:
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;
classe publique dbmanager {
Final statique final statique <Connexion> CONNS = nouveau ThreadLocal <Connexion> ();
Boolean statique privé show_sql = true;
Connexion statique finale publique getConnection () lève sqlexception {
Connexion conn = (connection) Conns.get ();
if (conn == null || conn.isclosed ()) {
// 这里使用我定义的一个简单的 ConnectionProvider 替代 DataSource 获取 Connexion
Conn = ConnectionProvider.getConnection ();
Conns.set (Conn);
}
return (show_sql &&! proxy.isproxyclass (conn.getClass ()))?
Nouveau _debugConnection (Conn) .getConnection (): Conn;
}
/ **
* 关闭连接
* /
Public final statique void CloseConnection () {
Connexion conn = (connection) Conns.get ();
essayer {
if (conn! = null &&! Conn.isclosed ()) {
conn.setAutoCommit (true);
conn.close ();
}
} catch (sqlexception e) {
}
Conns.set (null);
}
/ **
* 用于跟踪执行的 SQL 语句
* /
classe statique _debugConnection implémente invocationHandler {
connexion privée conn = null;
public _debugConnection (connexion conn) {
this.conn = conn;
}
Connexion publique getConnection () {
return (connection) proxy.newproxyinstance (Conn.getClass (). getClassOader (), new class [] {connection.class}, this);
}
invoque d'objet public (proxy d'objet, méthode m, objet [] args) lance le jetable
{
essayer
{
Méthode de chaîne = m.getName ();
if ("préparation" .equals (méthode) || "CreateStAlement" .Equals (méthode))
{
System.out.println (méthode);
System.out.println (args [0]);
}
retour M.Invoke (Conn, Args);
} catch (invocationTargetException e) {
lancer e.getTargetException ();
}
}
}
}
package com.lwj.test.proxy;
import java.sql.connection;
Importer Java.Sql.DiverManager;
classe publique ConnectionProvider {
Connexion statique publique getConnection ()
{
Connexion connexion = null;
essayer{
Class.forname ("oracle.jdbc.oracledriver"). NewInstance ();
connexion = driverManager.getConnection ("JDBC: Oracle: mince: @ 192.168.1.101: 1521: Orcl", "Scott", "Tiger");
} catch (exception e) {
}
connexion de retour;
}
}
package com.lwj.test.proxy;
import java.sql.connection;
Importer java.sql.PreparedStatement;
import java.sql.resultSet;
import java.sql.sqlexception;
import java.sql.statement;
classe publique testmain {
public static void main (String [] args)
{
Connexion conn = null;
Déclaration stmt = null;
Préparé Pstmt = null;
essayer
{
Conn = dbmanager.getConnection ();
STMT = Conn.CreatEstatement (resultSet.Type_Scroll_Sensitive, resultSet.CUCUR_UPDATable);
STMT.ExecuteUpdate ("Insérer dans Test1 (id, nom, carte, âge, adresse) (9, 'liuwj', '123456789098877", 24,' Hubeitianmen ') ");
/ * pstmt = conn.preparestatement ("insérer dans Test1 (id, nom, carte, âge, adresse) valeurs (?,?,?,?)");
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) {
}enfin{
essayer{
if (pstmt! = null) {
pstmt.close ();
pstmt = null;
}
} catch (sqlexception e) {
}
Dbmanager.closeConnection ();
}
}
}
:
复制代码代码如下:
PSTMT = Conn.PrepareStatement ("Insérer dans Test1 (id, nom, carte, âge, adresse) VALEURS (?,?,?,?,?)");
PSTMT.SetString (1, "10");
PSTMT.SetString (2, "Liuwj2");
PSTMT.SetString (3, "1234567890988777");
PSTMT.SetString (4, "22");
PSTMT.SetString (5, "123456");
pstmt.Execute ();
才能打印出 SQL 语句。