1 Java 和 MySQL 怎麼建立連接配接
2 通過Java怎麼對資料庫進行操作
1 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
轉載于:https://www.cnblogs.com/NeverCtrl-C/p/6670273.html