1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.zhangbz.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.zhangbz.pool.MyPool; public class JDBCDemo1 { public static void main(String[] args) { Connection conn = null ; PreparedStatement ps = null ; ResultSet rs = null ; MyPool pool = new MyPool(); try { conn = pool.getConnection(); ps = conn.prepareStatement( "select * from account" ); rs = ps.executeQuery(); while (rs.next()) { String name = rs.getString( "name" ); System.out.println(name); } } catch (Exception e) { e.printStackTrace(); } finally { if (rs != null ) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { rs = null ; } } if (ps != null ) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { ps = null ; } } if (conn != null ) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } finally { conn = null ; } } } } } |
MyPool.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | package com.zhangbz.pool; import java.io.PrintWriter; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.LinkedList; import java.util.List; import javax.sql.DataSource; public class MyPool implements DataSource{ private 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:///Day11" , "root" , "root" ); pool.add(conn); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public Connection getConnection() throws SQLException { if (pool.size() == 0 ) { for ( int i = 0 ; i < 3 ; i++) { Connection conn = DriverManager.getConnection( "jdbc:mysql:///Day11" , "root" , "root" ); pool.add(conn); } } final Connection conn = pool.remove( 0 ); //--利用动态代理改造close方法(还可以使用继承和装饰) Connection proxy = (Connection) Proxy.newProxyInstance(conn.getClass().getClassLoader(), conn.getClass().getInterfaces(), new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if ( "close" .equals(method.getName())) { //对于想要改造的方法我们自己写 retConn(conn); return null ; } else { //对于不想改造的方法,调用被代理者身上的方法 return method.invoke(conn, args); } } }); System.out.println( "获取了一个连接,池里还剩余" +pool.size()+ "个连接" ); return proxy; } private void retConn(Connection conn) { try { if (conn != null && !conn.isClosed()) { pool.add(conn); System.out.println( "换回来一个连接,池里还剩余" +pool.size()+ "个连接" ); } } catch (SQLException e) { e.printStackTrace(); } } public Connection getConnection(String arg0, String arg1) throws SQLException { // TODO Auto-generated method stub return null ; } public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null ; } public int getLoginTimeout() throws SQLException { // TODO Auto-generated method stub return 0 ; } public void setLogWriter(PrintWriter arg0) throws SQLException { // TODO Auto-generated method stub } public void setLoginTimeout( int arg0) throws SQLException { // TODO Auto-generated method stub } } |
#学习笔记,如有谬误,敬请指正。#
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步