├── .classpath ├── .gitignore ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── README.md └── src ├── Main.java └── com └── student ├── AppConstants.java ├── base └── BaseDAO.java ├── dao ├── AdminDAO.java └── StudentDAO.java ├── model ├── Course.java └── Student.java ├── util └── DBUtil.java └── view ├── AdminView.java ├── CourseInfo.java ├── LoginView.java ├── StudentInfo.java └── StudentView.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | .vscode/ -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Student-Course-Selection 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.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=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 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.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 学生选课成绩管理系统 2 | > Toy Project of DataBase Course 3 | 4 | ## 包结构 5 | > AppConstants.java 6 | > - 所有用到的常量 7 | 8 | > DAO.java 9 | > - 学生/管理员的枚举类型 10 | 11 | ### base 12 | > Base.java 13 | > - 数据访问基类 14 | 15 | ### dao 16 | > AdminDAO.java 17 | > - 管理员数据访问类 18 | 19 | > StudentDAO.java 20 | > - 学生数据访问类 21 | 22 | ### model 23 | > Student.java 24 | > - 学生实体类 25 | 26 | > Course.java 27 | > - 课程实体类 28 | 29 | ### util 30 | > DBUtil.java 31 | > - 数据库连接查询工具集 32 | 33 | ### view 34 | > LoginView.java 35 | > - 登录界面 36 | > - LoginListener 37 | > - 监听登录事件 38 | 39 | > StudentView.java 40 | > - 学生选课界面 41 | > - SelectListener 42 | > - 监听选课事件 43 | > - DropListener 44 | > - 监听退课事件 45 | 46 | > AdminView.java 47 | > - 管理员界面 48 | > - InputListener 49 | > - 监听登分事件 50 | 51 | > StudentInfo.java 52 | > - 学生管理界面 53 | > - AddStudent 54 | > - 添加课程子窗口 55 | > - DelStudent 56 | > - 删除课程子窗口 57 | 58 | > CourseInfo.java 59 | > - 课程管理界面 60 | > - AddCourse 61 | > - 添加课程子窗口 62 | > - DelCourse 63 | > - 删除课程子窗口 64 | 65 | ## 数据库结构 66 | ### student 表 67 | ``` 68 | +----------+-------------------+------+-----+---------+-------+ 69 | | Field | Type | Null | Key | Default | Extra | 70 | +----------+-------------------+------+-----+---------+-------+ 71 | | sno | char(4) | NO | PRI | NULL | | 72 | | sname | char(8) | NO | | NULL | | 73 | | sex | enum('男','女') | YES | | NULL | | 74 | | age | int(11) | YES | | NULL | | 75 | | sdept | char(10) | YES | | NULL | | 76 | | username | char(20) | NO | UNI | NULL | | 77 | | password | char(64) | NO | | NULL | | 78 | +----------+-------------------+------+-----+---------+-------+ 79 | ``` 80 | 81 | ### course 表 82 | ``` 83 | +--------+----------+------+-----+---------+-------+ 84 | | Field | Type | Null | Key | Default | Extra | 85 | +--------+----------+------+-----+---------+-------+ 86 | | cno | char(4) | NO | PRI | NULL | | 87 | | cname | char(20) | NO | | NULL | | 88 | | credit | int(11) | YES | | 0 | | 89 | | cdept | char(10) | YES | | NULL | | 90 | | tname | char(8) | YES | | NULL | | 91 | +--------+----------+------+-----+---------+-------+ 92 | ``` 93 | 94 | ### stu_course 表 95 | ``` 96 | +-------+---------+------+-----+---------+-------+ 97 | | Field | Type | Null | Key | Default | Extra | 98 | +-------+---------+------+-----+---------+-------+ 99 | | sno | char(4) | NO | PRI | NULL | | 100 | | cno | char(4) | NO | PRI | NULL | | 101 | | grade | int(11) | YES | | NULL | | 102 | +-------+---------+------+-----+---------+-------+ 103 | ``` 104 | 105 | ## 注意事项 106 | - 测试学生账号 107 | - 账号:demo 108 | - 密码:test1234 109 | - 测试管理账号 110 | - 账号:admin 111 | - 密码:admin1234 112 | -------------------------------------------------------------------------------- /src/Main.java: -------------------------------------------------------------------------------- 1 | import com.student.util.DBUtil; 2 | import com.student.view.LoginView; 3 | 4 | /** 5 | * @Description: Main 6 | * @ClassName: Main 7 | * 8 | */ 9 | public class Main { 10 | 11 | public static void main(String[] args) { 12 | new LoginView(); 13 | DBUtil.getDBUtil().close(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/com/student/AppConstants.java: -------------------------------------------------------------------------------- 1 | package com.student; 2 | 3 | /** 4 | * @Description: Constants 5 | * @ClassName: AppConstants 6 | * 7 | */ 8 | public class AppConstants { 9 | 10 | // JDBC 11 | public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; 12 | public static final String JDBC_URL = "jdbc:mysql://119.29.176.110:3306/school"; 13 | public static final String JDBC_USERNAME = "db1"; 14 | public static final String JDBC_PASSWORD = "shuacm"; 15 | 16 | // Login 17 | public static final String LOGIN_TITLE = "用户登录"; 18 | public static final String LOGIN = "登录"; 19 | public static final String EXIT = "退出"; 20 | public static final String LOGIN_USERNAME = "用户名:"; 21 | public static final String LOGIN_PASSWORD = "密 码:"; 22 | public static final String LOGIN_ERROR = "用户名或密码错误"; 23 | public static final String REGEX_PASSWORD = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$"; 24 | public static final String REGEX_USERNAME = "^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]{4,16}$"; 25 | 26 | // Student 27 | public static final String STUDENT_TITLE = "学生选课"; 28 | public static final String STUDENT_SELECT = "选课"; 29 | public static final String STUDENT_DROP = "退课"; 30 | public static final String STUDENT_CLOSE = "关闭"; 31 | public static final String STUDENT_INPUT = "请输入课程号"; 32 | 33 | public static final String STUDENT_INFO = "学生详细信息"; 34 | public static final String STUDENT_COURSE = "可选课程"; 35 | public static final String STUDENT_SCORE = "已修课程成绩"; 36 | public static final String STUDENT_SELECTED = "已选课程"; 37 | 38 | public static final String SNO = "学号"; 39 | public static final String SNAME = "姓名"; 40 | public static final String SEX = "性别"; 41 | public static final String AGE = "年龄"; 42 | public static final String SDEPT = "所在院系"; 43 | public static final String USERNAME = "用户名"; 44 | public static final String PASSWORD = "密码"; 45 | 46 | public static final String CNO = "课程号"; 47 | public static final String CNAME = "课程名"; 48 | public static final String CREDIT = "学分"; 49 | public static final String CDEPT = "开课院系"; 50 | public static final String TNAME = "任课老师"; 51 | 52 | public static final String SCORE = "成绩"; 53 | 54 | public static final String ERROR = "错误"; 55 | public static final String CNO_NULL_ERROR = "请输入课程号"; 56 | public static final String CNO_NOT_EXIST_ERROR = "课程号不存在!"; 57 | public static final String CNO_SELECTED_ERROR = "此课程已选!"; 58 | public static final String CNO_NOT_SELECTED_ERROR = "此课程未选!"; 59 | public static final String CNO_GRADED_ERROR = "此课程已登分!"; 60 | 61 | public static final String VERIFY = "确认"; 62 | public static final String DELETE = "删除"; 63 | public static final String TOTAL_COUNT = "记录总数:"; 64 | 65 | // Admin 66 | public static final String ADMIN_TITLE = "成绩管理"; 67 | public static final String ADMIN_QUERY = "查询"; 68 | public static final String ADMIN_INPUT = "登分"; 69 | public static final String ADMIN_SAVE = "保存"; 70 | public static final String ADMIN_CLOSE = "关闭"; 71 | public static final String ADMIN_CHOOSE = "请选择课程名:"; 72 | public static final String ADMIN_SELECTIONINFO = "选修此课程的学生:"; 73 | 74 | public static final String ADMIN_CNAME = "课程:"; 75 | public static final String ADMIN_TNAME = "任课教师:"; 76 | 77 | public static final String ADMIN_MAINTAIN = "维护"; 78 | public static final String ADMIN_COURSEINFO = "课程信息"; 79 | public static final String ADMIN_STUDENTINFO = "学生信息"; 80 | 81 | public static final String ADMIN_COURSEINFO_ADD = "新增课程"; 82 | public static final String ADMIN_COURSEINFO_DEL = "删除课程"; 83 | public static final String ADMIN_COURSEINFO_QUIT = "退出"; 84 | public static final String ADMIN_CNO_EXIST_ERROR = "不能添加,此课程号已存在!"; 85 | public static final String ADMIN_CNO_NOTEXIST_ERROR = "不能删除,此课程号不存在!"; 86 | public static final String ADMIN_COURSESELECTED_ERROR = "不能删除,此课程已有学生选!"; 87 | 88 | public static final String ADMIN_SUTDENTINFO = "学生信息"; 89 | public static final String ADMIN_SUTDENTINFO_ADD = "添加学生"; 90 | public static final String ADMIN_SUTDENTINFO_DEL = "删除学生"; 91 | public static final String ADMIN_SNO_EXIST_ERROR = "不能添加,此学号已存在!"; 92 | public static final String ADMIN_USER_EXIST_ERROR = "不能研究,此用戶已存在!"; 93 | public static final String ADMIN_SNO_NOTEXIST_ERROR = "不能删除,此学号不存在!"; 94 | public static final String ADMIN_SELECTEDCOURSE_ERROR = "不能刪除,此学生已选课!"; 95 | 96 | public static final String REGEX_SNO = "^[a-zA-Z0-9]{1,4}$"; 97 | public static final String REGEX_SNAME = "^(?!_)(?!.*?_$)[a-zA-Z0-9_\\u4e00-\\u9fa5]{1,8}$"; 98 | public static final String REGEX_SEX = "^(男|女)?$"; 99 | public static final String REGEX_AGE = "^(\\d)*"; 100 | public static final String REGEX_SDEPT = "^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]{0,10}$"; 101 | 102 | public static final String REGEX_CNO = "^[a-zA-Z0-9]{1,4}$"; 103 | public static final String REGEX_CNAME = "^(?!_)(?!.*?_$)[a-zA-Z0-9_\\u4e00-\\u9fa5]{1,8}$"; 104 | public static final String REGEX_CREDIT = "^(\\d)*"; 105 | public static final String REGEX_CDEPT = "^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]{0,10}$"; 106 | public static final String REGEX_TNAME = "^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]{0,8}$"; 107 | } 108 | -------------------------------------------------------------------------------- /src/com/student/base/BaseDAO.java: -------------------------------------------------------------------------------- 1 | package com.student.base; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.security.MessageDigest; 5 | import java.security.NoSuchAlgorithmException; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.util.Vector; 9 | import com.student.util.DBUtil; 10 | 11 | /** 12 | * @Description: Data Access Base Object 13 | * @ClassName: BaseDAO 14 | * 15 | */ 16 | public abstract class BaseDAO { 17 | 18 | protected final DBUtil db = DBUtil.getDBUtil(); 19 | 20 | protected ResultSet rs; 21 | 22 | protected BaseDAO() { 23 | 24 | } 25 | 26 | protected void destory() { 27 | try { 28 | if (rs != null) { 29 | rs.close(); 30 | } 31 | } catch (SQLException e) { 32 | e.printStackTrace(); 33 | } 34 | } 35 | 36 | /** 37 | * 38 | * @Description: buildResult build the query result to array. 39 | */ 40 | protected String[][] buildResult() { 41 | Vector table = new Vector(); 42 | int columcount = 0; 43 | try { 44 | columcount = rs.getMetaData().getColumnCount(); 45 | String[] data; 46 | while (rs.next()) { 47 | data = new String[columcount]; 48 | for (int i = 0; i < columcount; i++) { 49 | data[i] = rs.getString(i + 1); 50 | } 51 | table.add(data); 52 | } 53 | } catch (SQLException e) { 54 | e.printStackTrace(); 55 | } finally { 56 | destory(); 57 | } 58 | return table.toArray(new String[table.size()][columcount]); 59 | } 60 | 61 | /** 62 | * 63 | * @Description: query a student by sno. 64 | */ 65 | public String[][] queryStudent(String sno) { 66 | String sql = "select * from student where sno=?"; 67 | String[] param = {sno}; 68 | rs = db.executeQuery(sql, param); 69 | return buildResult(); 70 | } 71 | 72 | /** 73 | * 74 | * @Description: query a student by username. 75 | */ 76 | public String[][] queryUser(String sno) { 77 | String sql = "select * from student where username=?"; 78 | String[] param = {sno}; 79 | rs = db.executeQuery(sql, param); 80 | return buildResult(); 81 | } 82 | 83 | /** 84 | * 85 | * @Description: query a course by cno. 86 | */ 87 | public String[][] queryCourse(String cno) { 88 | String sql = "select * from course where cno=?"; 89 | String[] param = {cno}; 90 | rs = db.executeQuery(sql, param); 91 | return buildResult(); 92 | } 93 | 94 | /** 95 | * 96 | * @Description: encrypt the password with SHA256 97 | */ 98 | public String getSHA256(String password) { 99 | MessageDigest md; 100 | String ret = ""; 101 | try { 102 | md = MessageDigest.getInstance("SHA-256"); 103 | md.update(password.getBytes("UTF-8")); 104 | ret = byte2Hex(md.digest()); 105 | } catch (NoSuchAlgorithmException e) { 106 | e.printStackTrace(); 107 | } catch (UnsupportedEncodingException e) { 108 | e.printStackTrace(); 109 | } 110 | return ret; 111 | } 112 | 113 | /** 114 | * 115 | * @Description: byte to Hexadecimal 116 | */ 117 | private String byte2Hex(byte[] bytes) { 118 | StringBuffer sb = new StringBuffer(); 119 | String tmp = null; 120 | for (int i = 0; i < bytes.length; i++) { 121 | tmp = Integer.toHexString(bytes[i] & 0xFF); 122 | if (tmp.length() == 1) 123 | sb.append("0"); 124 | sb.append(tmp); 125 | } 126 | return sb.toString(); 127 | } 128 | 129 | 130 | 131 | public class StudentExistException extends Exception { 132 | 133 | private static final long serialVersionUID = 1L; 134 | 135 | } 136 | 137 | public class UserExistException extends Exception { 138 | 139 | private static final long serialVersionUID = 1L; 140 | 141 | } 142 | 143 | public class StudentNotFoundException extends Exception { 144 | 145 | private static final long serialVersionUID = 1L; 146 | 147 | } 148 | 149 | public class StudentSelectedCourseException extends Exception { 150 | 151 | private static final long serialVersionUID = 1L; 152 | 153 | } 154 | 155 | public class CourseExistException extends Exception { 156 | 157 | private static final long serialVersionUID = 1L; 158 | 159 | } 160 | 161 | public class CourseSelectedException extends Exception { 162 | 163 | private static final long serialVersionUID = 1L; 164 | 165 | } 166 | 167 | public class CourseNotFoundException extends Exception { 168 | 169 | private static final long serialVersionUID = 1L; 170 | } 171 | 172 | public class CourseNotSelectedException extends Exception { 173 | 174 | private static final long serialVersionUID = 1L; 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /src/com/student/dao/AdminDAO.java: -------------------------------------------------------------------------------- 1 | package com.student.dao; 2 | 3 | import com.student.base.BaseDAO; 4 | 5 | /** 6 | * @Description: Data Access Object of Student 7 | * @ClassName: AdminDAO 8 | * 9 | */ 10 | public class AdminDAO extends BaseDAO { 11 | 12 | private static AdminDAO ad = null; 13 | 14 | public static synchronized AdminDAO getInstance() { 15 | if (ad == null) { 16 | ad = new AdminDAO(); 17 | } 18 | return ad; 19 | } 20 | 21 | /** 22 | * 23 | * @Description: query students who have selected a specific course. 24 | */ 25 | public String[][] queryStuWhoSeleCou(String cno) { 26 | String sql = 27 | "select sno,grade from course as A, stu_course as B where A.cno=B.cno and A.cno=?"; 28 | String[] param = {cno}; 29 | rs = db.executeQuery(sql, param); 30 | return buildResult(); 31 | } 32 | 33 | /** 34 | * 35 | * @Description: get all courses. 36 | */ 37 | public String[][] getAllCourses() { 38 | String sql = "select * from course"; 39 | rs = db.executeQuery(sql); 40 | return buildResult(); 41 | } 42 | 43 | /** 44 | * 45 | * @Description: get all students. 46 | */ 47 | public String[][] getAllStudents() { 48 | String sql = "select * from student"; 49 | rs = db.executeQuery(sql); 50 | return buildResult(); 51 | } 52 | 53 | 54 | /** 55 | * 56 | * @Description: query the course for a student. 57 | */ 58 | public String[][] queryCourses(String sno) { 59 | String sql = "select cno from stu_course where sno=?"; 60 | String[] param = {sno}; 61 | rs = db.executeQuery(sql, param); 62 | return buildResult(); 63 | } 64 | 65 | /** 66 | * 67 | * @Description: update a student's grade. 68 | */ 69 | public int updateCourseGrade(String sno, String cno, String grade) { 70 | String sql = "update stu_course set grade=? where sno=? and cno=?"; 71 | String[] prarm = {grade, sno, cno}; 72 | return db.executeUpdate(sql, prarm); 73 | } 74 | 75 | /** 76 | * 77 | * @throws CourseExistException 78 | * @Description: AddCourse 79 | */ 80 | public void AddCourse(String[] prarm) throws CourseExistException { 81 | if (queryCourse(prarm[0]).length != 0) { 82 | // check if the course exist 83 | throw new CourseExistException(); 84 | } 85 | String sql = "insert into course values(?,?,?,?,?)"; 86 | db.executeUpdate(sql, prarm); 87 | } 88 | 89 | /** 90 | * 91 | * @throws CourseNotFoundException 92 | * @throws CourseSelectedException 93 | * @Description: DelCourse 94 | */ 95 | public void DelCourse(String cno) throws CourseNotFoundException, CourseSelectedException { 96 | if (queryCourse(cno).length == 0) { 97 | // check if the course exist 98 | throw new CourseNotFoundException(); 99 | } 100 | if (queryStuWhoSeleCou(cno).length != 0) { 101 | // check if some student selected the course 102 | throw new CourseSelectedException(); 103 | } 104 | String sql = "delete from course where cno=?"; 105 | String[] prarm = {cno}; 106 | db.executeUpdate(sql, prarm); 107 | } 108 | 109 | /** 110 | * 111 | * @throws StudentExistException 112 | * @throws UserExistException 113 | * @Description: AddStudent 114 | */ 115 | public void AddStudent(String[] prarm) throws StudentExistException, UserExistException { 116 | if (queryStudent(prarm[0]).length != 0) { 117 | // check if the student exist 118 | throw new StudentExistException(); 119 | } 120 | if (queryUser(prarm[6]).length != 0) { 121 | // check if the username exist 122 | throw new UserExistException(); 123 | } 124 | String sql = "insert into student values(?,?,?,?,?,?,?)"; 125 | prarm[6] = getSHA256(prarm[6] + prarm[5]); 126 | db.executeUpdate(sql, prarm); 127 | 128 | } 129 | 130 | /** 131 | * 132 | * @throws StudentNotFoundException 133 | * @throws StudentSelectedCourseException 134 | * @Description: DelStudent 135 | */ 136 | public void DelStudent(String sno) 137 | throws StudentNotFoundException, StudentSelectedCourseException { 138 | if (queryStudent(sno).length == 0) { 139 | // check if the student exist 140 | throw new StudentNotFoundException(); 141 | } 142 | if (queryCourses(sno).length != 0) { 143 | // check if the student selected some course 144 | throw new StudentSelectedCourseException(); 145 | } 146 | String sql = "delete from student where sno=?"; 147 | String[] prarm = {sno}; 148 | db.executeUpdate(sql, prarm); 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/com/student/dao/StudentDAO.java: -------------------------------------------------------------------------------- 1 | package com.student.dao; 2 | 3 | import java.sql.SQLException; 4 | import com.student.base.BaseDAO; 5 | 6 | /** 7 | * @Description: Data Access Object of Student 8 | * @ClassName: StudentDAO 9 | * 10 | */ 11 | public class StudentDAO extends BaseDAO { 12 | 13 | private static StudentDAO sd = null; 14 | 15 | public static synchronized StudentDAO getInstance() { 16 | if (sd == null) { 17 | sd = new StudentDAO(); 18 | } 19 | return sd; 20 | } 21 | 22 | public String queryForLogin(String username, String password) { 23 | String result = null; 24 | String sql = "select sno from student where username=? and password=?"; 25 | String[] param = {username, getSHA256(password)}; 26 | rs = db.executeQuery(sql, param); 27 | try { 28 | if (rs.next()) { 29 | result = rs.getString("sno"); 30 | } 31 | } catch (SQLException e) { 32 | e.printStackTrace(); 33 | } finally { 34 | destory(); 35 | } 36 | return result; 37 | } 38 | 39 | /** 40 | * 41 | * @Description: query optional courses for a student. 42 | */ 43 | public String[][] queryCourses(String sno) { 44 | String sql = 45 | "select * from course where cno not in (select cno from stu_course where sno=?)"; 46 | String[] param = {sno}; 47 | rs = db.executeQuery(sql, param); 48 | return buildResult(); 49 | } 50 | 51 | /** 52 | * 53 | * @Description: query selected courses for a student. 54 | */ 55 | public String[][] querySelectedCourse(String sno) { 56 | String sql = 57 | "select * from course where cno in (select cno from stu_course where sno=? and grade is null)"; 58 | String[] param = {sno}; 59 | rs = db.executeQuery(sql, param); 60 | return buildResult(); 61 | } 62 | 63 | /** 64 | * 65 | * @Description: query the grade of a specific student. 66 | */ 67 | public String[][] queryStuGrade(String sno) { 68 | String sql = 69 | "select A.cno, cname, grade from course as A, stu_course as B where A.cno = B.cno and sno=? and grade is not null"; 70 | String[] param = {sno}; 71 | rs = db.executeQuery(sql, param); 72 | return buildResult(); 73 | } 74 | 75 | 76 | /** 77 | * 78 | * @throws CourseNotFoundException 79 | * @throws CourseNotSelectedException 80 | * @Description: query a student's grade of a course. 81 | */ 82 | public int queryCourseGrade(String sno, String cno) 83 | throws CourseNotFoundException, CourseNotSelectedException { 84 | String[][] course = queryCourse(cno); 85 | if (course.length == 0) { 86 | throw new CourseNotFoundException(); 87 | } 88 | String sql = "select grade from stu_course where sno=? and cno=?"; 89 | String[] param = {sno, cno}; 90 | rs = db.executeQuery(sql, param); 91 | String grade = null; 92 | try { 93 | if (rs.next()) { 94 | grade = rs.getString("grade"); 95 | } else { 96 | throw new CourseNotSelectedException(); 97 | } 98 | } catch (SQLException e) { 99 | e.printStackTrace(); 100 | } finally { 101 | destory(); 102 | } 103 | return Integer.parseInt(grade); 104 | } 105 | 106 | /** 107 | * 108 | * @Description: select course for a student.
109 | * (sno, cno) should be checked additionally! 110 | * @see #queryCourseGrade(String, String) 111 | */ 112 | public void selectCourse(String sno, String cno) { 113 | String sql = "insert into stu_course values (?,?,null)"; 114 | String[] param = {sno, cno}; 115 | db.executeUpdate(sql, param); 116 | } 117 | 118 | /** 119 | * 120 | * @Description: drop course for a student. 121 | * (sno, cno) should be checked additionally! 122 | * @see #queryCourseGrade(String, String) 123 | */ 124 | public void dropCourse(String sno, String cno) { 125 | String sql = "delete from stu_course where sno=? and cno=?"; 126 | String[] param = {sno, cno}; 127 | db.executeUpdate(sql, param); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/com/student/model/Course.java: -------------------------------------------------------------------------------- 1 | package com.student.model; 2 | 3 | /** 4 | * @Description: Course 5 | * @ClassName: Course 6 | * 7 | */ 8 | public class Course { 9 | 10 | private String cno; 11 | private String cname; 12 | private int credit; 13 | private String cdept; 14 | private String tname; 15 | 16 | public Course(String cno) { 17 | this.cno = cno; 18 | } 19 | 20 | public String getCno() { 21 | return cno; 22 | } 23 | 24 | public void setCno(String cno) { 25 | this.cno = cno; 26 | } 27 | 28 | public String getCname() { 29 | return cname; 30 | } 31 | 32 | public void setCname(String cname) { 33 | this.cname = cname; 34 | } 35 | 36 | public int getCredit() { 37 | return credit; 38 | } 39 | 40 | public void setCredit(int credit) { 41 | this.credit = credit; 42 | } 43 | 44 | public String getCdept() { 45 | return cdept; 46 | } 47 | 48 | public void setCdept(String cdept) { 49 | this.cdept = cdept; 50 | } 51 | 52 | public String getTname() { 53 | return tname; 54 | } 55 | 56 | public void setTname(String tname) { 57 | this.tname = tname; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/com/student/model/Student.java: -------------------------------------------------------------------------------- 1 | package com.student.model; 2 | 3 | /** 4 | * @Description: Student 5 | * @ClassName: Student 6 | * 7 | */ 8 | public class Student { 9 | 10 | private String sno; 11 | private String sname; 12 | private String sex; 13 | private int age; 14 | private String sdept; 15 | private String username; 16 | @Deprecated 17 | private String password; 18 | 19 | public Student(String sno) { 20 | this.sno = sno; 21 | } 22 | 23 | public String getSno() { 24 | return sno; 25 | } 26 | 27 | public void setSno(String sno) { 28 | this.sno = sno; 29 | } 30 | 31 | public String getSname() { 32 | return sname; 33 | } 34 | 35 | public void setSname(String sname) { 36 | this.sname = sname; 37 | } 38 | 39 | public String getSex() { 40 | return sex; 41 | } 42 | 43 | public void setSex(String sex) { 44 | this.sex = sex; 45 | } 46 | 47 | public int getAge() { 48 | return age; 49 | } 50 | 51 | public void setAge(int age) { 52 | this.age = age; 53 | } 54 | 55 | public String getSdept() { 56 | return sdept; 57 | } 58 | 59 | public void setSdept(String sdept) { 60 | this.sdept = sdept; 61 | } 62 | 63 | public String getUsername() { 64 | return username; 65 | } 66 | 67 | public void setUsername(String username) { 68 | this.username = username; 69 | } 70 | 71 | @Deprecated 72 | public String getPassword() { 73 | return password; 74 | } 75 | 76 | @Deprecated 77 | public void setPassword(String password) { 78 | this.password = password; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/com/student/util/DBUtil.java: -------------------------------------------------------------------------------- 1 | package com.student.util; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import com.student.AppConstants; 9 | 10 | /** 11 | * @Description: DataBase Utility 12 | * @ClassName: DBUtil 13 | * 14 | */ 15 | 16 | public class DBUtil { 17 | 18 | private static DBUtil db; 19 | 20 | private Connection conn; 21 | private PreparedStatement ps; 22 | private ResultSet rs; 23 | 24 | public static DBUtil getDBUtil() { 25 | if (db == null) { 26 | db = new DBUtil(); 27 | } 28 | return db; 29 | } 30 | 31 | private DBUtil() { 32 | 33 | } 34 | 35 | public void close() { 36 | try { 37 | if (rs != null) { 38 | rs.close(); 39 | } 40 | if (ps != null) { 41 | ps.close(); 42 | } 43 | if (conn != null) { 44 | conn.close(); 45 | } 46 | } catch (SQLException e) { 47 | e.printStackTrace(); 48 | } 49 | } 50 | 51 | public ResultSet executeQuery(String sql) { 52 | if (getConn() == null) { 53 | return null; 54 | } 55 | try { 56 | ps = conn.prepareStatement(sql); 57 | rs = ps.executeQuery(); 58 | } catch (SQLException e) { 59 | e.printStackTrace(); 60 | } 61 | return rs; 62 | } 63 | 64 | public ResultSet executeQuery(String sql, Object[] obj) { 65 | if (getConn() == null) { 66 | return null; 67 | } 68 | try { 69 | ps = conn.prepareStatement(sql); 70 | for (int i = 0; i < obj.length; i++) { 71 | ps.setObject(i + 1, obj[i]); 72 | } 73 | rs = ps.executeQuery(); 74 | } catch (SQLException e) { 75 | e.printStackTrace(); 76 | } 77 | 78 | return rs; 79 | } 80 | 81 | public int executeUpdate(String sql) { 82 | int result = -1; 83 | if (getConn() == null) { 84 | return result; 85 | } 86 | try { 87 | ps = conn.prepareStatement(sql); 88 | result = ps.executeUpdate(); 89 | } catch (SQLException e) { 90 | e.printStackTrace(); 91 | } 92 | return result; 93 | } 94 | 95 | public int executeUpdate(String sql, Object[] obj) { 96 | int result = -1; 97 | if (getConn() == null) { 98 | return result; 99 | } 100 | try { 101 | ps = conn.prepareStatement(sql); 102 | for (int i = 0; i < obj.length; i++) { 103 | ps.setObject(i + 1, obj[i]); 104 | } 105 | result = ps.executeUpdate(); 106 | } catch (SQLException e) { 107 | e.printStackTrace(); 108 | } 109 | return result; 110 | } 111 | 112 | private Connection getConn() { 113 | try { 114 | if (conn == null || conn.isClosed()) { 115 | Class.forName(AppConstants.JDBC_DRIVER); 116 | conn = DriverManager.getConnection(AppConstants.JDBC_URL, 117 | AppConstants.JDBC_USERNAME, AppConstants.JDBC_PASSWORD); 118 | } 119 | } catch (ClassNotFoundException e) { 120 | System.err.println("JDBC Driver is not found."); 121 | } catch (SQLException e) { 122 | e.printStackTrace(); 123 | } 124 | return conn; 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/com/student/view/AdminView.java: -------------------------------------------------------------------------------- 1 | package com.student.view; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.Color; 5 | import java.awt.Component; 6 | import java.awt.Dimension; 7 | import java.awt.FlowLayout; 8 | import java.awt.GridLayout; 9 | import java.awt.event.ActionEvent; 10 | import java.awt.event.ActionListener; 11 | import java.awt.event.InputEvent; 12 | import java.awt.event.KeyEvent; 13 | import java.awt.event.WindowAdapter; 14 | import java.awt.event.WindowEvent; 15 | import java.util.Vector; 16 | import javax.swing.Box; 17 | import javax.swing.BoxLayout; 18 | import javax.swing.JButton; 19 | import javax.swing.JComboBox; 20 | import javax.swing.JFrame; 21 | import javax.swing.JLabel; 22 | import javax.swing.JMenu; 23 | import javax.swing.JMenuBar; 24 | import javax.swing.JMenuItem; 25 | import javax.swing.JPanel; 26 | import javax.swing.JScrollPane; 27 | import javax.swing.JTable; 28 | import javax.swing.JToggleButton; 29 | import javax.swing.KeyStroke; 30 | import javax.swing.table.DefaultTableCellRenderer; 31 | import javax.swing.table.DefaultTableModel; 32 | import javax.swing.table.TableCellEditor; 33 | import com.student.AppConstants; 34 | import com.student.dao.AdminDAO; 35 | import com.student.model.Course; 36 | 37 | /** 38 | * @Description: AdminView 39 | * @ClassName: AdminView 40 | * 41 | */ 42 | public class AdminView extends JFrame { 43 | 44 | private static final long serialVersionUID = 1L; 45 | private JMenuBar menuBar; 46 | private JPanel contentPane; 47 | private JComboBox course; 48 | private Vector courses; 49 | private JLabel coursename, teachername; 50 | private JTable gradetable; 51 | private static final String[] infocolumn = {AppConstants.SNO, AppConstants.SCORE}; 52 | private JButton querybtn; 53 | private JToggleButton inputbtn; 54 | private JPanel choosebox; 55 | 56 | public AdminView() { 57 | System.out.println("Admin Login Success."); 58 | 59 | setResizable(false); 60 | setTitle(AppConstants.ADMIN_TITLE); 61 | setSize(450, 300); 62 | setLocationRelativeTo(null); 63 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); 64 | setVisible(true); 65 | 66 | menuBar = new JMenuBar(); 67 | setJMenuBar(menuBar); 68 | 69 | contentPane = new JPanel(); 70 | setContentPane(contentPane); 71 | contentPane.setLayout(new BorderLayout(5, 5)); 72 | 73 | JPanel panel = new JPanel(); 74 | contentPane.add(panel, BorderLayout.NORTH); 75 | panel.setLayout(new GridLayout(0, 2, 0, 0)); 76 | 77 | JPanel courepanel = new JPanel(); 78 | panel.add(courepanel); 79 | courepanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 80 | 81 | JLabel courselabel = new JLabel(AppConstants.ADMIN_CNAME); 82 | courepanel.add(courselabel); 83 | 84 | coursename = new JLabel(); 85 | courepanel.add(coursename); 86 | 87 | JPanel teacherpanel = new JPanel(); 88 | panel.add(teacherpanel); 89 | teacherpanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 90 | 91 | JLabel teacherlabel = new JLabel(AppConstants.ADMIN_TNAME); 92 | teacherpanel.add(teacherlabel); 93 | 94 | teachername = new JLabel(); 95 | teacherpanel.add(teachername); 96 | 97 | addWindowListener(new WindowAdapter() { 98 | 99 | @Override 100 | public void windowClosed(WindowEvent e) { 101 | System.out.println("admin Logout."); 102 | new LoginView(); 103 | } 104 | }); 105 | 106 | initMenu(); 107 | initChoose(); 108 | initGrade(); 109 | initBtn(); 110 | } 111 | 112 | private void initMenu() { 113 | JMenu maintain = new JMenu(AppConstants.ADMIN_MAINTAIN); 114 | menuBar.add(maintain); 115 | 116 | JMenuItem courseinfo = new JMenuItem(AppConstants.ADMIN_COURSEINFO); 117 | courseinfo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.ALT_MASK)); 118 | maintain.add(courseinfo); 119 | courseinfo.addActionListener(new ActionListener() { 120 | @Override 121 | public void actionPerformed(ActionEvent e) { 122 | CourseInfo cInfo = new CourseInfo(AdminView.this); 123 | cInfo.setVisible(true); 124 | } 125 | }); 126 | 127 | JMenuItem studentinfo = new JMenuItem(AppConstants.ADMIN_STUDENTINFO); 128 | studentinfo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK)); 129 | maintain.add(studentinfo); 130 | studentinfo.addActionListener(new ActionListener() { 131 | @Override 132 | public void actionPerformed(ActionEvent e) { 133 | StudentInfo cInfo = new StudentInfo(AdminView.this); 134 | cInfo.setVisible(true); 135 | } 136 | }); 137 | } 138 | 139 | private void initChoose() { 140 | System.err.println("Loading Choose Box..."); 141 | JPanel panel = new JPanel(); 142 | contentPane.add(panel, BorderLayout.WEST); 143 | panel.setLayout(new BorderLayout(0, 0)); 144 | 145 | panel.add(new JLabel(AppConstants.ADMIN_CHOOSE), BorderLayout.NORTH); 146 | 147 | choosebox = new JPanel(); 148 | panel.add(choosebox); 149 | 150 | course = new JComboBox<>(); 151 | course.setPreferredSize(new Dimension(100, 20)); 152 | choosebox.add(course); 153 | genChoice(); 154 | } 155 | 156 | protected void genChoice() { 157 | course.removeAllItems(); 158 | String[][] result = AdminDAO.getInstance().getAllCourses(); 159 | courses = new Vector<>(); 160 | 161 | for (int i = 0; i < result.length; i++) { 162 | Course c = new Course(result[i][0]); 163 | c.setCname(result[i][1]); 164 | try { 165 | c.setCredit(Integer.parseInt(result[i][2])); 166 | } catch (NumberFormatException e) { 167 | c.setCredit(0); 168 | } 169 | c.setCdept(result[i][3]); 170 | c.setTname(result[i][4]); 171 | courses.add(c); 172 | course.addItem(c.getCname()); 173 | } 174 | coursename.setText(null); 175 | teachername.setText(null); 176 | course.setSelectedIndex(-1); 177 | } 178 | 179 | private void initGrade() { 180 | System.err.println("Loading Score Info..."); 181 | JPanel panel = new JPanel(); 182 | contentPane.add(panel, BorderLayout.CENTER); 183 | panel.setLayout(new BorderLayout(5, 5)); 184 | 185 | panel.add(new JLabel(AppConstants.ADMIN_SELECTIONINFO), BorderLayout.NORTH); 186 | 187 | gradetable = new JTable(); 188 | gradetable.setEnabled(false); 189 | 190 | initGradeTable(gradetable, null, infocolumn); 191 | JScrollPane scrollPane = new JScrollPane(gradetable); 192 | gradetable.getTableHeader().setReorderingAllowed(false); 193 | panel.add(scrollPane, BorderLayout.CENTER); 194 | } 195 | 196 | private void initBtn() { 197 | System.err.println("Loading Buttons..."); 198 | JPanel panel = new JPanel(); 199 | contentPane.add(panel, BorderLayout.EAST); 200 | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 201 | 202 | RedCellRenderer rcr = new RedCellRenderer(); 203 | gradetable.setDefaultRenderer(Object.class, rcr); 204 | 205 | querybtn = new JButton(AppConstants.ADMIN_QUERY); 206 | querybtn.setAlignmentX(Component.CENTER_ALIGNMENT); 207 | inputbtn = new JToggleButton(AppConstants.ADMIN_INPUT); 208 | inputbtn.setAlignmentX(Component.CENTER_ALIGNMENT); 209 | inputbtn.setEnabled(false); 210 | course.addActionListener(new ActionListener() { 211 | 212 | @Override 213 | public void actionPerformed(ActionEvent e) { 214 | inputbtn.setEnabled(false); 215 | } 216 | }); 217 | JButton exitbtn = new JButton(AppConstants.ADMIN_CLOSE); 218 | exitbtn.setAlignmentX(Component.CENTER_ALIGNMENT); 219 | 220 | panel.add(Box.createRigidArea(new Dimension(100, 30))); 221 | panel.add(querybtn); 222 | panel.add(Box.createRigidArea(new Dimension(100, 30))); 223 | panel.add(inputbtn); 224 | panel.add(Box.createRigidArea(new Dimension(100, 30))); 225 | panel.add(exitbtn); 226 | 227 | inputbtn.addActionListener(new InputListener()); 228 | querybtn.addActionListener(new ActionListener() { 229 | 230 | @Override 231 | public void actionPerformed(ActionEvent e) { 232 | 233 | int index = course.getSelectedIndex(); 234 | try { 235 | coursename.setText(courses.get(index).getCname()); 236 | teachername.setText(courses.get(index).getTname()); 237 | String[][] result = 238 | AdminDAO.getInstance().queryStuWhoSeleCou(courses.get(index).getCno()); 239 | initGradeTable(gradetable, result, infocolumn); 240 | inputbtn.setEnabled(true); 241 | } catch (ArrayIndexOutOfBoundsException e2) { 242 | // Do nothing... 243 | } 244 | } 245 | }); 246 | exitbtn.addActionListener(new ActionListener() { 247 | 248 | @Override 249 | public void actionPerformed(ActionEvent e) { 250 | dispose(); 251 | } 252 | }); 253 | getRootPane().setDefaultButton(querybtn); 254 | } 255 | 256 | private void initGradeTable(JTable jTable, String[][] result, String[] column) { 257 | jTable.setModel(new DefaultTableModel(result, column) { 258 | 259 | private static final long serialVersionUID = 1L; 260 | 261 | @Override 262 | public boolean isCellEditable(int row, int column) { 263 | return column == 1; 264 | } 265 | }); 266 | jTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 267 | } 268 | 269 | private class InputListener implements ActionListener { 270 | 271 | @Override 272 | public void actionPerformed(ActionEvent e) { 273 | JToggleButton btn = (JToggleButton) e.getSource(); 274 | if (btn.isSelected()) { 275 | System.out.println("Edit students' grade"); 276 | gradetable.setEnabled(true); 277 | btn.setText(AppConstants.ADMIN_SAVE); 278 | course.setEnabled(false); 279 | querybtn.setEnabled(false); 280 | } else { 281 | save(gradetable); 282 | if (!gradeCheck()) { 283 | btn.setSelected(true); 284 | return; 285 | } 286 | System.out.println("Update students' grade"); 287 | update(); 288 | gradetable.setEnabled(false); 289 | btn.setText(AppConstants.ADMIN_INPUT); 290 | course.setEnabled(true); 291 | querybtn.setEnabled(true); 292 | } 293 | } 294 | 295 | private boolean gradeCheck() { 296 | int row = gradetable.getRowCount(); 297 | for (int i = 0; i < row; i++) { 298 | String grade = (String) gradetable.getValueAt(i, 1); 299 | if (grade == null || grade.equals("")) { 300 | continue; 301 | } 302 | try { 303 | int g = Integer.parseInt(grade); 304 | if (g < 0 || g > 100) 305 | return false; 306 | } catch (NumberFormatException e) { 307 | return false; 308 | } 309 | } 310 | return true; 311 | } 312 | 313 | private void save(JTable table) { 314 | if (table.isEditing()) { 315 | TableCellEditor cellEditor = table.getCellEditor(); 316 | if (cellEditor != null) { 317 | cellEditor.stopCellEditing(); 318 | } 319 | } 320 | } 321 | 322 | private void update() { 323 | int index = course.getSelectedIndex(); 324 | int row = gradetable.getRowCount(); 325 | String cno = courses.get(index).getCno(); 326 | for (int i = 0; i < row; i++) { 327 | String sno = (String) gradetable.getValueAt(i, 0); 328 | String grade = (String) gradetable.getValueAt(i, 1); 329 | try { 330 | Integer.parseInt(grade); 331 | } catch (NumberFormatException e) { 332 | grade = null; 333 | gradetable.setValueAt(grade, i, 1); 334 | } 335 | AdminDAO.getInstance().updateCourseGrade(sno, cno, grade); 336 | } 337 | } 338 | } 339 | 340 | /** 341 | * 342 | * @Description: when you text invalid content, the cell will be red. 343 | */ 344 | public class RedCellRenderer extends DefaultTableCellRenderer { 345 | 346 | private static final long serialVersionUID = 1L; 347 | 348 | @Override 349 | public Component getTableCellRendererComponent(JTable table, Object value, 350 | boolean isSelected, boolean hasFocus, int row, int column) { 351 | Component com = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, 352 | row, column); 353 | String grade = (String) table.getModel().getValueAt(row, 1); 354 | if (column != 1) { 355 | com.setBackground(Color.WHITE); 356 | return com; 357 | } 358 | if (grade == null || grade.equals("")) { 359 | com.setBackground(Color.WHITE); 360 | } else { 361 | try { 362 | 363 | int g = Integer.parseInt(grade); 364 | if (g < 0 || g > 100) 365 | com.setBackground(Color.PINK); 366 | else 367 | com.setBackground(Color.WHITE); 368 | 369 | } catch (NumberFormatException e) { 370 | com.setBackground(Color.PINK); 371 | } 372 | } 373 | return com; 374 | } 375 | } 376 | 377 | } 378 | -------------------------------------------------------------------------------- /src/com/student/view/CourseInfo.java: -------------------------------------------------------------------------------- 1 | package com.student.view; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.Color; 5 | import java.awt.Component; 6 | import java.awt.Dimension; 7 | import java.awt.FlowLayout; 8 | import java.awt.GridLayout; 9 | import java.awt.event.ActionEvent; 10 | import java.awt.event.ActionListener; 11 | import java.util.regex.Pattern; 12 | import javax.swing.Box; 13 | import javax.swing.BoxLayout; 14 | import javax.swing.JButton; 15 | import javax.swing.JDialog; 16 | import javax.swing.JLabel; 17 | import javax.swing.JOptionPane; 18 | import javax.swing.JPanel; 19 | import javax.swing.JScrollPane; 20 | import javax.swing.JTable; 21 | import javax.swing.JTextField; 22 | import javax.swing.table.DefaultTableModel; 23 | import com.student.AppConstants; 24 | import com.student.base.BaseDAO.CourseExistException; 25 | import com.student.base.BaseDAO.CourseNotFoundException; 26 | import com.student.base.BaseDAO.CourseSelectedException; 27 | import com.student.dao.AdminDAO; 28 | 29 | public class CourseInfo extends JDialog { 30 | 31 | private static final long serialVersionUID = 1L; 32 | 33 | private JPanel container; 34 | private JTable courseMess; 35 | private static final String[] infocolumn = {AppConstants.CNO, AppConstants.CNAME, 36 | AppConstants.CREDIT, AppConstants.CDEPT, AppConstants.TNAME}; 37 | private JLabel totCount; 38 | private AdminView frame; 39 | 40 | public CourseInfo(AdminView frame) { 41 | super(frame, AppConstants.ADMIN_COURSEINFO, true); 42 | this.frame = frame; 43 | setResizable(false); 44 | setLocationRelativeTo(null); 45 | setSize(450, 300); 46 | setTitle(AppConstants.ADMIN_COURSEINFO); 47 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); 48 | 49 | container = new JPanel(); 50 | setContentPane(container); 51 | container.setLayout(new BorderLayout(5, 5)); 52 | 53 | initBtn(); 54 | initTable(); 55 | 56 | } 57 | 58 | public void initBtn() { 59 | JPanel panel = new JPanel(); 60 | container.add(panel, BorderLayout.EAST); 61 | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 62 | 63 | JButton addBtn = new JButton(AppConstants.ADMIN_COURSEINFO_ADD); 64 | addBtn.setAlignmentX(Component.CENTER_ALIGNMENT); 65 | JButton delBtn = new JButton(AppConstants.ADMIN_COURSEINFO_DEL); 66 | delBtn.setAlignmentX(Component.CENTER_ALIGNMENT); 67 | JButton quitBtn = new JButton(AppConstants.ADMIN_COURSEINFO_QUIT); 68 | quitBtn.setAlignmentX(Component.CENTER_ALIGNMENT); 69 | 70 | panel.add(Box.createRigidArea(new Dimension(100, 30))); 71 | panel.add(addBtn); 72 | panel.add(Box.createRigidArea(new Dimension(100, 30))); 73 | panel.add(delBtn); 74 | panel.add(Box.createRigidArea(new Dimension(100, 30))); 75 | panel.add(quitBtn); 76 | 77 | addBtn.addActionListener(new ActionListener() { 78 | 79 | @Override 80 | public void actionPerformed(ActionEvent e) { 81 | AddCourse ac = new AddCourse(CourseInfo.this); 82 | ac.setVisible(true); 83 | } 84 | }); 85 | delBtn.addActionListener(new ActionListener() { 86 | 87 | @Override 88 | public void actionPerformed(ActionEvent e) { 89 | DelCourse dc = new DelCourse(CourseInfo.this); 90 | dc.setVisible(true); 91 | } 92 | }); 93 | quitBtn.addActionListener(new ActionListener() { 94 | 95 | @Override 96 | public void actionPerformed(ActionEvent e) { 97 | dispose(); 98 | frame.genChoice(); 99 | } 100 | }); 101 | } 102 | 103 | public void initTable() { 104 | JPanel panel = new JPanel(); 105 | container.add(panel, BorderLayout.CENTER); 106 | courseMess = new JTable(); 107 | courseMess.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 108 | totCount = new JLabel(); 109 | panel.add(totCount, BorderLayout.NORTH); 110 | courseMess.setEnabled(false); 111 | courseMess.getTableHeader().setReorderingAllowed(false); 112 | JScrollPane scrollPane = new JScrollPane(courseMess); 113 | scrollPane.setPreferredSize(new Dimension(300, 180)); 114 | panel.add(scrollPane); 115 | genTable(); 116 | } 117 | 118 | public void genTable() { 119 | String[][] result = AdminDAO.getInstance().getAllCourses(); 120 | courseMess.setModel(new DefaultTableModel(result, infocolumn) { 121 | private static final long serialVersionUID = 1L; 122 | }); 123 | totCount.setText(AppConstants.TOTAL_COUNT + String.valueOf(courseMess.getRowCount())); 124 | } 125 | 126 | private class AddCourse extends JDialog { 127 | 128 | private static final long serialVersionUID = 1L; 129 | 130 | private JPanel contPanel; 131 | private JTextField[] tFields; 132 | private final String[] checkregex = {AppConstants.REGEX_CNO, AppConstants.REGEX_CNAME, 133 | AppConstants.REGEX_CREDIT, AppConstants.REGEX_CDEPT, AppConstants.REGEX_TNAME}; 134 | private final boolean[] checknull = {false, false, true, true, true}; 135 | 136 | public AddCourse(CourseInfo frame) { 137 | super(frame, AppConstants.ADMIN_COURSEINFO_ADD, true); 138 | contPanel = new JPanel(); 139 | setContentPane(contPanel); 140 | setLayout(new BorderLayout(5, 5)); 141 | setResizable(false); 142 | setLocationRelativeTo(null); 143 | setSize(310, 260); 144 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); 145 | 146 | initBtn(); 147 | initTable(); 148 | } 149 | 150 | public void initBtn() { 151 | JPanel panel = new JPanel(); 152 | JButton jb = new JButton(AppConstants.VERIFY); 153 | panel.add(jb); 154 | contPanel.add(panel, BorderLayout.SOUTH); 155 | getRootPane().setDefaultButton(jb); 156 | jb.addActionListener(new ActionListener() { 157 | @Override 158 | public void actionPerformed(ActionEvent e) { 159 | String[] info = new String[5]; 160 | for (int i = 0; i < 5; i++) { 161 | info[i] = tFields[i].getText(); 162 | } 163 | boolean isVaild = true; 164 | for (int i = 0; i < 5; i++) { 165 | if (Pattern.matches(checkregex[i], info[i]) == false) { 166 | isVaild = false; 167 | tFields[i].setBackground(Color.PINK); 168 | } else { 169 | tFields[i].setBackground(Color.WHITE); 170 | } 171 | if (checknull[i] && info[i].equals("")) { 172 | info[i] = null; 173 | } 174 | } 175 | if (!isVaild) { 176 | return; 177 | } 178 | try { 179 | AdminDAO.getInstance().AddCourse(info); 180 | } catch (CourseExistException e1) { 181 | JOptionPane.showMessageDialog(null, AppConstants.ADMIN_CNO_EXIST_ERROR, 182 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 183 | return; 184 | } 185 | dispose(); 186 | CourseInfo.this.genTable(); 187 | } 188 | }); 189 | } 190 | 191 | public void initTable() { 192 | JPanel panel = new JPanel(); 193 | panel.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 15)); 194 | JPanel[] panels = new JPanel[5]; 195 | JLabel[] labels = new JLabel[5]; 196 | tFields = new JTextField[5]; 197 | for (int i = 0; i < 5; i++) { 198 | panels[i] = new JPanel(); 199 | panels[i].setLayout(new GridLayout(1, 2, 5, 5)); 200 | labels[i] = new JLabel(infocolumn[i]); 201 | tFields[i] = new JTextField(10); 202 | panels[i].add(labels[i], Component.CENTER_ALIGNMENT); 203 | panels[i].add(tFields[i], Component.CENTER_ALIGNMENT); 204 | panel.add(panels[i]); 205 | } 206 | contPanel.add(panel, BorderLayout.CENTER); 207 | } 208 | } 209 | 210 | private class DelCourse extends JDialog { 211 | 212 | private static final long serialVersionUID = 1L; 213 | private JPanel contPanel; 214 | private JTextField tField; 215 | 216 | public DelCourse(CourseInfo frame) { 217 | super(frame, AppConstants.ADMIN_COURSEINFO_DEL, true); 218 | contPanel = new JPanel(); 219 | setContentPane(contPanel); 220 | setLayout(new BorderLayout(5, 5)); 221 | setResizable(false); 222 | setLocationRelativeTo(null); 223 | setSize(280, 120); 224 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); 225 | 226 | initBtn(); 227 | initTable(); 228 | } 229 | 230 | public void initBtn() { 231 | JPanel panel = new JPanel(); 232 | JButton jb = new JButton(AppConstants.DELETE); 233 | panel.add(jb); 234 | contPanel.add(panel, BorderLayout.SOUTH); 235 | getRootPane().setDefaultButton(jb); 236 | jb.addActionListener(new ActionListener() { 237 | @Override 238 | public void actionPerformed(ActionEvent e) { 239 | String cno = tField.getText(); 240 | if (Pattern.matches(AppConstants.REGEX_SNO, cno) == false) { 241 | tField.setBackground(Color.PINK); 242 | return; 243 | } else { 244 | tField.setBackground(Color.WHITE); 245 | } 246 | try { 247 | AdminDAO.getInstance().DelCourse(cno); 248 | } catch (CourseNotFoundException e1) { 249 | JOptionPane.showMessageDialog(null, AppConstants.ADMIN_CNO_NOTEXIST_ERROR, 250 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 251 | return; 252 | } catch (CourseSelectedException e2) { 253 | JOptionPane.showMessageDialog(null, AppConstants.ADMIN_COURSESELECTED_ERROR, 254 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 255 | return; 256 | } 257 | dispose(); 258 | CourseInfo.this.genTable(); 259 | } 260 | }); 261 | } 262 | 263 | public void initTable() { 264 | JPanel panel = new JPanel(); 265 | panel.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 15)); 266 | JPanel panel2 = new JPanel(); 267 | panel2.setLayout(new GridLayout(1, 2, 5, 5)); 268 | JLabel label = new JLabel(AppConstants.CNO); 269 | tField = new JTextField(10); 270 | panel2.add(label, Component.CENTER_ALIGNMENT); 271 | panel2.add(tField, Component.LEFT_ALIGNMENT); 272 | panel.add(panel2); 273 | contPanel.add(panel, BorderLayout.CENTER); 274 | } 275 | } 276 | } 277 | -------------------------------------------------------------------------------- /src/com/student/view/LoginView.java: -------------------------------------------------------------------------------- 1 | package com.student.view; 2 | 3 | import java.awt.Color; 4 | import java.awt.GridLayout; 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | import java.util.regex.Pattern; 8 | import javax.swing.JButton; 9 | import javax.swing.JFrame; 10 | import javax.swing.JLabel; 11 | import javax.swing.JOptionPane; 12 | import javax.swing.JPanel; 13 | import javax.swing.JPasswordField; 14 | import javax.swing.JTextField; 15 | import com.student.AppConstants; 16 | import com.student.dao.StudentDAO; 17 | import com.student.model.Student; 18 | 19 | /** 20 | * @Description: LoginView 21 | * @ClassName: LoginView 22 | * 23 | */ 24 | public class LoginView extends JFrame { 25 | 26 | private static final long serialVersionUID = 1L; 27 | private JPanel contentPane; 28 | private JTextField userField; 29 | private JPasswordField passwordField; 30 | 31 | public LoginView() { 32 | setResizable(false); 33 | setTitle(AppConstants.LOGIN_TITLE); 34 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); 35 | setSize(300, 200); 36 | setLocationRelativeTo(null); 37 | 38 | contentPane = new JPanel(); 39 | setContentPane(contentPane); 40 | contentPane.setLayout(new GridLayout(4, 1)); 41 | 42 | contentPane.add(new JPanel()); 43 | 44 | JPanel userPanel = new JPanel(); 45 | contentPane.add(userPanel); 46 | 47 | JLabel userLabel = new JLabel(AppConstants.LOGIN_USERNAME); 48 | userPanel.add(userLabel); 49 | 50 | userField = new JTextField(10); 51 | userPanel.add(userField); 52 | 53 | JPanel passwordPanel = new JPanel(); 54 | contentPane.add(passwordPanel); 55 | 56 | JLabel passwordLael = new JLabel(AppConstants.LOGIN_PASSWORD); 57 | passwordPanel.add(passwordLael); 58 | 59 | passwordField = new JPasswordField(); 60 | passwordField.setColumns(10); 61 | passwordPanel.add(passwordField); 62 | 63 | JPanel btnPanel = new JPanel(); 64 | contentPane.add(btnPanel); 65 | 66 | JButton loginbtn = new JButton(AppConstants.LOGIN); 67 | btnPanel.add(loginbtn); 68 | loginbtn.addActionListener(new LoginListener()); 69 | getRootPane().setDefaultButton(loginbtn); 70 | 71 | JButton exitbtn = new JButton(AppConstants.EXIT); 72 | exitbtn.addActionListener(new ActionListener() { 73 | 74 | @Override 75 | public void actionPerformed(ActionEvent e) { 76 | dispose(); 77 | } 78 | }); 79 | btnPanel.add(exitbtn); 80 | setVisible(true); 81 | } 82 | 83 | private class LoginListener implements ActionListener { 84 | 85 | @Override 86 | public void actionPerformed(ActionEvent e) { 87 | 88 | String username = userField.getText(); 89 | String password = String.valueOf(passwordField.getPassword()); 90 | 91 | boolean isValid = true; 92 | if (Pattern.matches(AppConstants.REGEX_USERNAME, username) == false) { 93 | userField.setBackground(Color.PINK); 94 | isValid = false; 95 | } else { 96 | userField.setBackground(Color.WHITE); 97 | } 98 | if (Pattern.matches(AppConstants.REGEX_PASSWORD, password) == false) { 99 | passwordField.setBackground(Color.PINK); 100 | isValid = false; 101 | } else 102 | passwordField.setBackground(Color.WHITE); 103 | 104 | if (isValid == false) { 105 | return; 106 | } else { 107 | userField.setBackground(Color.WHITE); 108 | passwordField.setBackground(Color.WHITE); 109 | } 110 | 111 | if (username.equals("admin")) { 112 | if (password.equals("admin1234")) { 113 | dispose(); 114 | new AdminView(); 115 | } else { 116 | userField.setBackground(Color.PINK); 117 | passwordField.setBackground(Color.PINK); 118 | JOptionPane.showMessageDialog(null, AppConstants.LOGIN_ERROR); 119 | } 120 | } else { 121 | String sno = StudentDAO.getInstance().queryForLogin(username, password + username); 122 | if (sno != null) { 123 | dispose(); 124 | new StudentView(new Student(sno)); 125 | } else { 126 | userField.setBackground(Color.PINK); 127 | passwordField.setBackground(Color.PINK); 128 | JOptionPane.showMessageDialog(null, AppConstants.LOGIN_ERROR); 129 | } 130 | } 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/com/student/view/StudentInfo.java: -------------------------------------------------------------------------------- 1 | package com.student.view; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.Color; 5 | import java.awt.Component; 6 | import java.awt.Dimension; 7 | import java.awt.FlowLayout; 8 | import java.awt.GridLayout; 9 | import java.awt.event.ActionEvent; 10 | import java.awt.event.ActionListener; 11 | import java.util.regex.Pattern; 12 | import javax.swing.Box; 13 | import javax.swing.BoxLayout; 14 | import javax.swing.JButton; 15 | import javax.swing.JDialog; 16 | import javax.swing.JLabel; 17 | import javax.swing.JOptionPane; 18 | import javax.swing.JPanel; 19 | import javax.swing.JScrollPane; 20 | import javax.swing.JTable; 21 | import javax.swing.JTextField; 22 | import javax.swing.table.DefaultTableModel; 23 | import com.student.AppConstants; 24 | import com.student.base.BaseDAO.StudentExistException; 25 | import com.student.base.BaseDAO.StudentNotFoundException; 26 | import com.student.base.BaseDAO.StudentSelectedCourseException; 27 | import com.student.base.BaseDAO.UserExistException; 28 | import com.student.dao.AdminDAO; 29 | 30 | public class StudentInfo extends JDialog { 31 | 32 | private static final long serialVersionUID = 1L; 33 | private JPanel container; 34 | private JTable stuMess; 35 | private static final String[] infocolumn = 36 | {AppConstants.SNO, AppConstants.SNAME, AppConstants.SEX, AppConstants.AGE, 37 | AppConstants.SDEPT, AppConstants.USERNAME, AppConstants.PASSWORD}; 38 | private JLabel totCount; 39 | 40 | public StudentInfo(AdminView frame) { 41 | super(frame, AppConstants.ADMIN_SUTDENTINFO, true); 42 | setResizable(false); 43 | setLocationRelativeTo(null); 44 | setSize(450, 300); 45 | setTitle(AppConstants.ADMIN_SUTDENTINFO); 46 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); 47 | 48 | container = new JPanel(); 49 | setContentPane(container); 50 | container.setLayout(new BorderLayout(5, 5)); 51 | 52 | initBtn(); 53 | initTable(); 54 | 55 | } 56 | 57 | public void initBtn() { 58 | JPanel panel = new JPanel(); 59 | container.add(panel, BorderLayout.EAST); 60 | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 61 | 62 | JButton addBtn = new JButton(AppConstants.ADMIN_SUTDENTINFO_ADD); 63 | addBtn.setAlignmentX(Component.CENTER_ALIGNMENT); 64 | JButton delBtn = new JButton(AppConstants.ADMIN_SUTDENTINFO_DEL); 65 | delBtn.setAlignmentX(Component.CENTER_ALIGNMENT); 66 | JButton quitBtn = new JButton(AppConstants.EXIT); 67 | quitBtn.setAlignmentX(Component.CENTER_ALIGNMENT); 68 | 69 | panel.add(Box.createRigidArea(new Dimension(100, 30))); 70 | panel.add(addBtn); 71 | panel.add(Box.createRigidArea(new Dimension(100, 30))); 72 | panel.add(delBtn); 73 | panel.add(Box.createRigidArea(new Dimension(100, 30))); 74 | panel.add(quitBtn); 75 | 76 | addBtn.addActionListener(new ActionListener() { 77 | 78 | @Override 79 | public void actionPerformed(ActionEvent e) { 80 | AddStudent ac = new AddStudent(StudentInfo.this); 81 | ac.setVisible(true); 82 | } 83 | }); 84 | delBtn.addActionListener(new ActionListener() { 85 | 86 | @Override 87 | public void actionPerformed(ActionEvent e) { 88 | DelStudent dc = new DelStudent(StudentInfo.this); 89 | dc.setVisible(true); 90 | } 91 | }); 92 | quitBtn.addActionListener(new ActionListener() { 93 | 94 | @Override 95 | public void actionPerformed(ActionEvent e) { 96 | dispose(); 97 | } 98 | }); 99 | } 100 | 101 | public void initTable() { 102 | JPanel panel = new JPanel(); 103 | container.add(panel, BorderLayout.CENTER); 104 | stuMess = new JTable(); 105 | stuMess.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 106 | totCount = new JLabel(); 107 | panel.add(totCount, BorderLayout.NORTH); 108 | stuMess.setEnabled(false); 109 | stuMess.getTableHeader().setReorderingAllowed(false); 110 | JScrollPane scrollPane = new JScrollPane(stuMess); 111 | scrollPane.setPreferredSize(new Dimension(300, 180)); 112 | panel.add(scrollPane); 113 | genTable(); 114 | } 115 | 116 | public void genTable() { 117 | String[][] result = AdminDAO.getInstance().getAllStudents(); 118 | stuMess.setModel(new DefaultTableModel(result, infocolumn) { 119 | private static final long serialVersionUID = 1L; 120 | }); 121 | totCount.setText(AppConstants.TOTAL_COUNT + String.valueOf(stuMess.getRowCount())); 122 | } 123 | 124 | private class AddStudent extends JDialog { 125 | 126 | private static final long serialVersionUID = 1L; 127 | private JPanel contPanel; 128 | private JTextField[] tFields; 129 | private final String[] checkregex = {AppConstants.REGEX_SNO, AppConstants.REGEX_SNAME, 130 | AppConstants.REGEX_SEX, AppConstants.REGEX_AGE, AppConstants.REGEX_SDEPT, 131 | AppConstants.REGEX_USERNAME, AppConstants.REGEX_PASSWORD}; 132 | private final boolean checknull[] = {false, false, true, true, true, false, false}; 133 | 134 | public AddStudent(StudentInfo frame) { 135 | super(frame, AppConstants.ADMIN_SUTDENTINFO_ADD, true); 136 | contPanel = new JPanel(); 137 | setContentPane(contPanel); 138 | setLayout(new BorderLayout(5, 5)); 139 | setResizable(false); 140 | setLocationRelativeTo(null); 141 | setSize(310, 330); 142 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); 143 | 144 | initBtn(); 145 | initTable(); 146 | } 147 | 148 | public void initBtn() { 149 | JPanel panel = new JPanel(); 150 | JButton jb = new JButton(AppConstants.VERIFY); 151 | panel.add(jb); 152 | contPanel.add(panel, BorderLayout.SOUTH); 153 | getRootPane().setDefaultButton(jb); 154 | jb.addActionListener(new ActionListener() { 155 | 156 | @Override 157 | public void actionPerformed(ActionEvent e) { 158 | String[] info = new String[7]; 159 | for (int i = 0; i < 7; i++) { 160 | info[i] = tFields[i].getText(); 161 | } 162 | boolean isValid = true; 163 | for (int i = 0; i < 7; i++) { 164 | if (Pattern.matches(checkregex[i], info[i]) == false) { 165 | isValid = false; 166 | tFields[i].setBackground(Color.PINK); 167 | } else { 168 | tFields[i].setBackground(Color.WHITE); 169 | } 170 | if (checknull[i] && info[i].equals("")) { 171 | info[i] = null; 172 | } 173 | } 174 | if (!isValid) { 175 | return; 176 | } 177 | try { 178 | AdminDAO.getInstance().AddStudent(info); 179 | } catch (StudentExistException e1) { 180 | JOptionPane.showMessageDialog(null, AppConstants.ADMIN_SNO_EXIST_ERROR, 181 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 182 | return; 183 | } catch (UserExistException e2) { 184 | JOptionPane.showMessageDialog(null, AppConstants.ADMIN_USER_EXIST_ERROR, 185 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 186 | return; 187 | } 188 | dispose(); 189 | StudentInfo.this.genTable(); 190 | } 191 | }); 192 | } 193 | 194 | public void initTable() { 195 | JPanel panel = new JPanel(); 196 | panel.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 15)); 197 | JPanel[] panels = new JPanel[7]; 198 | JLabel[] labels = new JLabel[7]; 199 | tFields = new JTextField[7]; 200 | for (int i = 0; i < 7; i++) { 201 | panels[i] = new JPanel(); 202 | panels[i].setLayout(new GridLayout(1, 2, 5, 5)); 203 | labels[i] = new JLabel(infocolumn[i]); 204 | tFields[i] = new JTextField(10); 205 | panels[i].add(labels[i], Component.CENTER_ALIGNMENT); 206 | panels[i].add(tFields[i], Component.CENTER_ALIGNMENT); 207 | panel.add(panels[i]); 208 | } 209 | contPanel.add(panel, BorderLayout.CENTER); 210 | } 211 | } 212 | 213 | private class DelStudent extends JDialog { 214 | 215 | private static final long serialVersionUID = 1L; 216 | private JPanel contPanel; 217 | private JTextField tField; 218 | 219 | public DelStudent(StudentInfo frame) { 220 | super(frame, AppConstants.ADMIN_SUTDENTINFO_DEL, true); 221 | contPanel = new JPanel(); 222 | setContentPane(contPanel); 223 | setLayout(new BorderLayout(5, 5)); 224 | setResizable(false); 225 | setLocationRelativeTo(null); 226 | setSize(280, 120); 227 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); 228 | 229 | initBtn(); 230 | initTable(); 231 | } 232 | 233 | public void initBtn() { 234 | JPanel panel = new JPanel(); 235 | JButton jb = new JButton(AppConstants.DELETE); 236 | panel.add(jb); 237 | contPanel.add(panel, BorderLayout.SOUTH); 238 | getRootPane().setDefaultButton(jb); 239 | jb.addActionListener(new ActionListener() { 240 | 241 | @Override 242 | public void actionPerformed(ActionEvent e) { 243 | String sno = tField.getText(); 244 | if (Pattern.matches(AppConstants.REGEX_SNO, sno) == false) { 245 | tField.setBackground(Color.PINK); 246 | return; 247 | } else { 248 | tField.setBackground(Color.WHITE); 249 | } 250 | try { 251 | AdminDAO.getInstance().DelStudent(sno); 252 | } catch (StudentNotFoundException e1) { 253 | JOptionPane.showMessageDialog(null, AppConstants.ADMIN_SNO_NOTEXIST_ERROR, 254 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 255 | return; 256 | } catch (StudentSelectedCourseException e2) { 257 | JOptionPane.showMessageDialog(null, AppConstants.ADMIN_SELECTEDCOURSE_ERROR, 258 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 259 | return; 260 | } 261 | dispose(); 262 | StudentInfo.this.genTable(); 263 | } 264 | }); 265 | } 266 | 267 | public void initTable() { 268 | JPanel panel = new JPanel(); 269 | panel.setLayout(new FlowLayout(FlowLayout.LEFT, 15, 15)); 270 | JPanel panel2 = new JPanel(); 271 | panel2.setLayout(new GridLayout(1, 2, 5, 5)); 272 | JLabel label = new JLabel(AppConstants.SNO); 273 | tField = new JTextField(10); 274 | panel2.add(label, Component.CENTER_ALIGNMENT); 275 | panel2.add(tField, Component.LEFT_ALIGNMENT); 276 | panel.add(panel2); 277 | contPanel.add(panel, BorderLayout.CENTER); 278 | } 279 | } 280 | } 281 | -------------------------------------------------------------------------------- /src/com/student/view/StudentView.java: -------------------------------------------------------------------------------- 1 | package com.student.view; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.Component; 5 | import java.awt.Dimension; 6 | import java.awt.GridLayout; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.ActionListener; 9 | import java.awt.event.WindowAdapter; 10 | import java.awt.event.WindowEvent; 11 | import javax.swing.Box; 12 | import javax.swing.BoxLayout; 13 | import javax.swing.JButton; 14 | import javax.swing.JFrame; 15 | import javax.swing.JLabel; 16 | import javax.swing.JOptionPane; 17 | import javax.swing.JPanel; 18 | import javax.swing.JScrollPane; 19 | import javax.swing.JTable; 20 | import javax.swing.JTextField; 21 | import javax.swing.table.DefaultTableModel; 22 | import com.student.AppConstants; 23 | import com.student.base.BaseDAO.CourseNotFoundException; 24 | import com.student.base.BaseDAO.CourseNotSelectedException; 25 | import com.student.dao.StudentDAO; 26 | import com.student.model.Student; 27 | 28 | /** 29 | * @Description: Student Select Course View 30 | * @ClassName: StudentView 31 | * 32 | */ 33 | public class StudentView extends JFrame { 34 | 35 | private static final long serialVersionUID = 1L; 36 | private JPanel contentPane; 37 | private Student student; 38 | private JTable infotable, coursetable, scoretable, selectedtable; 39 | private static final String[] infocolumn = {AppConstants.SNO, AppConstants.SNAME, 40 | AppConstants.SEX, AppConstants.AGE, AppConstants.SDEPT}; 41 | private static final String[] coursecolumn = {AppConstants.CNO, AppConstants.CNAME, 42 | AppConstants.CREDIT, AppConstants.CDEPT, AppConstants.TNAME}; 43 | private static final String[] scorecolumn = 44 | {AppConstants.CNO, AppConstants.CNAME, AppConstants.SCORE}; 45 | private JTextField textField; 46 | 47 | 48 | public StudentView(Student student) { 49 | this.student = student; 50 | System.out.println("Student " + student.getSno() + " Login Success."); 51 | 52 | setResizable(false); 53 | setTitle(AppConstants.STUDENT_TITLE); 54 | setSize(800, 400); 55 | setLocationRelativeTo(null); 56 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); 57 | addWindowListener(new WindowAdapter() { 58 | 59 | @Override 60 | public void windowClosed(WindowEvent e) { 61 | System.out.println("Student " + student.getSno() + " Logout."); 62 | new LoginView(); 63 | } 64 | }); 65 | 66 | setVisible(true); 67 | contentPane = new JPanel(); 68 | setContentPane(contentPane); 69 | contentPane.setLayout(new BorderLayout()); 70 | 71 | JPanel btnpanel = new JPanel(); 72 | contentPane.add(btnpanel, BorderLayout.EAST); 73 | btnpanel.setLayout(new BoxLayout(btnpanel, BoxLayout.Y_AXIS)); 74 | 75 | JButton selectbtn = new JButton(AppConstants.STUDENT_SELECT); 76 | selectbtn.setAlignmentX(Component.CENTER_ALIGNMENT); 77 | JButton dropbtn = new JButton(AppConstants.STUDENT_DROP); 78 | dropbtn.setAlignmentX(Component.CENTER_ALIGNMENT); 79 | JButton closebtn = new JButton(AppConstants.STUDENT_CLOSE); 80 | closebtn.setAlignmentX(Component.CENTER_ALIGNMENT); 81 | 82 | selectbtn.addActionListener(new SelectListener()); 83 | dropbtn.addActionListener(new DropListener()); 84 | closebtn.addActionListener(new ActionListener() { 85 | 86 | @Override 87 | public void actionPerformed(ActionEvent e) { 88 | dispose(); 89 | } 90 | }); 91 | 92 | btnpanel.add(Box.createRigidArea(new Dimension(100, 50))); 93 | btnpanel.add(selectbtn); 94 | btnpanel.add(Box.createRigidArea(new Dimension(100, 50))); 95 | btnpanel.add(dropbtn); 96 | btnpanel.add(Box.createRigidArea(new Dimension(100, 50))); 97 | btnpanel.add(closebtn); 98 | 99 | JPanel centerpanel = new JPanel(); 100 | contentPane.add(centerpanel, BorderLayout.CENTER); 101 | centerpanel.setLayout(new GridLayout(2, 2, 15, 15)); 102 | 103 | initInfo(centerpanel); 104 | initCourse(centerpanel); 105 | initScore(centerpanel); 106 | initSelect(centerpanel); 107 | getRootPane().setDefaultButton(selectbtn); 108 | } 109 | 110 | private void initInfo(JPanel centerpanel) { 111 | System.err.println("Loading Student Info..."); 112 | JPanel panel = new JPanel(); 113 | centerpanel.add(panel); 114 | panel.setLayout(new BorderLayout()); 115 | 116 | JPanel label = new JPanel(); 117 | panel.add(label, BorderLayout.NORTH); 118 | label.add(new JLabel(AppConstants.STUDENT_INFO)); 119 | 120 | infotable = new JTable(); 121 | infotable.setEnabled(false); 122 | String[][] result = StudentDAO.getInstance().queryStudent(student.getSno()); 123 | 124 | // Assign the information. 125 | student.setSname(result[0][1]); 126 | student.setSex(result[0][2]); 127 | try { 128 | // Maybe the age is NULL 129 | student.setAge(Integer.parseInt(result[0][3])); 130 | } catch (NumberFormatException e) { 131 | student.setAge(-1); 132 | } 133 | student.setSdept(result[0][4]); 134 | student.setUsername(result[0][5]); 135 | 136 | initTable(infotable, result, infocolumn); 137 | JScrollPane scrollpane = new JScrollPane(infotable); 138 | infotable.getTableHeader().setReorderingAllowed(false); 139 | panel.add(scrollpane, BorderLayout.CENTER); 140 | } 141 | 142 | private void initCourse(JPanel centerpanel) { 143 | System.err.println("Loading Course Info..."); 144 | JPanel panel = new JPanel(); 145 | centerpanel.add(panel); 146 | panel.setLayout(new BorderLayout()); 147 | 148 | JPanel mainpanel = new JPanel(); 149 | panel.add(mainpanel, BorderLayout.CENTER); 150 | mainpanel.setLayout(new BorderLayout()); 151 | 152 | JPanel label = new JPanel(); 153 | mainpanel.add(label, BorderLayout.NORTH); 154 | 155 | JLabel courselabel = new JLabel(AppConstants.STUDENT_COURSE); 156 | label.add(courselabel); 157 | 158 | coursetable = new JTable(); 159 | coursetable.setEnabled(false); 160 | 161 | String[][] result = StudentDAO.getInstance().queryCourses(student.getSno()); 162 | 163 | initTable(coursetable, result, coursecolumn); 164 | JScrollPane scrollPane = new JScrollPane(coursetable); 165 | coursetable.getTableHeader().setReorderingAllowed(false); 166 | mainpanel.add(scrollPane, BorderLayout.CENTER); 167 | 168 | JPanel inputpanel = new JPanel(); 169 | panel.add(inputpanel, BorderLayout.SOUTH); 170 | 171 | inputpanel.add(new JLabel(AppConstants.STUDENT_INPUT)); 172 | textField = new JTextField(); 173 | inputpanel.add(textField); 174 | textField.setColumns(10); 175 | } 176 | 177 | 178 | private void initScore(JPanel centerpanel) { 179 | System.err.println("Loading Score Info..."); 180 | JPanel panel = new JPanel(); 181 | centerpanel.add(panel); 182 | panel.setLayout(new BorderLayout()); 183 | 184 | JPanel label = new JPanel(); 185 | panel.add(label, BorderLayout.NORTH); 186 | label.add(new JLabel(AppConstants.STUDENT_SCORE)); 187 | 188 | scoretable = new JTable(); 189 | scoretable.setEnabled(false); 190 | String[][] result = StudentDAO.getInstance().queryStuGrade(student.getSno()); 191 | 192 | initTable(scoretable, result, scorecolumn); 193 | JScrollPane scrollpane = new JScrollPane(scoretable); 194 | scoretable.getTableHeader().setReorderingAllowed(false); 195 | panel.add(scrollpane); 196 | 197 | } 198 | 199 | private void initSelect(JPanel centerpanel) { 200 | System.err.println("Loading Selected Info..."); 201 | JPanel panel = new JPanel(); 202 | centerpanel.add(panel); 203 | panel.setLayout(new BorderLayout()); 204 | 205 | JPanel label = new JPanel(); 206 | panel.add(label, BorderLayout.NORTH); 207 | label.add(new JLabel(AppConstants.STUDENT_SELECTED)); 208 | 209 | selectedtable = new JTable(); 210 | selectedtable.setEnabled(false); 211 | String[][] result = StudentDAO.getInstance().querySelectedCourse(student.getSno()); 212 | 213 | initTable(selectedtable, result, coursecolumn); 214 | JScrollPane scrollpane = new JScrollPane(selectedtable); 215 | selectedtable.getTableHeader().setReorderingAllowed(false); 216 | panel.add(scrollpane); 217 | } 218 | 219 | private void initTable(JTable jTable, String[][] result, String[] column) { 220 | jTable.setModel(new DefaultTableModel(result, column)); 221 | jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 222 | } 223 | 224 | private class SelectListener implements ActionListener { 225 | 226 | @Override 227 | public void actionPerformed(ActionEvent e) { 228 | String cno = textField.getText(); 229 | if (cno.equals("")) { 230 | JOptionPane.showMessageDialog(null, AppConstants.CNO_NULL_ERROR); 231 | return; 232 | } 233 | try { 234 | StudentDAO.getInstance().queryCourseGrade(student.getSno(), cno); 235 | JOptionPane.showMessageDialog(null, AppConstants.CNO_SELECTED_ERROR, 236 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 237 | } catch (CourseNotFoundException e1) { 238 | JOptionPane.showMessageDialog(null, AppConstants.CNO_NOT_EXIST_ERROR, 239 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 240 | } catch (CourseNotSelectedException e2) { 241 | StudentDAO.getInstance().selectCourse(student.getSno(), cno); 242 | textField.setText(null); 243 | updateTables(); 244 | System.out.println("Student " + student.getSno() + " selected course " + cno + "."); 245 | } catch (NumberFormatException e3) { 246 | JOptionPane.showMessageDialog(null, AppConstants.CNO_SELECTED_ERROR, 247 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 248 | } catch (Exception e4) { 249 | System.err.println("Unknown Error!"); 250 | } 251 | } 252 | 253 | } 254 | 255 | private class DropListener implements ActionListener { 256 | 257 | @Override 258 | public void actionPerformed(ActionEvent e) { 259 | String cno = textField.getText(); 260 | if (cno.equals("")) { 261 | JOptionPane.showMessageDialog(null, AppConstants.CNO_NULL_ERROR); 262 | return; 263 | } 264 | try { 265 | StudentDAO.getInstance().queryCourseGrade(student.getSno(), cno); 266 | JOptionPane.showMessageDialog(null, AppConstants.CNO_GRADED_ERROR, 267 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 268 | } catch (CourseNotFoundException e2) { 269 | JOptionPane.showMessageDialog(null, AppConstants.CNO_NOT_EXIST_ERROR, 270 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 271 | } catch (CourseNotSelectedException e2) { 272 | JOptionPane.showMessageDialog(null, AppConstants.CNO_NOT_SELECTED_ERROR, 273 | AppConstants.ERROR, JOptionPane.ERROR_MESSAGE); 274 | } catch (NumberFormatException e2) { 275 | StudentDAO.getInstance().dropCourse(student.getSno(), cno); 276 | textField.setText(null); 277 | updateTables(); 278 | System.out.println("Student " + student.getSno() + " droped course " + cno + "."); 279 | } catch (Exception e2) { 280 | System.err.println("Unknown Error!"); 281 | } 282 | } 283 | } 284 | 285 | private void updateTables() { 286 | initTable(coursetable, StudentDAO.getInstance().queryCourses(student.getSno()), 287 | coursecolumn); 288 | initTable(selectedtable, StudentDAO.getInstance().querySelectedCourse(student.getSno()), 289 | coursecolumn); 290 | } 291 | } 292 | --------------------------------------------------------------------------------