├── .classpath ├── .gitignore ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── Database └── StudentDB.sql ├── README.md └── src ├── DBConnection ├── Main.java └── Student.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.toptal.com/developers/gitignore/api/java 2 | # Edit at https://www.toptal.com/developers/gitignore?templates=java 3 | 4 | ### Java ### 5 | # Compiled class file 6 | *.class 7 | 8 | # User account file 9 | *.properties 10 | 11 | # Log file 12 | *.log 13 | 14 | # BlueJ files 15 | *.ctxt 16 | 17 | # Mobile Tools for Java (J2ME) 18 | .mtj.tmp/ 19 | 20 | # Package Files # 21 | *.jar 22 | *.war 23 | *.nar 24 | *.ear 25 | *.zip 26 | *.tar.gz 27 | *.rar 28 | 29 | # Virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 30 | hs_err_pid* 31 | 32 | # End of https://www.toptal.com/developers/gitignore/api/java 33 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/seopit95/Alignment-of-student-performance-programs-Java-Project/67fe876fd6231c8337e5305a2c8fab46f5ad5d33/.project -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=17 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning 13 | org.eclipse.jdt.core.compiler.release=enabled 14 | org.eclipse.jdt.core.compiler.source=17 15 | -------------------------------------------------------------------------------- /Database/StudentDB.sql: -------------------------------------------------------------------------------- 1 | drop database if exists StudentDB; 2 | create database StudentDB; 3 | use StudentDB; 4 | 5 | create table student( 6 | no varchar(6) not null primary key, 7 | name varchar(5) not null, 8 | kor tinyint not null, 9 | eng tinyint not null, 10 | math tinyint not null, 11 | total smallint null, 12 | avr decimal(7,2) null, 13 | grade char(1) null, 14 | rate tinyint null 15 | ); 16 | 17 | insert into student values('123456', '김자바', 90, 100, 95, 285, 95, 'A', 1); 18 | insert into student(no,name,kor,eng,math,total,avr,grade) values ('123458','김미니',100,100,100,300,100,'A'); 19 | 20 | 21 | drop procedure if exists procedure_insert_student; 22 | delimiter $$ 23 | create procedure procedure_insert_student( 24 | In in_no char(6), 25 | In in_name varchar(10), 26 | in in_kor int, 27 | in in_eng int, 28 | in in_math int 29 | ) 30 | begin 31 | declare in_total int; 32 | declare in_avr double; 33 | declare in_grade varchar(2); 34 | SET in_total = in_kor + in_eng + in_math; 35 | SET in_avr = in_total / 3.0; 36 | SET in_grade = 37 | CASE 38 | when in_avr >= 90.0 then 'A' 39 | when in_avr >= 80.0 then 'B' 40 | when in_avr >= 70.0 then 'C' 41 | when in_avr >= 60.0 then 'D' 42 | else 'F' 43 | end; 44 | 45 | insert into student(no, name, kor, eng, math) 46 | values(in_no, in_name, in_kor, in_eng, in_math); 47 | 48 | update student set total = in_total, avr = in_avr, grade = in_grade 49 | where no = in_no; 50 | 51 | end $$ 52 | delimiter ; 53 | call procedure_insert_student(); 54 | 55 | drop procedure if exists procedure_update_student; 56 | DELIMITER $$ 57 | CREATE PROCEDURE procedure_update_student( 58 | In in_no char(6), 59 | in in_kor int, 60 | in in_eng int, 61 | in in_math int 62 | ) 63 | BEGIN 64 | DECLARE in_total INT; 65 | DECLARE in_avr DOUBLE; 66 | DECLARE in_grade VARCHAR(2); 67 | SET in_total = in_kor + in_eng + in_math; 68 | SET in_avr = (in_total / 3.0); 69 | SET in_grade = 70 | CASE 71 | WHEN in_avr >= 90.0 THEN 'A' 72 | WHEN in_avr >= 80.0 THEN 'B' 73 | WHEN in_avr >= 70.0 THEN 'C' 74 | WHEN in_avr >= 60.0 THEN 'D' 75 | ELSE 'F' 76 | END; 77 | update student set kor = in_kor, eng = in_eng, math = in_math, total = in_total, avr = in_avr, grade = in_grade where no = in_no; 78 | END$$ 79 | DELIMITER ; 80 | 81 | 82 | use studentdb; 83 | 84 | create table deletestudent( 85 | no char(6) not null, 86 | name varchar(10) not null, 87 | kor tinyint not null, 88 | eng tinyint not null, 89 | math tinyint not null, 90 | total smallint null, 91 | avr double null, 92 | grade varchar(2) null, 93 | rate int null, 94 | deletedate datetime 95 | ); 96 | 97 | create table updatestudent( 98 | no char(6) not null, 99 | name varchar(10) not null, 100 | kor tinyint not null, 101 | eng tinyint not null, 102 | math tinyint not null, 103 | total smallint null, 104 | avr double null, 105 | grade varchar(2) null, 106 | rate int null, 107 | updatedate datetime 108 | ); 109 | describe student; 110 | 111 | delimiter !! 112 | create trigger trg_deleteStudent 113 | after delete 114 | on student 115 | for each row 116 | begin 117 | INSERT INTO deletestudent VALUES 118 | (old.no, old.name, old.kor, old.eng, old.math, old.total, 119 | old.avr,old.grade,old.rate,now()); 120 | 121 | end !! 122 | delimiter ; 123 | 124 | select * from student; 125 | select * from deletestudent; 126 | 127 | 128 | delimiter !! 129 | create trigger trg_updateStudent 130 | after update 131 | on student 132 | for each row 133 | begin 134 | INSERT INTO updatestudent VALUES 135 | (old.no, old.name, old.kor, old.eng, old.math, old.total, 136 | old.avr,old.grade,old.rate,now()); 137 | end !! 138 | delimiter ; 139 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # 학생 성적 관리 프로그램 3 | 4 | ## 개 요 5 | 6 | Java와 MySQL을 활용한 성적 관리 프로그램 제작 7 | 8 | ## 개발환경 9 | 10 | | 구 분 | 내 용 | 11 | | --- | --- | 12 | | OS | Windows 11 Home | 13 | | Language | Java 17.0.4 | 14 | | Editor | Eclipse 2022-06 | 15 | | DBMS | MySQL Workbench 8.0 | 16 | | Github | https://github.com/seopit95/Alignment-of-student-performance-programs-Java-Project.git | 17 | 18 | 19 | 20 | ![프로그램구조도](https://user-images.githubusercontent.com/115531849/195788749-4afc2685-fad8-40c7-a0cc-f17f47e0c832.png) 21 | 22 | ![학생정보입력](https://user-images.githubusercontent.com/115531849/195788764-bc7ad041-3a3b-40f3-a0b5-d534cd72f50c.png) 23 | 24 | ![학생정보검색](https://user-images.githubusercontent.com/115531849/195789177-2ef9841b-9094-4418-9ea0-884b28241d75.png) 25 | 26 | ![학생정보수정](https://user-images.githubusercontent.com/115531849/195794069-bdb25d87-0699-4bb5-899e-6b4bdd0599a9.png) 27 | 28 | ![학생정보삭제](https://user-images.githubusercontent.com/115531849/195794083-213ff63f-ba55-48af-a316-522d654c99fb.png) 29 | 30 | ![학생정렬통계](https://user-images.githubusercontent.com/115531849/195794102-d8aa2319-f436-4dcf-b94a-0c20aacc20cc.png) 31 | -------------------------------------------------------------------------------- /src/DBConnection: -------------------------------------------------------------------------------- 1 | package studentProject; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.FileNotFoundException; 5 | import java.io.IOException; 6 | import java.sql.Connection; 7 | import java.sql.DriverManager; 8 | import java.sql.PreparedStatement; 9 | import java.sql.ResultSet; 10 | import java.sql.SQLException; 11 | import java.sql.Statement; 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | import java.util.Properties; 15 | 16 | public class DBConnection { 17 | private Connection connection = null; 18 | private Statement statement = null; 19 | private ResultSet rs = null; 20 | 21 | public void connect() { 22 | Properties properties = new Properties(); 23 | 24 | try { 25 | FileInputStream fis = new FileInputStream("C:\\thisisjava\\thisisjava\\src\\studentProject\\db.properties"); 26 | properties.load(fis); 27 | } catch (FileNotFoundException e) { 28 | System.out.println("FileInputStream error" + e.getMessage()); 29 | } catch (IOException e) { 30 | System.out.println("Properties error" + e.getMessage()); 31 | } 32 | 33 | try { 34 | Class.forName(properties.getProperty("driver")); 35 | connection = DriverManager.getConnection(properties.getProperty("url"), properties.getProperty("userid"), 36 | properties.getProperty("password")); 37 | } catch (ClassNotFoundException e) { 38 | System.out.println("Class.forname load error" + e.getMessage()); 39 | } catch (SQLException e) { 40 | System.out.println("connection error" + e.getMessage()); 41 | } 42 | } 43 | 44 | public int insert(Student student) { 45 | PreparedStatement ps = null; 46 | int insertReturnValue = -1; 47 | String insertQuery = "call procedure_insert_student (?,?,?,?,?)"; 48 | 49 | try { 50 | ps = connection.prepareStatement(insertQuery); 51 | ps.setString(1, student.getNo()); 52 | ps.setString(2, student.getName()); 53 | ps.setInt(3, student.getKor()); 54 | ps.setInt(4, student.getEng()); 55 | ps.setInt(5, student.getMath()); 56 | insertReturnValue = ps.executeUpdate(); 57 | } catch (SQLException e) { 58 | System.out.println("inserReturnValue error" + e.getMessage()); 59 | } catch (Exception e) { 60 | System.out.println("오류발생" + e.getMessage()); 61 | } finally { 62 | try { 63 | if (ps != null) { 64 | ps.close(); 65 | } 66 | } catch (SQLException e) { 67 | System.out.println("preparedStatement or ResultSeterror" + e.getMessage()); 68 | } 69 | } 70 | return insertReturnValue; 71 | } 72 | 73 | public List select() { 74 | List list = new ArrayList(); 75 | PreparedStatement ps = null; 76 | ResultSet rs = null; 77 | String selectQuery = "select * from student"; 78 | try { 79 | ps = connection.prepareStatement(selectQuery); 80 | rs = ps.executeQuery(selectQuery); 81 | if (!(rs != null || rs.isBeforeFirst())) { 82 | return list; 83 | } 84 | 85 | while (rs.next()) { 86 | String no = rs.getString("no"); 87 | String name = rs.getString("name"); 88 | int kor = rs.getInt("kor"); 89 | int eng = rs.getInt("eng"); 90 | int math = rs.getInt("math"); 91 | int total = rs.getInt("total"); 92 | double avr = rs.getInt("avr"); 93 | String grade = rs.getString("grade"); 94 | int rate = rs.getInt("rate"); 95 | list.add(new Student(no, name, kor, eng, math, total, avr, grade, rate)); 96 | } 97 | } catch (Exception e) { 98 | System.out.println("select Error" + e.getMessage()); 99 | } finally { 100 | try { 101 | if (ps != null) { 102 | ps.close(); 103 | } 104 | } catch (SQLException e) { 105 | System.out.println("PreparedStatement close Error" + e.getMessage()); 106 | } 107 | } 108 | return list; 109 | } 110 | 111 | public List selectSearch(String data, int type) { 112 | List list = new ArrayList(); 113 | PreparedStatement ps = null; 114 | ResultSet rs = null; 115 | String selectSearchQuery = "select * from student where "; 116 | try { 117 | switch (type) { 118 | case 1: 119 | selectSearchQuery += "no like ?"; 120 | break; 121 | case 2: 122 | selectSearchQuery += "name like ?"; 123 | break; 124 | default: 125 | System.out.println("잘못된 입력타입"); 126 | return list; 127 | } 128 | 129 | ps = connection.prepareStatement(selectSearchQuery); 130 | ps.setString(1, "%" + data + "%"); 131 | rs = ps.executeQuery(); 132 | 133 | if (!(rs != null || rs.isBeforeFirst())) { 134 | return list; 135 | } 136 | 137 | while (rs.next()) { 138 | String no = rs.getString("no"); 139 | String name = rs.getString("name"); 140 | int kor = rs.getInt("kor"); 141 | int eng = rs.getInt("eng"); 142 | int math = rs.getInt("math"); 143 | int total = rs.getInt("total"); 144 | double avr = rs.getDouble("avr"); 145 | String grade = rs.getString("grade"); 146 | int rate = rs.getInt("rate"); 147 | list.add(new Student(no, name, kor, eng, math, total, avr, grade, rate)); 148 | } 149 | } catch (Exception e) { 150 | System.out.println("selectSearch Error" + e.getMessage()); 151 | } finally { 152 | try { 153 | if (ps != null) { 154 | ps.close(); 155 | } 156 | } catch (SQLException e) { 157 | System.out.println("PreparedStatement close Error" + e.getMessage()); 158 | } 159 | } 160 | return list; 161 | } 162 | 163 | public int update(Student student) { 164 | PreparedStatement ps = null; 165 | int UpdateReturnValue = -1; 166 | String updateQuery = "call procedure_update_student(?,?,?,?)"; 167 | 168 | try { 169 | ps = connection.prepareStatement(updateQuery); 170 | ps.setString(1, student.getNo()); 171 | ps.setInt(2, student.getKor()); 172 | ps.setInt(3, student.getEng()); 173 | ps.setInt(4, student.getMath()); 174 | UpdateReturnValue = ps.executeUpdate(); 175 | } catch (Exception e) { 176 | System.out.println("update 오류발생" + e.getMessage()); 177 | } finally { 178 | try { 179 | if (ps != null) { 180 | ps.close(); 181 | } 182 | } catch (SQLException e) { 183 | System.out.println("PrepareStatement Error" + e.getMessage()); 184 | } 185 | } 186 | return UpdateReturnValue; 187 | } 188 | 189 | public int delete(String no) { 190 | PreparedStatement ps = null; 191 | int deleteReturnValue = -1; 192 | String deleteQuery = "delete from student where no = ?"; 193 | try { 194 | ps = connection.prepareStatement(deleteQuery); 195 | ps.setString(1, no); 196 | deleteReturnValue = ps.executeUpdate(); 197 | } catch (Exception e) { 198 | System.out.println("오류발생" + e.getStackTrace()); 199 | } finally { 200 | 201 | try { 202 | if (ps != null) { 203 | ps.close(); 204 | } 205 | } catch (SQLException e) { 206 | System.out.println("PrepareStatement or ResultSet Close Error" + e.getMessage()); 207 | } 208 | } 209 | return deleteReturnValue; 210 | } 211 | 212 | public List selectOrderBy(int type) { 213 | List list = new ArrayList(); 214 | PreparedStatement ps = null; 215 | ResultSet rs = null; 216 | String selectOrderByQuery = "select * from student order by "; 217 | try { 218 | switch (type) { 219 | case 1: 220 | selectOrderByQuery += "no asc"; 221 | break; 222 | case 2: 223 | selectOrderByQuery += "name asc"; 224 | break; 225 | case 3: 226 | selectOrderByQuery += "total desc"; 227 | break; 228 | default: 229 | System.out.println("정렬 타입 오류"); 230 | return list; 231 | } 232 | 233 | ps = connection.prepareStatement(selectOrderByQuery); 234 | rs = ps.executeQuery(selectOrderByQuery); 235 | if (!(rs != null || rs.isBeforeFirst())) { 236 | return list; 237 | } 238 | 239 | int rank = 0; 240 | while (rs.next()) { 241 | String no = rs.getString("no"); 242 | String name = rs.getString("name"); 243 | int kor = rs.getInt("kor"); 244 | int eng = rs.getInt("eng"); 245 | int math = rs.getInt("math"); 246 | int total = rs.getInt("total"); 247 | double avr = rs.getDouble("avr"); 248 | String grade = rs.getString("grade"); 249 | int rate = rs.getInt("rate"); 250 | if (type == 3) { 251 | rate = ++rank; 252 | } 253 | list.add(new Student(no, name, kor, eng, math, total, avr, grade, rate)); 254 | } 255 | } catch (Exception e) { 256 | System.out.println("OrderBy Error" + e.getMessage()); 257 | } finally { 258 | try { 259 | if (ps != null) { 260 | ps.close(); 261 | } 262 | } catch (SQLException e) { 263 | System.out.println("PreparedStatement Close Error" + e.getMessage()); 264 | } 265 | } 266 | return list; 267 | } 268 | 269 | public List selectMaxMin(int type) { 270 | List list = new ArrayList(); 271 | Statement statement = null; 272 | ResultSet rs = null; 273 | 274 | String selectMaxMinQuery = "select * from student where total = "; 275 | try { 276 | 277 | switch (type) { 278 | case 1: 279 | selectMaxMinQuery += "(select max(total) from student)"; 280 | break; 281 | case 2: 282 | selectMaxMinQuery += "(select min(total) from student)"; 283 | break; 284 | default: 285 | System.out.println("통계 타입 오류"); 286 | return list; 287 | } 288 | statement = connection.createStatement(); 289 | rs = statement.executeQuery(selectMaxMinQuery); 290 | 291 | if (!(rs != null || rs.isBeforeFirst())) { 292 | return list; 293 | } 294 | int rank = 1; 295 | 296 | while (rs.next()) { 297 | String no = rs.getString("no"); 298 | String name = rs.getString("name"); 299 | int kor = rs.getInt("kor"); 300 | int eng = rs.getInt("eng"); 301 | int math = rs.getInt("math"); 302 | int total = rs.getInt("total"); 303 | double avr = rs.getDouble("avr"); 304 | String grade = rs.getString("grade"); 305 | int rate = rs.getInt("rate"); 306 | if (type == 1) { 307 | rate = rank; 308 | } 309 | list.add(new Student(no, name, kor, eng, math, total, avr, grade, rate)); 310 | } 311 | } catch (Exception e) { 312 | System.out.println("select sort error " + e.getMessage()); 313 | } finally { 314 | try { 315 | if (statement != null) { 316 | statement.close(); 317 | } 318 | } catch (SQLException e) { 319 | System.out.println("PreparedStatement close error " + e.getMessage()); 320 | } 321 | } 322 | return list; 323 | } 324 | public void close() { 325 | try { 326 | if (connection != null) { 327 | connection.close(); 328 | } 329 | } catch (SQLException e) { 330 | System.out.println("Statement or ResultSet Close Error" + e.getMessage()); 331 | } 332 | } 333 | } 334 | -------------------------------------------------------------------------------- /src/Main.java: -------------------------------------------------------------------------------- 1 | package studentProject; 2 | 3 | import java.util.ArrayList; 4 | import java.util.InputMismatchException; 5 | import java.util.List; 6 | import java.util.Scanner; 7 | import java.util.regex.Pattern; 8 | 9 | public class Main { 10 | public static final int INPUT = 1, UPDATE = 2, DELETE = 3, SEARCH = 4; 11 | public static final int OUTPUT = 5, SORT = 6, STATS = 7, EXIT = 8; 12 | public static Scanner sc = new Scanner(System.in); 13 | 14 | public static void main(String[] agrs) { 15 | List list = new ArrayList(); 16 | 17 | boolean loopflag = false; 18 | 19 | while (!loopflag) { 20 | int num = displayMenu(); 21 | switch (num) { 22 | case INPUT: 23 | studentInputData(); 24 | break; 25 | case UPDATE: 26 | studentUpDate(); 27 | break; 28 | case DELETE: 29 | studentDelete(); 30 | break; 31 | case SEARCH: 32 | studentSearch(); 33 | break; 34 | case OUTPUT: 35 | studentOutput(); 36 | break; 37 | case SORT: 38 | studentSort(); 39 | break; 40 | case STATS: 41 | studentStats(); 42 | break; 43 | case EXIT: 44 | System.out.println("프로그램 종료"); 45 | return; 46 | default: 47 | System.out.println("1~8번 중에 입력해주세요."); 48 | return; 49 | } 50 | } 51 | } 52 | 53 | private static void studentStats() { 54 | List list = new ArrayList(); 55 | try { 56 | DBConnection dbConnection = new DBConnection(); 57 | dbConnection.connect(); 58 | 59 | System.out.print("통계방식선택 (1.최고점수 2.최저점수) >>"); 60 | int type = sc.nextInt(); 61 | //번호 패턴 62 | boolean value = checkInputPattern(String.valueOf(type), 6); 63 | if(!value) { 64 | return; 65 | } 66 | 67 | list = dbConnection.selectMaxMin(type); 68 | 69 | if (list.size() <= 0) { 70 | System.out.println("보여줄 리스트가 없습니다."); 71 | return; 72 | } 73 | 74 | System.out.println("학번\t이름\t국어점수\t영어점수\t수학점수\t총점\t평균\t등급\t순위"); 75 | for (Student student : list) { 76 | System.out.println(student); 77 | } 78 | dbConnection.close(); 79 | }catch (InputMismatchException e) { 80 | System.out.println("올바르지 않은 입력입니다. 다시 입력해주세요. " + e.getMessage()); 81 | return; 82 | }catch (Exception e) { 83 | System.out.println("데이터베이스 통계 에러. " + e.getMessage()); 84 | } 85 | } 86 | 87 | private static void studentSort() { 88 | List list = new ArrayList(); 89 | try { 90 | DBConnection dbConnection = new DBConnection(); 91 | dbConnection.connect(); 92 | 93 | System.out.print("정렬방식선택 (1.학번순 2.이름순 3.총점순) >>"); 94 | int type = sc.nextInt(); 95 | //번호 패턴 96 | boolean value = checkInputPattern(String.valueOf(type), 5); 97 | if(!value) { 98 | return; 99 | } 100 | 101 | list = dbConnection.selectOrderBy(type); 102 | 103 | if (list.size() <= 0) { 104 | System.out.println("보여줄 리스트가 없습니다."); 105 | return; 106 | } 107 | 108 | System.out.println("학번\t이름\t국어점수\t영어점수\t수학점수\t총점\t평균\t등급\t순위"); 109 | for (Student student : list) { 110 | System.out.println(student); 111 | } 112 | dbConnection.close(); 113 | }catch (Exception e) { 114 | System.out.println("데이터베이스 정렬 에러. " + e.getMessage()); 115 | } 116 | return; 117 | 118 | } 119 | 120 | private static void studentDelete() { 121 | try { 122 | System.out.print("삭제할 학생번호 입력요망 >>"); 123 | String no = sc.nextLine(); 124 | 125 | boolean value = checkInputPattern(no, 2); 126 | if (!value) { 127 | return; 128 | } 129 | 130 | DBConnection dbConnection = new DBConnection(); 131 | dbConnection.connect(); 132 | 133 | int deleteReturnValue = dbConnection.delete(no); 134 | if (deleteReturnValue == -1) { 135 | System.out.println("삭제실패입니다. " + deleteReturnValue); 136 | } else if (deleteReturnValue == 0) { 137 | System.out.println("삭제할 번호가 존재하지 않습니다. " + deleteReturnValue); 138 | } else { 139 | System.out.println("삭제성공입니다."); 140 | } 141 | dbConnection.close(); 142 | } catch (InputMismatchException e) { 143 | System.out.println("올바르지 않은 입력입니다. 다시 입력해주세요"); 144 | return; 145 | } catch (Exception e) { 146 | System.out.println("데이터베이스 삭제 에러. " + e.getStackTrace()); 147 | } 148 | } 149 | 150 | private static void studentSearch() { 151 | List list = new ArrayList(); 152 | try { 153 | System.out.print("검색할 학생이름 입력 >>"); 154 | String name = sc.nextLine(); 155 | boolean value = checkInputPattern(name, 3); 156 | if (!value) { 157 | return; 158 | } 159 | 160 | DBConnection dbConnection = new DBConnection(); 161 | dbConnection.connect(); 162 | 163 | list = dbConnection.selectSearch(name, 2); 164 | if (list.size() <= 0) { 165 | System.out.println("검색할 리스트가 없습니다"); 166 | } 167 | 168 | System.out.println("학번\t이름\t국어점수\t영어점수\t수학점수\t총점\t평균\t등급\t순위"); 169 | for (Student student : list) { 170 | System.out.println(student); 171 | } 172 | dbConnection.close(); 173 | } catch (InputMismatchException e) { 174 | System.out.println("올바르지 않은 입력입니다. 다시 입력해주세요"); 175 | return; 176 | } catch (Exception e) { 177 | System.out.println("데이터베이스 검색 에러. " + e.getMessage()); 178 | } 179 | } 180 | 181 | private static void studentUpDate() { 182 | List list = new ArrayList(); 183 | System.out.print("수정할 학생번호 입력 >>"); 184 | String no = sc.nextLine(); 185 | // 패턴분석 186 | boolean value = checkInputPattern(no, 2); 187 | if (!value) { 188 | return; 189 | } 190 | 191 | DBConnection dbConnection = new DBConnection(); 192 | dbConnection.connect(); 193 | 194 | list = dbConnection.selectSearch(no, 1); 195 | if (list.size() <= 0) { 196 | System.out.println("수정할 학생정보가 없습니다" + list.size()); 197 | return; 198 | } 199 | System.out.println("학번\t이름\t국어점수\t영어점수\t수학점수\t총점\t평균\t등급\t순위"); 200 | for (Student student : list) { 201 | System.out.println(student); 202 | } 203 | 204 | Student savedStudent = list.get(0); 205 | System.out.println("국어" + savedStudent.getKor() + ">>"); 206 | int kor = sc.nextInt(); 207 | value = checkInputPattern(String.valueOf(kor), 4); 208 | if (!value) { 209 | return; 210 | } 211 | savedStudent.setKor(kor); 212 | 213 | System.out.println("영어" + savedStudent.getEng() + ">>"); 214 | int eng = sc.nextInt(); 215 | value = checkInputPattern(String.valueOf(eng), 4); 216 | if (!value) { 217 | return; 218 | } 219 | savedStudent.setEng(eng); 220 | 221 | System.out.println("수학" + savedStudent.getMath() + ">>"); 222 | int math = sc.nextInt(); 223 | value = checkInputPattern(String.valueOf(math), 4); 224 | if (!value) { 225 | return; 226 | } 227 | savedStudent.setMath(math); 228 | 229 | int returnUpdateValue = dbConnection.update(savedStudent); 230 | if (returnUpdateValue == -1) { 231 | System.out.println("학생정보 수정실패"); 232 | return; 233 | } 234 | System.out.println("학생수정 완료"); 235 | 236 | dbConnection.close(); 237 | } 238 | 239 | private static void studentOutput() { 240 | List list = new ArrayList(); 241 | 242 | try { 243 | DBConnection dbConnection = new DBConnection(); 244 | dbConnection.connect(); 245 | 246 | list = dbConnection.select(); 247 | if (list.size() <= 0) { 248 | System.out.println("보여줄 리스트가 없습니다."); 249 | } 250 | 251 | System.out.println("학번\t이름\t국어점수\t영어점수\t수학점수\t총점\t평균\t등급\t순위"); 252 | for (Student student : list) { 253 | System.out.println(student); 254 | } 255 | dbConnection.close(); 256 | } catch (Exception e) { 257 | System.out.println("데이터베이스 출력 에러. " + e.getMessage()); 258 | } 259 | } 260 | 261 | public static void studentInputData() { 262 | String pattern = null; 263 | boolean regex = false; 264 | 265 | try { 266 | System.out.print("학년(01~03)반(01~09)번호(01~60) >>"); 267 | String no = sc.nextLine(); 268 | boolean value = checkInputPattern(no, 2); 269 | if (!value) { 270 | return; 271 | } 272 | 273 | System.out.print("학생이름 입력요망 >>"); 274 | String name = sc.nextLine(); 275 | value = checkInputPattern(name, 3); 276 | if (!value) { 277 | return; 278 | } 279 | 280 | System.out.print("국어점수 입력요망 >>"); 281 | int kor = sc.nextInt(); 282 | value = checkInputPattern(String.valueOf(kor), 4); 283 | if (!value) { 284 | return; 285 | } 286 | 287 | System.out.print("영어점수 입력요망 >>"); 288 | int eng = sc.nextInt(); 289 | value = checkInputPattern(String.valueOf(eng), 4); 290 | if (!value) { 291 | return; 292 | } 293 | 294 | System.out.print("수학점수 입력요망 >>"); 295 | int math = sc.nextInt(); 296 | value = checkInputPattern(String.valueOf(math), 4); 297 | if (!value) { 298 | return; 299 | } 300 | 301 | Student student = new Student(no, name, kor, eng, math); 302 | DBConnection dbConnection = new DBConnection(); 303 | dbConnection.connect(); 304 | 305 | int insertReturnValue = dbConnection.insert(student); 306 | if (insertReturnValue == -1) { 307 | System.out.println("삽입 실패입니다."); 308 | } else { 309 | System.out.println("삽입 성공입니다."); 310 | } 311 | dbConnection.close(); 312 | } catch (InputMismatchException e) { 313 | System.out.println("올바르지 않은 입력입니다. 다시 입력해주세요."); 314 | } finally { 315 | sc.nextLine(); 316 | } 317 | } 318 | 319 | private static int displayMenu() { 320 | int num = -1; 321 | try { 322 | System.out.print("1.입력 2.수정 3.삭제 4.검색 5.출력 6.정렬 7.통계 8.종료\n>>"); 323 | num = sc.nextInt(); 324 | // 정수패턴 검색 325 | boolean value = checkInputPattern(String.valueOf(num), 1); 326 | if (!value) { 327 | return num - 1; 328 | } 329 | } catch (Exception e) { 330 | System.out.println("displayMenu error" + e.getMessage()); 331 | } finally { 332 | sc.nextLine(); 333 | } 334 | return num; 335 | } 336 | 337 | private static boolean checkInputPattern(String data, int patternType) { 338 | 339 | String pattern = null; 340 | boolean regex = false; 341 | String message = null; 342 | switch (patternType) { 343 | case 1: 344 | pattern = "^[1-9]$"; 345 | message = "번호 재입력(1~9)"; 346 | break; 347 | case 2: 348 | pattern = "^0[1-3]0[1-9][0-6][0-9]$"; 349 | message = "학번 재입력(ex010101)"; 350 | break; 351 | case 3: 352 | pattern = "^[가-힣]{3,5}$"; 353 | message = "이름 재입력(5자 이내)"; 354 | break; 355 | case 4: 356 | pattern = "^[0-9]{1,2}|100$"; 357 | message = "점수 재입력(0~100)"; 358 | break; 359 | case 5: 360 | pattern = "^[1-3]$"; 361 | message = "정렬타입 재입력(1~9)"; 362 | break; 363 | case 6: 364 | pattern = "^[1-2]$"; 365 | message = "통계타입 재입력(1~9)"; 366 | break; 367 | } 368 | 369 | regex = Pattern.matches(pattern, data); 370 | if (!regex) { 371 | System.out.println(message); 372 | return false; 373 | } 374 | return regex; 375 | } 376 | } 377 | -------------------------------------------------------------------------------- /src/Student.java: -------------------------------------------------------------------------------- 1 | package studentProject; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Student implements Comparable, Serializable{ 6 | private static final int SUBJECT_NO = 3; 7 | private String no; 8 | private String name; 9 | private int kor; 10 | private int eng; 11 | private int math; 12 | private int total; 13 | private double avr; 14 | private String grade; 15 | private int rate; 16 | 17 | public Student(String no, String name, int kor, int eng, int math){ 18 | this(no,name,kor,eng,math,0,0.0,null,0); 19 | } 20 | 21 | public Student(String no, String name, int kor, int eng, int math, int total, double avr, String grade, int rate) { 22 | super(); 23 | this.no = no; 24 | this.name = name; 25 | this.kor = kor; 26 | this.eng = eng; 27 | this.math = math; 28 | this.total = total; 29 | this.avr = avr; 30 | this.grade = grade; 31 | this.rate = rate; 32 | } 33 | 34 | public String getNo() { 35 | return no; 36 | } 37 | 38 | public void setNo(String no) { 39 | this.no = no; 40 | } 41 | 42 | public String getName() { 43 | return name; 44 | } 45 | 46 | public void setName(String name) { 47 | this.name = name; 48 | } 49 | 50 | public int getKor() { 51 | return kor; 52 | } 53 | 54 | public void setKor(int kor) { 55 | this.kor = kor; 56 | } 57 | 58 | public int getEng() { 59 | return eng; 60 | } 61 | 62 | public void setEng(int eng) { 63 | this.eng = eng; 64 | } 65 | 66 | public int getMath() { 67 | return math; 68 | } 69 | 70 | public void setMath(int math) { 71 | this.math = math; 72 | } 73 | 74 | public int getTotal() { 75 | return total; 76 | } 77 | 78 | public void setTotal(int total) { 79 | this.total = total; 80 | } 81 | 82 | public double getAvr() { 83 | return avr; 84 | } 85 | 86 | public void setAvr(double avr) { 87 | this.avr = avr; 88 | } 89 | 90 | public String getGrade() { 91 | return grade; 92 | } 93 | 94 | public void setGrade(String grade) { 95 | this.grade = grade; 96 | } 97 | 98 | public int getRate() { 99 | return rate; 100 | } 101 | 102 | public void setRate(int rate) { 103 | this.rate = rate; 104 | } 105 | 106 | @Override 107 | public int hashCode() { 108 | return this.no.hashCode(); 109 | } 110 | 111 | @Override 112 | public boolean equals(Object obj) { 113 | if(!(obj instanceof Student)) return false; 114 | return this.no.equalsIgnoreCase(((Student)obj).no); 115 | } 116 | 117 | @Override 118 | public int compareTo(Student student) { 119 | return this.no.compareToIgnoreCase(student.no); 120 | } 121 | 122 | @Override 123 | public String toString() { 124 | return no + "\t" + name + "\t" + kor + "\t" + eng + "\t" + math + "\t" 125 | + total + "\t" + avr + "\t" + grade + "\t" + rate; 126 | } 127 | } 128 | --------------------------------------------------------------------------------