Repository navigation
Expand file tree
/
Copy pathUserInfoDAO.java
More file actions
executable file
·157 lines (147 loc) · 3.66 KB
/
Copy pathUserInfoDAO.java
File metadata and controls
executable file
·157 lines (147 loc) · 3.66 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
155
156
157
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.entity.*;
public class UserInfoDAO {
private BaseDAO dao = new BaseDAO();
/**
* 创建注册用户
*
* @param userName
* 用户名
* @param passWord
* 密码
* @param sex
* 性别
* @param userFace
* 头像图片名
* @return boolean 返回true表示注册成功
*/
public boolean checkReg(String userName, String passWord, boolean sex,
String userFace) {
String sql = "insert into userInfo(uName,uPassWord,uSex,uFace) values(?,?,?,?)";
int result = -1;
try {
result = dao.executeUpdate(sql, new Object[] { userName, passWord,
sex, userFace });
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeStatement();
dao.closeConnection();
}
return result > 0 ? true : false;
}
// 测试
public UserInfo getUserInfoByID(int uid) {
UserInfo user = new UserInfo();
String sql = "select * from userInfo where uId=?";
ResultSet rs = null;
try {
rs = dao.executeQuery(sql, new Object[] { uid });
if (rs != null && rs.next()) {
user.setUid(rs.getInt("uId"));
user.setUname(rs.getString("uName"));
user.setUpassword(rs.getString("uPassWord"));
user.setUsex(rs.getBoolean("uSex"));
user.setUface(rs.getString("uFace"));
user.setUregtime(rs.getDate("uRegTime"));
user.setUtype(rs.getInt("uType"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return user;
}
/**
* 检查用户登录名和密码
*
* @param userName
* 用户名
* @param passWord
* 密码
* @return boolean 返回true表示用户合法
*/
public boolean checkLogin(String userName, String passWord) {
String sql = "select * from userInfo where uName=? and uPassWord=? ";
ResultSet rs = null;
try {
rs = dao.executeQuery(sql, new Object[] { userName, passWord, });
} catch (SQLException e) {
e.printStackTrace();
}
try {
return (rs != null && rs.next()) ? true : false;
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return false;
}
/**
* 根据用户名,获得用户信息
*
* @param userName
* 用户名
* @return UserInfo 返回一个对象
*/
public UserInfo getUserInfo(String userName) {
UserInfo user = new UserInfo();
String sql = "select * from userInfo where uName=?";
ResultSet rs = null;
try {
rs = dao.executeQuery(sql, new Object[] { userName });
if (rs != null && rs.next()) {
user.setUid(rs.getInt("uId"));
user.setUname(rs.getString("uName"));
user.setUpassword(rs.getString("uPassWord"));
user.setUsex(rs.getBoolean("uSex"));
user.setUface(rs.getString("uFace"));
user.setUregtime(rs.getDate("uRegTime"));
user.setUtype(rs.getInt("uType"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return user;
}
/**
* 根据用户类型id,获得名称
*
* @param id
* 用户类型编号
* @return 返回用户类型名称
*/
public String getUserTypeNameById(Integer id) {
switch (id) {
case 0:
return "会员";
case 1:
return "版主";
case 2:
return "管理员";
}
return "";
}
/**
* 根据布尔值获得性别名称
*
* @param sex
* 布尔值 true:男 false:女
* @return 返回性别名
*/
public String getSexName(Boolean sex) {
return sex ? "男" : "女";
}
}