package day01;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Scanner;
11
12 /**
13 * 用户登录系统
14 * Description:
15 */
16 public class Service {
17 private static final int USER_REG = 1
;
18 private static final int USER_LOGIN = USER_REG + 1
;
19 private static final int USER_UPDATE = USER_LOGIN + 1
;
20 private static final int USER_DELETE = USER_UPDATE + 1
;
21 private static final int USER_INFO = USER_DELETE + 1
;
22 private static final int USER_TRANSFER = USER_INFO + 1
;
23 private static final int USER_QUIT = USER_TRANSFER + 1
;
24 private static final int EXIT = USER_QUIT + 1
;
25
26 UserInfo user =
null;
27
28 public static void main(String[] args) {
29 Service serv =
new Service();
30 serv.start();
31 }
32
33 private void start() {
34 welcome();
35 int code =
getCode();
36 execute(code);
37 }
38
39 /**
40 * 执行选择
41 * Description:
42 */
43 private void execute(
int code) {
44 switch (code) {
45 case USER_REG:
46 user_reg();
47 break;
48 case USER_LOGIN:
49 user_login();
50 break;
51 case USER_UPDATE:
52 user_update();
53 break;
54 case USER_DELETE:
55 user_delete();
56 break;
57 case USER_INFO:
58 user_info();
59 break;
60 case USER_TRANSFER:
61 user_transfer();
62 break;
63 case USER_QUIT:
64 user_quit();
65 break;
66 case EXIT:
67 exit();
68 break;
69 default:
70 System.out.println("输入错误,请重新输入"
);
71 start();
72 break;
73 }
74 }
75
76 /**
77 * Description:
78 */
79 private void exit() {
80 // TODO Auto-generated method stub
81 if(
null !=
this.user) {
82 System.out.println("当前用户还没有退出,所以执行自动退出当前用户"
);
83 user_quit();
84 }
else {
85 System.out.println("你选择了退出系统"
);
86 System.out.println("系统退出成功"
);
87 }
88
89 }
90
91 /**
92 * 退出当前用户
93 * Description:
94 */
95 private void user_quit() {
96 // TODO Auto-generated method stub
97 if(
null !=
this.user) {
98 System.out.println("你选择了退出当前用户功能"
);
99 this.user =
null;
100 if(
null ==
this.user) {
101 System.out.println("成功退出当前用户"
);
102 }
else {
103 System.out.println("退出当前用户失败"
);
104 }
105 }
else {
106 System.out.println("你还没有登录成功,还不能使用该功能"
);
107 System.out.println("请登录!"
);
108 user_login();
109 }
110 start();
111 }
112
113 /**
114 * 转账功能
115 * Description:
116 */
117 private void user_transfer() {
118 // TODO Auto-generated method stub
119 if(
null !=
this.user) {
120 System.out.println("你选择了转账功能!"
);
121 Scanner scanner =
new Scanner(System.in);
122 System.out.println("请输入转入账户的用户名:"
);
123 String name =
scanner.nextLine();
124 System.out.println("请输入转账金额:"
);
125 int money =
Integer.parseInt(scanner.nextLine());
126
127 Connection conn =
null;
128 try {
129 Class.forName("com.mysql.jdbc.Driver"
);
130 conn =
DriverManager.getConnection(
131 "jdbc:mysql://localhost:3306/test"
,
132 "root"
,
133 "182838"
);
134 Statement state =
conn.createStatement();
135
136 //转出
137 String out_sql = "UPDATE userinfo_fury "
138 + "SET account = account - ‘"+money+"‘ "
139 + "WHERE username = ‘"+
this.user.getUsername()+"‘ "
;
140 int judge01 =
state.executeUpdate(out_sql);
141 if(judge01 > 0
) {
142 System.out.println("转出成功"
);
143 }
else {
144 System.out.println("转出失败"
);
145 }
146
147 //转入
148 String in_sql = "UPDATE userinfo_fury "
149 + "SET account = account + ‘"+money+"‘ "
150 + "WHERE username = ‘"+name+"‘ "
;
151 int judge02 =
state.executeUpdate(in_sql);
152 if(judge02 > 0
) {
153 System.out.println("转入成功"
);
154 }
else {
155 System.out.println("转入失败"
);
156 }
157 }
catch(Exception e) {
158 e.printStackTrace();
159 }
finally {
160 if(
null !=
conn) {
161 try {
162 conn.close();
163 }
catch(SQLException e1) {
164 e1.printStackTrace();
165 }
166 }
167 }
168 }
else {
169 System.out.println("请先登录!"
);
170 user_login();
171 }
172 start();
173 }
174
175 /**
176 * 查询表中的所有数据
177 * Description:
178 */
179 private void user_info() {
180 // TODO Auto-generated method stub
181 if(
null !=
this.user) {
182 System.out.println("你选择了查询所有用户功能!"
);
183 Connection conn =
null;
184 try {
185 Class.forName("com.mysql.jdbc.Driver"
);
186 conn =
DriverManager.getConnection(
187 "jdbc:mysql://localhost:3306/test"
,
188 "root"
,
189 "182838"
);
190 Statement state =
conn.createStatement();
191 String sql = "SELECT id,username,password,email,nickname,account "
192 + "FROM userinfo_fury "
;
193 ResultSet rs =
state.executeQuery(sql);
194 List<UserInfo> list =
new ArrayList<UserInfo>
();
195
196 while(rs.next()) {
197 int id = rs.getInt("id"
);
198 String username = rs.getString("username"
);
199 String password = rs.getString("password"
);
200 String email = rs.getString("email"
);
201 String nickname = rs.getString("nickname"
);
202 double account = rs.getDouble("account"
);
203 UserInfo userinfo =
new UserInfo(id, username, password, email, nickname, account);
204 list.add(userinfo);
205 }
206 for(UserInfo lis : list) {
207 System.out.println(lis);
208 }
209 }
catch(Exception e) {
210 e.printStackTrace();
211 }
finally {
212 if(
null !=
conn) {
213 try {
214 conn.close();
215 }
catch(SQLException e1) {
216 e1.printStackTrace();
217 }
218 }
219 }
220 }
else {
221 System.out.println("请先登录"
);
222 user_login();
223 }
224 start();
225 }
226
227 /**
228 * 删除用户
229 * Description:
230 */
231 private void user_delete() {
232 // TODO Auto-generated method stub
233 if(
null !=
this.user) {
234 System.out.println("你选择了删除用户功能"
);
235 System.out.println("你不是超级用户,你无法使用删除用户功能"
);
236 }
else {
237 System.out.println("请先登录!"
);
238 user_login();
239 }
240 start();
241 }
242
243 /**
244 * 修改用户信息
245 * Description:
246 */
247 private void user_update() {
248 // TODO Auto-generated method stub
249 if(
null !=
this.user) {
250 System.out.println("你选择了修改当前用户功能!"
);
251 //可改进 -->> 可由用户选择需要修改的字段
252 System.out.println("你当前的昵称为:" +
this.user.getNickname());
253 Scanner scanner =
new Scanner(System.in);
254 System.out.println("你想将你的昵称修改为:"
);
255 String nickname =
scanner.nextLine();
256
257 Connection conn =
null;
258 try {
259 Class.forName("com.mysql.jdbc.Driver"
);
260 conn =
DriverManager.getConnection(
261 "jdbc:mysql://localhost:3306/test"
,
262 "root"
,
263 "182838"
);
264 Statement state =
conn.createStatement();
265
266 String sql = "UPDATE userinfo_fury "
267 + "SET nickname = ‘"+nickname+"‘ "
268 + "WHERE username = ‘"+
this.user.getUsername()+"‘ "
;
269 int judge =
state.executeUpdate(sql);
270 if(judge > 0
) {
271 this.user.setNickname(nickname);
272 System.out.println("修改昵称成功,当前昵称为:" +
this.user.getNickname());
273 }
else {
274 System.out.println("修改昵称失败"
);
275 }
276 }
catch(Exception e) {
277 e.printStackTrace();
278 }
finally {
279 if(
null !=
conn) {
280 try {
281 conn.close();
282 }
catch(SQLException e1) {
283 e1.printStackTrace();
284 }
285 }
286 }
287 }
else {
288 System.out.println("请登录成功后在进行此操作!"
);
289 user_login();
290 }
291 start();
292 }
293
294 /**
295 * 用户登录
296 * Description:
297 */
298 private void user_login() {
299 // TODO Auto-generated method stub
300 System.out.println("你选择了用户登录功能!"
);
301 Scanner scanner =
new Scanner(System.in);
302 System.out.println("请输入用户名:"
);
303 String username =
scanner.nextLine();
304 System.out.println("请输入密码:"
);
305 String password =
scanner.nextLine();
306
307 Connection conn =
null;
308 try {
309 Class.forName("com.mysql.jdbc.Driver"
);
310 conn =
DriverManager.getConnection(
311 "jdbc:mysql://localhost:3306/test"
,
312 "root"
,
313 "182838"
);
314 Statement state =
conn.createStatement();
315
316 String sql = "SELECT id, username, password,email, nickname,account "
317 + "FROM userinfo_fury "
318 + "WHERE username = ‘"+username+"‘ "
319 + "AND password = ‘"+password+"‘ "
;
320 System.out.println(sql);
321 ResultSet rs =
state.executeQuery(sql);
322 if(rs.next()) {
323 int id = rs.getInt("id"
);
324 String name = rs.getString("username"
);
325 String word = rs.getString("password"
);
326 String email = rs.getString("email"
);
327 String nickname = rs.getString("nickname"
);
328 double account = rs.getDouble("account"
);
329 UserInfo userinfo =
new UserInfo(id, name, word, email, nickname, account);
330 this.user =
userinfo;
331 System.out.println("登录成功,你的昵称为:" +
this.user.getNickname());
332 }
else {
333 System.out.println("登录失败:" +
this.user);
334 }
335 /*
336 * 注意:
337 * 当用户输入的密码个的格式是: 任意字符‘ or ‘数值开头 时无论用户名和密码正确与否,都会登录成功
338 * 因为 如果这样输入就改变了 SQL 语句的原意(在SQL语句中AND的优先级要高于OR)
339 * 实例 : asdfaer1234‘ or ‘1
340 */
341 }
catch(Exception e) {
342 e.printStackTrace();
343 }
finally {
344 if(
null !=
conn) {
345 try {
346 conn.close();
347 }
catch(SQLException e1) {
348 e1.printStackTrace();
349 }
350 }
351 }
352 start();
353 }
354
355 /**
356 * 用户注册
357 * Description:
358 */
359 private void user_reg() {
360 System.out.println("你选择了用户注册功能!"
);
361 Scanner scanner =
new Scanner(System.in);
362 System.out.println("请输入用户名:"
);
363 String username =
scanner.nextLine();
364 System.out.println("请输入密码:"
);
365 String password =
scanner.nextLine();
366 System.out.println("请输入邮箱:"
);
367 String email =
scanner.nextLine();
368 System.out.println("请输入昵称:"
);
369 String nickname =
scanner.nextLine();
370 Connection conn =
null;
371 try {
372 Class.forName("com.mysql.jdbc.Driver"
);
373 conn =
DriverManager.getConnection(
374 "jdbc:mysql://localhost:3306/test"
,
375 "root"
,
376 "182838"
);
377 Statement state =
conn.createStatement();
378 String sql = "INSERT INTO userinfo_fury "
379 + "(username,password,email,nickname) "
380 + "VALUES "
381 + "(‘"+username+"‘,‘"+password+"‘,‘"+email+"‘,‘"+nickname+"‘)"
;
382 int judge =
state.executeUpdate(sql);
383 if(judge > 0
) {
384 System.out.println("注册成功"
);
385 }
else {
386 System.out.println("注册失败"
);
387 }
388 }
catch(Exception e) {
389 e.printStackTrace();
390 }
finally {
391 if(
null !=
conn) {
392 try {
393 conn.close();
394 }
catch(SQLException e1) {
395 e1.printStackTrace();
396 }
397 }
398 }
399 start();
400 }
401
402 /**
403 * 功能选择
404 * Description:
405 */
406 private int getCode() {
407 System.out.println("请选择功能:"
);
408 Scanner scanner =
new Scanner(System.in);
409 int code =
Integer.parseInt(scanner.nextLine());
410 return code;
411 }
412
413 /**
414 * 界面信息
415 * Description:
416 */
417 private void welcome() {
418 System.out.println("欢迎使用用户登录系统!"
);
419 System.out.println("请输入需要操作的功能序号"
);
420 System.out.println("======================"
);
421 System.out.println("================"
);
422 System.out.println("1 : 用户注册"
);
423 System.out.println("2 : 用户登录"
);
424 System.out.println("3 : 修改用户信息"
);
425 System.out.println("4 : 删除用户"
);
426 System.out.println("5 : 查看所有用户信息"
);
427 System.out.println("6 : 转账业务"
);
428 System.out.println("7 : 用户退出"
);
429 System.out.println("8 : 退出系统"
);
430 System.out.println("================"
);
431 System.out.println("======================"
);
432 }
433 }
用户管理系统
上面程序用到的类
3 改进
改程序是典型的 高耦合、低内聚, 改进之处很多很多...
三少今天有点懒,笔记待更新...
2017年4月5日19:42:31
JDBC MySQL 实例之 用户管理系统
标签:inf rate new ack 界面 print generate delete pac