hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
package chat.util;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import com.alibaba.druid.pool.DruidDataSource;
 
public class DBUtil {
 
    private DruidDataSource dataSource;
    
    public static boolean IsEmbedDB = false;
    
    public DBUtil() {
        dataSource = new DruidDataSource();
        this.dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        this.dataSource.setUrl("jdbc:mysql://rm-uf65021a947ky5h453o.mysql.rds.aliyuncs.com:3306/medeasy_doctor?useSSL=false");
        this.dataSource.setUsername("user");
        this.dataSource.setPassword("U7t6XJnnHMii");
        //设置连接池初始化的连接个数
        //this.dataSource.setPoolPreparedStatements(true);
        //this.dataSource.setMaxOpenPreparedStatements(25);
        this.dataSource.setInitialSize(50);
        this.dataSource.setMaxActive(100);
        this.dataSource.setMinIdle(20);
    }
    
    public DruidDataSource getDataSource() {
        return dataSource;
    }
    
    public Connection getConnection() {
        Connection conn = null;
        try {
              conn = dataSource.getConnection();
        } catch (SQLException e) {
              e.printStackTrace();
        }
        return conn;
    }
    
    public void close(Connection conn, PreparedStatement pst, ResultSet rs) {
        if (rs != null) {
            try {
                  rs.close();
            } catch (SQLException e) {
                  e.printStackTrace();
            }
        }
        close(conn, pst);
    }
    
    public void close(Connection conn, PreparedStatement pst) {
        if (pst != null) {
            try {
                 pst.close();
            } catch(SQLException e) {
                 e.printStackTrace();
            }
        }
        close(conn);
    }
    
    public void close(Connection conn) {
        if (conn != null) {
            try {
                 conn.close();
            } catch(SQLException e) {
                 e.printStackTrace();
            }
        }
    }
    
    public void commit(Connection conn) {
        if (conn != null) {
            try {
                 conn.commit();
            } catch(SQLException e) {
                 e.printStackTrace();
            }
        }
    }
    
    public void rollback(Connection conn) {
        if (conn != null) {
            try {
                 conn.rollback();
            } catch(Exception e) {
                 e.printStackTrace();
            }
        }
    }
}