forked from hyunsooBang/Database
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWelcome.java
More file actions
602 lines (503 loc) · 19.8 KB
/
Welcome.java
File metadata and controls
602 lines (503 loc) · 19.8 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import java.util.Scanner;
import java.sql.Connection;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
// 유저 클래스
class User {
private String name;
private String number;
// 유저 생성
public User(String name, String number) {
this.name = name; // 유저 이름
this.number = number; // 유저 학번
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
public class Welcome {
static User mUser; // 현재 로그인한 유저
static List<User> userList = new ArrayList<>(); // 유저 리스트
static ResultSet stuRs; // 학생 정보 결과셋
static Statement stmt; // SQL 문 실행을 위한 Statement 객체
static Connection conn; // 데이터베이스 연결 객체
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// MySQL 데이터베이스에 연결 (본인의 mysql 비밀번호 입력)
//conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/project", "root", "root12124");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root", "tjdwns246246");
System.out.println("MySQL DB 연결 성공");
// SQL 연결
stmt = conn.createStatement();
String sql = "select* from student";
stuRs = stmt.executeQuery(sql);
while (stuRs.next()) {
String id = stuRs.getString("id");
String name = stuRs.getString("name");
// 데이터베이스에 저장된 student id와 name을 유저 리스트에 저장
userList.add(new User(name, id));
}
stuRs.close();
} catch (ClassNotFoundException error) {
System.out.println("MySQL 드라이버 미설치 또는 드라이버 이름 오류");
} catch (SQLException error) {
System.out.println("DB 접속 오류");
System.out.println("오류 코드: " + error.getErrorCode());
System.out.println("오류 메시지: " + error.getMessage());
error.printStackTrace(); // 스택 트레이스 출력
}
Scanner input = new Scanner(System.in);
boolean validUser = false;
while (!validUser) {
System.out.print("이름을 입력하세요: ");
String userName = input.nextLine();
System.out.print("학번을 입력하세요: ");
String userNumber = input.nextLine();
// 사용자 검증
// 입력받은 유저 이름 및 학번이 유저리스트 내에 존재하는지 확인
for (User user : userList) {
if (user.getName().equals(userName) && user.getNumber().equals(userNumber)) {
validUser = true;
mUser = user;
break;
}
}
if (!validUser) {
System.out.println("유저가 존재하지 않습니다. 다시 입력해주세요.");
}
}
String greeting = "Ewha Course Evaluation";
String tagline = "Welcome to ECE!";
boolean quit = false;
while (!quit) {
System.out.println("***********************************************");
System.out.println("\t" + greeting);
System.out.println("\t" + tagline);
System.out.println("\tLogged in as: " + mUser.getName());
menuIntroduction();
System.out.print("메뉴 번호를 선택해주세요: ");
int n = input.nextInt();
if (n < 1 || n > 6) {
System.out.println("1부터 5까지의 숫자를 입력하세요.");
} else {
if (mUser != null) {
switch (n) {
case 1:
// 강의평 삽입 함수
insertECE();
break;
case 2:
// 강의평 검색 함수
searchECE();
break;
case 3:
// 강의평 수정 함수
modifyECE();
break;
case 4:
// 강의평 삭제 함수
deleteECE();
break;
case 5:
// 유저 정보 출력 함수
menuGuestInfo(mUser.getName(), mUser.getNumber());
break;
case 6:
// 종료
menuExit();
quit = true;
break;
}
} else {
System.out.println("유저가 존재하지 않습니다.");
}
}
}
}
// 메뉴 설명 함수
public static void menuIntroduction() {
System.out.println("******************************");
System.out.println(" 1. 강의평가 입력하기 ");
System.out.println(" 2. 강의평가 검색하기 ");
System.out.println(" 3. 강의평가 수정하기 ");
System.out.println(" 4. 강의평가 삭제하기 ");
System.out.println(" 5. 회원정보 ");
System.out.println(" 6. 로그아웃 ");
System.out.println("******************************");
}
// 현재 유저 정보 출력하는 함수
public static void menuGuestInfo(String name, String mobile) {
System.out.println("현재 고객 정보 : ");
System.out.println("이름 " + mUser.getName() + " 학번 " + mUser.getNumber());
}
// 강의평가 검색하는 함수
public static void searchECE() {
System.out.println("1. 강의명 및 교수명으로 검색하기");
System.out.println("2. 강의내용으로 검색하기");
System.out.println("3. 강의명 및 교수명으로 평균 강의평 검색하기");
Scanner input = new Scanner(System.in);
System.out.print("검색할 번호를 입력하세요: ");
int searchOption = input.nextInt();
switch (searchOption) {
case 1:
searchCourse(); // 강의명 및 교수명으로 검색 메서드 호출
break;
case 2:
searchContents(); // 강의내용으로 검색 메서드 호출
break;
case 3:
searchAverageRating(); // 강의명 및 교수명으로 평균 강의평 검색 메서드 호출
break;
default:
System.out.println("유효하지 않은 옵션입니다.");
}
}
// 강의평 이름과 교수명으로 강의평 검색하는 함수
public static void searchCourse() {
// 검색할 강의명과 교수명을 입력 받음
Scanner input = new Scanner(System.in);
System.out.print("검색할 강의명을 입력하세요: ");
String courseName = input.nextLine();
System.out.print("검색할 교수님 성함을 입력하세요: ");
String professorName = input.nextLine();
System.out.println("----------------------------");
try {
String dropViewSql = "DROP VIEW IF EXISTS V";
stmt.executeUpdate(dropViewSql);
// 임시로 뷰를 생성하여 검색 조건에 해당하는 데이터를 조회
String createViewSql = "CREATE VIEW V AS " +
"SELECT c.title, c.id AS course_id, r.student_id, r.contents, r.point, c.prof_id " +
"FROM rating r " +
"JOIN course c ON r.course_id = c.id " +
"WHERE c.title LIKE ?";
PreparedStatement createViewStmt = conn.prepareStatement(createViewSql);
createViewStmt.setString(1, "%" + courseName + "%");
createViewStmt.executeUpdate();
// 생성된 view와 professor 테이블을 조인하여 데이터 출력
String selectSql = "SELECT v.title, v.course_id, p.name AS professor_name, v.student_id, v.contents, v.point " +
"FROM V v " +
"JOIN professor p ON v.prof_id = p.id " +
"WHERE p.name LIKE ?";
PreparedStatement selectStmt = conn.prepareStatement(selectSql);
selectStmt.setString(1, "%" + professorName + "%");
ResultSet result = selectStmt.executeQuery();
while (result.next()) {
String title = result.getString("title");
String courseId = result.getString("course_id");
professorName = result.getString("professor_name");
String studentId = result.getString("student_id");
String contents = result.getString("contents");
double ratingPoint = result.getDouble("point");
System.out.println("강의 ID: " + courseId);
System.out.println("강의명: " + title);
System.out.println("교수 이름: " + professorName);
System.out.println("학생 ID: " + studentId);
System.out.println("평점: " + ratingPoint);
System.out.println("강의평 내용: " + contents);
System.out.println("----------------------------");
}
result.close();
// 생성한 뷰 삭제
dropViewSql = "DROP VIEW V";
stmt.executeUpdate(dropViewSql);
} catch (SQLException e) {
System.out.println("검색 중 오류 발생: " + e.getMessage());
}
}
public static void searchContents() {
// 검색할 강의 내용을 입력 받음
Scanner input = new Scanner(System.in);
System.out.print("강의내용을 입력하세요: ");
String courseContents = input.nextLine();
System.out.println("----------------------------");
try {
// 기존에 생성된 'V' 뷰가 있으면 삭제
String dropViewSql = "DROP VIEW IF EXISTS V";
stmt.executeUpdate(dropViewSql);
// 임시로 뷰를 생성하여 검색 조건에 해당하는 데이터를 조회
String createViewSql = "CREATE VIEW V AS " +
"SELECT c.title, c.id AS course_id, r.contents, r.point, r.student_id, c.prof_id " +
"FROM rating r " +
"JOIN course c ON r.course_id = c.id " +
"WHERE r.contents LIKE ?";
PreparedStatement createViewStmt = conn.prepareStatement(createViewSql);
createViewStmt.setString(1, "%" + courseContents + "%");
createViewStmt.executeUpdate();
// 생성된 view와 professor 테이블을 조인하여 원하는 결과 출력
String selectSql = "SELECT v.title, v.course_id, p.name AS professor_name, v.contents, v.point, v.student_id " +
"FROM V v " +
"JOIN professor p ON v.prof_id = p.id";
PreparedStatement selectStmt = conn.prepareStatement(selectSql);
ResultSet result = selectStmt.executeQuery();
while (result.next()) {
String title = result.getString("title");
String courseId = result.getString("course_id");
String professorName = result.getString("professor_name");
String contents = result.getString("contents");
String studentId = result.getString("student_id");
double ratingPoint = result.getDouble("point");
System.out.println("강의 ID: " + courseId);
System.out.println("강의명: " + title);
System.out.println("교수 이름: " + professorName);
System.out.println("학생 ID: " + studentId);
System.out.println("평점: " + ratingPoint);
System.out.println("강의평 내용: " + contents);
System.out.println("----------------------------");
}
result.close();
// 생성한 뷰 삭제
String dropViewSql2 = "DROP VIEW V";
stmt.executeUpdate(dropViewSql2);
} catch (SQLException e) {
System.out.println("검색 중 오류 발생: " + e.getMessage());
}
}
// 특정 course의 평균 강의평점을 검색하는 함수
public static void searchAverageRating() {
Scanner input = new Scanner(System.in);
System.out.print("과목명을 입력하세요: ");
String courseTitle = input.nextLine();
System.out.print("교수명을 입력하세요: ");
String professorName = input.nextLine();
System.out.println("----------------------------");
// rating, course, professor 테이블을 조인하고, 특정 교수 이름과 강의 제목에 해당하는 레코드들의 평균 평점을 조회
try {
String sql = "SELECT AVG(point) AS average_rating " +
"FROM rating r " +
"JOIN course c ON r.course_id = c.id " +
"JOIN professor p ON c.prof_id = p.id " +
"WHERE p.name = ? AND c.title = ? " +
"GROUP BY c.title, p.name";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, professorName);
stmt.setString(2, courseTitle);
ResultSet result = stmt.executeQuery();
if (result.next()) {
double averageRating = result.getDouble("average_rating");
System.out.printf("평균 강의평점: %.2f%n", averageRating);
} else {
System.out.println("일치하는 강의평이 없습니다.");
}
result.close();
} catch (SQLException e) {
System.out.println("검색 중 오류 발생: " + e.getMessage());
}
}
// 자신이 작성한 강의평 수정하는 함수
public static void modifyECE() {
if (mUser != null) {
String studentId = mUser.getNumber();
String studentName = mUser.getName();
System.out.println("\n다음은 " + studentName + "님이 남기신 강의평 목록입니다.\n");
// studentId에 해당하는 학생의 강의평을 조회
try {
String selectSql = "SELECT r.rating_id, r.contents, r.point, c.title, p.name AS professor_name " +
"FROM rating r " +
"JOIN course c ON r.course_id = c.id " +
"JOIN professor p ON c.prof_id = p.id " +
"WHERE r.student_id = ?";
PreparedStatement selectStmt = conn.prepareStatement(selectSql);
selectStmt.setString(1, studentId);
ResultSet result = selectStmt.executeQuery();
// 강의평가가 없는 경우를 대비해 불리언 변수 생성
boolean hasRating = false;
// 강의평가가 있는 경우 결과 출력
while (result.next()) {
hasRating = true;
int ratingId = result.getInt("rating_id");
String contents = result.getString("contents");
double point = result.getDouble("point");
String courseTitle = result.getString("title");
String professorName = result.getString("professor_name");
System.out.println("강의평 ID: " + ratingId);
System.out.println("강의명: " + courseTitle);
System.out.println("교수 이름: " + professorName);
System.out.println("평점: " + point);
System.out.println("강의평 내용: " + contents);
System.out.println("-----------------------------");
}
// 강의평가가 없는 경우
if (!hasRating) {
System.out.println("강의평가가 없습니다.");
return;
}
System.out.println("강의평을 수정할 ID와 새로운 강의평 내용, 평점을 입력하세요.");
Scanner input = new Scanner(System.in);
System.out.print("강의평 ID: ");
int ratingId = input.nextInt();
input.nextLine(); // 개행 문자 제거
System.out.print("새로운 평점: ");
double newPoint = input.nextDouble();
// 강의평점은 1-5점만 입력 가능
if (newPoint < 1 || newPoint > 5) {
System.out.println("강의평점은 1점에서 5점 사이만 입력할 수 있습니다.");
return;
}
input.nextLine();
System.out.print("새로운 강의평 내용: ");
String newContents = input.nextLine();
// 유저에게 입력받은 새로운 평점과 내용으로 강의평 업데이트
String updateSql = "UPDATE rating " +
"SET contents = ?, point = ? " +
"WHERE rating_id = ? AND student_id = ?";
PreparedStatement updateStmt = conn.prepareStatement(updateSql);
updateStmt.setString(1, newContents);
updateStmt.setDouble(2, newPoint);
updateStmt.setInt(3, ratingId);
updateStmt.setString(4, studentId);
int rowsAffected = updateStmt.executeUpdate();
if (rowsAffected > 0) {
System.out.println("강의평 수정이 완료되었습니다.");
} else {
System.out.println("일치하는 강의평 ID가 없습니다.");
}
} catch (SQLException e) {
System.out.println("강의평 수정 중 오류 발생: " + e.getMessage());
}
} else {
System.out.println("유저가 존재하지 않습니다.");
}
}
// 새로운 강의평 추가 함수
public static void insertECE() {
try {
// 전체 강의목록 출력
System.out.println("<< 강의 목록 >>");
String sql = "SELECT c.id AS course_id, c.title AS course_title, p.name AS professor_name " +
"FROM course c " +
"JOIN professor p ON c.prof_id = p.id";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet courseRs = stmt.executeQuery();
while (courseRs.next()) {
String courseId = courseRs.getString("course_id");
String courseTitle = courseRs.getString("course_title");
String professorName = courseRs.getString("professor_name");
System.out.println("강의 ID: " + courseId + ", 강의명: " + courseTitle + ", 교수: " + professorName);
}
courseRs.close();
// 강의 ID, 평점, 내용을 입력받기
Scanner input = new Scanner(System.in);
System.out.print("평가하고 싶은 강의 ID를 입력하세요: ");
String courseId = input.nextLine();
System.out.print("강의 평점을 입력하세요: ");
double point = input.nextDouble();
input.nextLine(); // 개행 문자 제거
System.out.print("강의평 내용을 입력하세요: ");
String contents = input.nextLine();
contents = contents.replace("'", "''");
// 유저 정보 기반으로 입력받은 강의 ID, 평점, 내용을 rating 테이블에 삽입
sql = "INSERT INTO rating (course_id, prof_id, student_id, point, contents) " +
"SELECT ?, c.prof_id, s.id, ?, ? " +
"FROM course c, student s " +
"WHERE s.name = ? AND s.id = ? AND c.id = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, courseId);
stmt.setDouble(2, point);
stmt.setString(3, contents);
stmt.setString(4, mUser.getName());
stmt.setString(5, mUser.getNumber());
stmt.setString(6, courseId);
stmt.executeUpdate();
System.out.println("\n강의평이 성공적으로 추가되었습니다.");
} catch (SQLException e) {
System.out.println("강의평 추가 중 오류 발생: " + e.getMessage());
}
}
// 기존 강의평 삭제 함수
public static void deleteECE() {
if (mUser != null) {
String studentId = mUser.getNumber();
String studentName = mUser.getName();
try {
// 작성한 전체 강의평 보여주기
String selectSql = "SELECT r.rating_id, c.title AS course_title, p.name AS professor_name, r.point, r.contents " +
"FROM rating r " +
"JOIN course c ON r.course_id = c.id " +
"JOIN professor p ON c.prof_id = p.id " +
"WHERE r.student_id = ?";
PreparedStatement selectStmt = conn.prepareStatement(selectSql);
selectStmt.setString(1, studentId);
ResultSet result = selectStmt.executeQuery();
System.out.println("\n다음은 " + studentName + "님이 남기신 강의평 목록입니다.\n");
boolean hasRating = false;
while (result.next()) {
hasRating = true;
int ratingId = result.getInt("rating_id");
String courseTitle = result.getString("course_title");
String professorName = result.getString("professor_name");
double point = result.getDouble("point");
String contents = result.getString("contents");
System.out.println("강의평 ID: " + ratingId);
System.out.println("강의명: " + courseTitle);
System.out.println("교수 이름: " + professorName);
System.out.println("평점: " + point);
System.out.println("강의평 내용: " + contents);
System.out.println("-----------------------------");
}
if (!hasRating) {
System.out.println("강의평가가 없습니다.");
return;
}
// 삭제할 강의평 ID 입력받기
Scanner input = new Scanner(System.in);
System.out.print("삭제할 강의평 ID를 입력하세요: ");
int ratingId = input.nextInt();
// 본인이 작성한 강의평인지 확인 과정
String checkSql = "SELECT * FROM rating " +
"WHERE rating_id = ? AND student_id = ?";
PreparedStatement checkStmt = conn.prepareStatement(checkSql);
checkStmt.setInt(1, ratingId);
checkStmt.setString(2, studentId);
ResultSet checkResult = checkStmt.executeQuery();
if (checkResult.next()) {
// rating 테이블에서 강의평 삭제하기
String deleteSql = "DELETE FROM rating WHERE rating_id = ?";
PreparedStatement deleteStmt = conn.prepareStatement(deleteSql);
deleteStmt.setInt(1, ratingId);
deleteStmt.executeUpdate();
System.out.println("강의평 삭제가 완료되었습니다.");
} else {
System.out.println("일치하는 강의평 ID가 없거나 권한이 없습니다.");
}
checkResult.close();
} catch (SQLException e) {
System.out.println("강의평 삭제 중 오류 발생: " + e.getMessage());
}
} else {
System.out.println("유저가 존재하지 않습니다.");
}
}
// 로그아웃 함수 - mySQL 해제
public static void menuExit() {
System.out.println("로그아웃 되었습니다.");
try {
if (stuRs != null) {
stuRs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
System.out.println("MySQL 연결 해제 성공");
} catch (SQLException e) {
System.out.println("MySQL 연결 해제 실패: " + e.getMessage());
}
}
}