Code:
Copy Code code as follows:
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 ()) {
Here I define a simple connectionprovider instead of DataSource to get connection
conn = Connectionprovider.getconnection ();
Conns.set (conn);
}
Return (Show_sql &&! Proxy.isproxyclass (Conn.getclass ())?
New _debugconnection (conn). getconnection (): conn;
}
/**
* Close Connection
*/
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 statement for tracking execution
*/
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 ();
}
}
}
The forum is seen with the following statements:
Copy Code code as follows:
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 ();
To print out the SQL statement.