蓄电池监控管理平台数据库初始化程序
Administrator
2021-07-08 56c672f46d4b901b1ee4b1ee47768ed53ec5e6a8
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
package com.sql;
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(60);
    }
    public Connection getConn()
    {
        Connection con = null;
        try {
            con = mysql_ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    
    public int getSqlConnPort() {
        return mSqlPort;
    }
}