實現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();