DButil.java
3.64 KB
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
package com.essa.framework;
import com.mysql.cj.api.jdbc.Statement;
import com.mysql.cj.jdbc.CallableStatement;
import com.mysql.cj.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DButil {
/*// 数据库驱动名字
private static String jdbcName = null;
// 数据库协议地址
private static String dbUrl = null;
// 数据库用户名
private static String dbUser = null;
// 数据库密码
private static String dbPassword = null;
*/
public static Connection getCon() {
Connection conn = null;
try {
//通过环境找寻相应的配置,然后初始化数据库连接
EnvEnum envEnum = EnvEnum.fromCode(Model.getEnv());
ConfigProperties dbConfig = ConfigProperties.getConfig(envEnum);
Class.forName(dbConfig.getJdbcName());
conn = DriverManager.getConnection(dbConfig.getDbUrl(),dbConfig.getDbUser(), dbConfig.getDbPassword());
} catch (Exception e) {
e.printStackTrace();
}
return conn;
/*if ("SIT".equals(Model.getEnv())) {
try {
jdbcName = DBInfo.getSitJdbcName();
dbUrl = DBInfo.getSitSqlUrl();
dbUser = DBInfo.getSitUser();
dbPassword = DBInfo.getSitPassword();
Class.forName(jdbcName);
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
} else if ("HOTFIX".equals(Model.getEnv())) {
try {
jdbcName = DBInfo.getHotfixJdbcName();
dbUrl = DBInfo.getHotfixSqlUrl();
dbUser = DBInfo.getHotfixUser();
dbPassword = DBInfo.getHotfixPassword();
Class.forName(jdbcName);
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
} else if ("DIT".equals(Model.getEnv())) {
try {
jdbcName = DBInfo.getDitJdbcName();
dbUrl = DBInfo.getDitSqlUrl();
dbUser = DBInfo.getDitUser();
dbPassword = DBInfo.getDitPassword();
Class.forName(jdbcName);
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
} else if ("UAT".equals(Model.getEnv())) {
try {
jdbcName = DBInfo.getUatJdbcName();
dbUrl = DBInfo.getUatSqlUrl();
dbUser = DBInfo.getUatUser();
dbPassword = DBInfo.getUatPassword();
Class.forName(jdbcName);
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;*/
}
/**
* 关闭连接
*
* @param stmt
* @param conn
* @throws Exception
*/
public static void close(Statement stmt, Connection conn) throws Exception {
if (stmt != null) {
stmt.close();
if (conn != null) {
conn.close();
}
}
}
/**
* 关闭连接
*
* @param cstmt
* @param conn
* @throws Exception
*/
public static void close(CallableStatement cstmt, Connection conn) throws Exception {
if (cstmt != null) {
cstmt.close();
if (conn != null) {
conn.close();
}
}
}
/**
* 关闭连接
*
* @param pstmt
* @param conn
* @throws SQLException
*/
public static void close(PreparedStatement pstmt, Connection conn) throws SQLException {
if (pstmt != null) {
pstmt.close();
if (conn != null) {
conn.close();
}
}
}
/**
* 重载关闭方法
*
* @param rs
* @param pstmt
* @param conn
* @throws Exception
*/
public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn) throws Exception {
if (rs != null) {
rs.close();
if (pstmt != null) {
pstmt.close();
if (conn != null) {
conn.close();
}
}
}
}
}