Blame view

src/main/java/com/essa/framework/DButil.java 3.57 KB
27d9a429   suweicheng   开发梳理;
1
2
  package com.essa.framework;
  
ca59c24b   suweicheng   开发梳理;
3
4
5
6
  import com.mysql.cj.api.jdbc.Statement;
  import com.mysql.cj.jdbc.CallableStatement;
  import com.mysql.cj.jdbc.PreparedStatement;
  
27d9a429   suweicheng   开发梳理;
7
8
9
10
11
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
27d9a429   suweicheng   开发梳理;
12
  public class DButil {
ca59c24b   suweicheng   开发梳理;
13
  	/*// 数据库驱动名字
27d9a429   suweicheng   开发梳理;
14
15
16
17
18
19
20
  	private static String jdbcName = null;
  	// 数据库协议地址
  	private static String dbUrl = null;
  	// 数据库用户名
  	private static String dbUser = null;
  	// 数据库密码
  	private static String dbPassword = null;
ca59c24b   suweicheng   开发梳理;
21
  */
27d9a429   suweicheng   开发梳理;
22
  	public static Connection getCon() {
ca59c24b   suweicheng   开发梳理;
23
  
27d9a429   suweicheng   开发梳理;
24
  		Connection conn = null;
ca59c24b   suweicheng   开发梳理;
25
26
27
28
29
30
31
32
33
34
  		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())) {
27d9a429   suweicheng   开发梳理;
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
  			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();
  			}
  		}
ca59c24b   suweicheng   开发梳理;
83
  		return null;*/
27d9a429   suweicheng   开发梳理;
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
  	}
  
  	/**
  	 * 关闭连接
  	 * 
  	 * @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();
  				}
  			}
  		}
  	}
  }