CentreDBUtil.java
3.3 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
package com.espeed.reading.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
/**
*
*
* @项目名称: 邮件跟踪系统
* @版权所有: 深圳市科飞时速网络技术有限公司(0755-88843776)
* @技术支持: info@21gmail.com
* @单元名称: 操作用户中心数据库的工具类
* @开始时间: 2018-5-23
* @开发人员: 杨志钊
*/
public class CentreDBUtil {
public static String DBUrl = ConfigPath.getCentreUrl();
public static String DBUser = ConfigPath.getCentreUsername();
public static String DBPassword = ConfigPath.getCentrePassword();
public static final String DBDriver = "com.mysql.jdbc.Driver";
public static Connection conn;
// 加载驱动
static {
try {
Class.forName(DBDriver);
} catch (Exception e) {
e.printStackTrace();
}
}
// 获得连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DBUrl, DBUser, DBPassword);
}
/** 关闭数据库连接 */
public static void close(ResultSet rs, Statement st, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static Map<String, Object> getUserInfo(String loginId, String domain) {
Map<String, Object> userInfo = new HashMap<String, Object>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = CentreDBUtil.getConnection();
String sql = "select u.user_id,u.company_id from centre_user as u,centre_company as c where u.company_id = c.company_id and u.login_id = ? and c.domain = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, loginId);
ps.setString(2, domain);
rs = ps.executeQuery();
if (rs.first()) {
userInfo.put("code", 200);
userInfo.put("userId", rs.getObject("user_id"));
userInfo.put("companyId", rs.getObject("company_id"));
} else {
userInfo.put("code", 400);
userInfo.put("msg", "不存在的账号->" + (loginId + "@" + domain));
}
} catch (SQLException e) {
userInfo.put("code", 500);
userInfo.put(
"msg",
"异常:"
+ e.getClass().getName()
+ (e.getMessage() == null ? "" : ("->" + e
.getMessage())));
} finally {
CentreDBUtil.close(rs, ps, conn);
}
return userInfo;
}
public static int getUserId(Integer companyId){
int userId = 0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = CentreDBUtil.getConnection();
String sql = "select u.user_id from centre_user as u,centre_company as c where u.company_id = c.company_id and c.company_id = ? and u.user_role = 1 limit 1";
ps = conn.prepareStatement(sql);
ps.setInt(1, companyId);
rs = ps.executeQuery();
if (rs.first()) {
userId = rs.getInt("user_id");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CentreDBUtil.close(rs, ps, conn);
}
return userId;
}
}