随笔- 66  文章- 0  评论- 15  阅读- 10万 
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
         
    }
 
}

 

 posted on   zhangbz  阅读(679)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示