Administrator
2022-06-17 c5bff04a7761b7aca5dbd511a5989ebab20adb24
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
package com.power.mysql;
 
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
 
import com.mchange.v2.c3p0.ComboPooledDataSource;
 
public class MysqlConnPool {
    private ComboPooledDataSource mysql_ds = new ComboPooledDataSource();
    private int mSqlPort = 5306;
    
    public MysqlConnPool(String server_ip, int port, int conncount_max)
    {
        try {
            init(server_ip, port, conncount_max);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
    public void init(String server_ip, int port, int conncount_max) throws PropertyVetoException
    {
        mSqlPort = port;
        mysql_ds.setDriverClass("com.mysql.jdbc.Driver");
        mysql_ds.setJdbcUrl("jdbc:mysql://" + server_ip + ":" + mSqlPort);
        mysql_ds.setUser("root");
        mysql_ds.setPassword("lmx8688139");
        mysql_ds.setMaxPoolSize(conncount_max);
        mysql_ds.setMinPoolSize(2);
        mysql_ds.setIdleConnectionTestPeriod(300);
    }
    public Connection getConn()
    {
        Connection con = null;
        try {
            con = mysql_ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    
    public int getSqlConnPort() {
        return mSqlPort;
    }
    
    public int getNumBusyConnections() {
        int cnt = 0;
        try {
            cnt = mysql_ds.getNumBusyConnections();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return cnt;
    }
    
    public int getNumIdleConnections() {
        int cnt = 0;
        try {
            cnt = mysql_ds.getNumIdleConnections();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return cnt;
    }
    
    public int getMaxPoolSize() {
        return (mysql_ds.getMaxPoolSize());
    }
    
    public void setMaxPoolSize(int size) {
        mysql_ds.setMaxPoolSize(size);
    }
}