[javaEE] 資料庫連接池和動態代理,javaee資料庫連接
實現javax.sql.DataSource介面
實現Connection getConnection()方法
定義一個靜態成員屬性LinkedList類型作為串連池,在靜態代碼塊中初始化5條資料庫連接,添加到串連池中,在getConnection方法中,當擷取串連的時候在串連池中remove掉一條串連就可以了
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;public class JDBCTest { public static void main(String[] args) throws Exception { //使用反射的方式 Class.forName("com.mysql.jdbc.Driver"); //擷取資料庫連接,導包的時候,注意要導java.sql下的,面向介面編程 MyPool pool=new MyPool(); Connection conn=pool.getConnection(); //擷取傳輸器對象 Statement statement=conn.createStatement(); //擷取結果集對象 ResultSet resultSet=statement.executeQuery("select * from user"); //遍曆 while(resultSet.next()){ String username=resultSet.getString("username"); System.out.println(username); } //關閉資源 resultSet.close(); statement.close(); pool.resetConn(conn); }}
我的串連池
import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.SQLFeatureNotSupportedException;import java.util.LinkedList;import java.util.List;import java.util.logging.Logger;import javax.sql.DataSource;/** * 手寫串連池 * * @author taoshihan * */public class MyPool implements DataSource { // 串連池 public static List<Connection> pool = new LinkedList<Connection>(); // 初始化 static { try { Class.forName("com.mysql.jdbc.Driver"); for (int i = 0; i < 5; i++) { Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/java", "root", "root"); pool.add(conn); } } catch (Exception e) { } } /** * 擷取串連 */ @Override public Connection getConnection() throws SQLException { // 如果池中沒有串連 if (pool.size() == 0) { for (int i = 0; i < 5; i++) { Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/java", "root", "root"); pool.add(conn); } } //先進先出 Connection conn=pool.remove(0); System.out.println("擷取一個串連,池裡還剩餘"+pool.size()); return conn; } /** * 重設串連 */ public void resetConn(Connection conn){ try { if(conn!=null && !conn.isClosed()){ pool.add(conn); System.out.println("還回一個串連,池裡還剩餘"+pool.size()); } } catch (Exception e) { e.printStackTrace(); } } @Override public Connection getConnection(String username, String password) throws SQLException { return null; } @Override public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { // TODO Auto-generated method stub } @Override public void setLoginTimeout(int seconds) throws SQLException { // TODO Auto-generated method stub } @Override public int getLoginTimeout() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { // TODO Auto-generated method stub return null; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { // TODO Auto-generated method stub return false; }}
使用繼承,裝飾,動態代理改造一個類中的方法
繼承的缺點:此時我們已經得到了Connection對象,因此無法通過繼承改造這個對象
裝飾的測試實現:
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;public class JDBCTest { public static void main(String[] args) throws Exception { //測試裝飾模式 Animal dog=new BigDog(new Dog()); dog.eat(); dog.sound(); }}/** * 裝飾模式測試 * @author taoshihan * */interface Animal{ public void eat(); public void sound();}class Dog implements Animal{ @Override public void eat() { System.out.println("吃"); } @Override public void sound() { System.out.println("汪"); }}//此時我想修改Dog類中的sound方法class BigDog implements Animal{ private Dog dog; public BigDog(Dog dog) { this.dog=dog; } /** * 這個方法調原來的 */ @Override public void eat() { dog.eat(); } /** * 這個方法進行裝飾 */ @Override public void sound() { System.out.println("大叫"); } }
動態代理:
//測試代理程式模式 final Dog dog=new Dog(); Animal proxy=(Animal) Proxy.newProxyInstance(Dog.class.getClassLoader(),Dog.class.getInterfaces() , new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if("sound".equals(method.getName())){ System.out.println("大叫"); return null; }else{ return method.invoke(dog, args); } } }); proxy.eat(); proxy.sound();