1
81041
2019-06-24 7a764effdf2e4f0ff2753f81cdb6f83bc8bd9b0b
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.fgkj.db;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
 
import com.mchange.v2.c3p0.ComboPooledDataSource;
 
public class DBUtil {
    private static ComboPooledDataSource mysql_ds = new ComboPooledDataSource();
    private static String classDriver="com.mysql.jdbc.Driver";
    //private static String burl="jdbc:mysql://192.168.0.34:5306/";
    //private static String username="website";
    //private static String password="amtb3737";
    
    
    private static String username="root";
    private static String password="lmx8688139";
    private static String url="jdbc:mysql://118.89.139.230:3360/db_user";
    //private static String url="jdbc:mysql://123.207.82.239:3360/db_user";
    //private static String url="jdbc:mysql://192.168.0.34:3360/db_user";
    //private static String url="jdbc:mysql://127.0.0.1:3360/db_user";
    /*private static String username="root";
    private static String password="123456";*/
    
    
    private static int conncount_max = 500;
    private static int conncount_min=2;
    private static int MaxStatements = 0;
    private static int MaxIdleTime=60;
    private static int InitialPoolSize=2;
    
    static{
        try {
            //Thread.sleep(1000*60);
            mysql_ds.setDriverClass(classDriver);
            mysql_ds.setUser(username);
            mysql_ds.setPassword(password);
            mysql_ds.setMaxPoolSize(conncount_max);
            mysql_ds.setMinPoolSize(conncount_min);
            //mysql_ds.setIdleConnectionTestPeriod(60);
            mysql_ds.setJdbcUrl(url);
            
            // 设置连接池中的最大Statements数量!  
            mysql_ds.setMaxStatements(MaxStatements); 
            
            // 设置连接池的最大空闲时间!  
            mysql_ds.setMaxIdleTime(MaxIdleTime); 
            // 设置初始连接池的大小!  
            mysql_ds.setInitialPoolSize(InitialPoolSize);
            mysql_ds.setIdleConnectionTestPeriod(60);
            //mysql_ds.setPreferredTestQuery("SELECT 1");
            //mysql_ds.setIdleConnectionTestPeriod(1800);
            
            //mysql_ds.setMaxIdleTime(25000);
            //mysql_ds.setAcquireRetryDelay(100);
             
            //mysql_ds.setBreakAfterAcquireFailure(false);
            
            //当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒,默认为0
            //mysql_ds.setCheckoutTimeout(10000);
            
            //因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
            //时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
            //等方法来提升连接测试的性能。Default: false
            //mysql_ds.setTestConnectionOnCheckin(false);
            
            //如果设置为true,每次从池中取一个连接,将做一下测试,使用automaticTestTable 或者 preferredTestQuery,做一条查询语句.看看连接好不好用,不好用,就关闭它,重新从池中拿一个.
            //mysql_ds.setTestConnectionOnCheckout(true);
            //System.out.println("11111111111111111111111111");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    public static Connection getConn(){
        /*//System.out.println(DBUtil.getUrl());
        Connection conn=null;
        try {
            Class.forName(classDriver);
            conn=DriverManager.getConnection(url+databaseName, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
        }*/
        Connection conn = null;
        try {
            conn = mysql_ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    //获取当前连接数
    public static void getConnections(){
        try {
            //System.out.println("最大连接数: "+mysql_ds.getMaxPoolSize());// 最大连接数
            //System.out.println("最小连接数: "+mysql_ds.getMinPoolSize());// 最小连接数
            System.out.println("正在使用连接数: "+mysql_ds.getNumBusyConnections());// 正在使用连接数
            System.out.println("空闲连接数: "+mysql_ds.getNumIdleConnections());// 空闲连接数
            //System.out.println("总连接数: "+mysql_ds.getNumConnections());// 总连接数
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }
    //设置IP地址
    public static void     setUrl(String ip) {
        DBUtil.url = "jdbc:mysql://"+ip+":5306";
        //System.out.println(DBUtil.url);
    }
   
    public static String getUrl() {
        return url;
    }
    //关闭资源
    public static void close(ResultSet rs,Statement stat,Connection conn){
        try {
            if(rs != null)
                rs.close();
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(stat != null)
                stat.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn != null){
                conn.close();                
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 初始化连接池
     */
    public static void initPool(){
        close(null, null, getConn());
    }
    
    public static void main(String[] args){
        //System.out.println("start...");
        //System.out.println(getConn());
        //System.out.println("end...");
        Connection conn=DBUtil.getConn();
        DBUtil.getConnections();
        DBUtil.close(null, null, conn);
        while(true){
            DBUtil.getConnections();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        
    }
}