[Java] 学生那个选课(其二) →→→→→进入此内容的聊天室

来自 , 2020-09-21, 写在 Java, 查看 105 次.
URL http://www.code666.cn/view/06a81a4f
  1. //根据用户的时间,做出相应的反映
  2. public void actionPerformed(ActionEvent e) {
  3. String str = e.getActionCommand();
  4. //清空结果显示区中的内容,如果有的话。
  5. if ("查询".trim().equals(str)) {
  6. int k = 1;
  7. while (k < 10) {
  8. for (int i = 1; i < 7; i++) {
  9. jtSearch.setValueAt(null, k - 1, i - 1);
  10. }
  11. k++;
  12. }
  13. //调用下面的这个方法,在数据库中进行查找,并将结果显示在表格中。
  14. searchAvailableCourse();
  15. } else if ("提交".equals(str)) {
  16. //processBeforeCommit()对用户选课操作进行有效性检验;
  17. //剔除无效操作:如输入无效的课程号,或已经选择了某一课程,已经选满的6学分等各种情况
  18. boolean effect=processBeforeCommit();
  19. //如果课程存在,且该学生具有选择该课程的资格,即effect为true,进入正式提交程序(tryCommit())
  20. if(effect==true)
  21. {tryCommit();}
  22.  
  23. }
  24. }
  25.  
  26. //对用户选课操作进行有效性检验;
  27. public boolean processBeforeCommit(){
  28. //清空原结果显示区中的内容,如果有的话。
  29. int k = 1;
  30. while (k < 10) {
  31. for (int i = 1; i < 7; i++) {
  32. jtSearch.setValueAt(null, k - 1, i - 1);
  33. }
  34. k++;
  35. }
  36. //取得用户输入的课程号
  37. String userInput = jtfSelectedCourse.getText().toString().trim()
  38. .toLowerCase();
  39. //无效操作1:在数据库中的coursexx表中查询该课程号。如果不存在该课程,给出提示。
  40. String sql = "select cno from coursexx where cno=? ";
  41. boolean flagCourseExist = false;
  42. try {
  43. ps = con.prepareStatement(sql);
  44. ps.setString(1, userInput);
  45. rs = ps.executeQuery();
  46. flagCourseExist = rs.next();
  47. } catch (Exception eC) {
  48. eC.printStackTrace();
  49. }
  50. if (!flagCourseExist) {
  51. JOptionPane.showMessageDialog(null, "该课程不存在,请重新输入");
  52. return false;
  53. }
  54. //判断该学生选修课已选课程的总学分是否小于6;
  55. //无效操作2:如果已有选课记录,并且总学分大于6学分,该学生不能在选了。
  56. sql = "select sum(grade) "
  57. + "from (select x.sname , x.cno,k.grade grade "
  58. + "from coursexx k join choicesxx x "
  59. + "on k.cno=x.cno and x.sname=?) result";
  60. String grade =null;
  61. try {
  62. ps = con.prepareStatement(sql);
  63. ps.setString(1, usrName);
  64. rs = ps.executeQuery();
  65. while (rs.next()) {
  66. grade = rs.getString(1);
  67. if(grade==null){grade="0";}
  68. }
  69. } catch (Exception rrr) {
  70. rrr.printStackTrace();
  71. }
  72. System.out.println("总学分:" + grade);
  73.  
  74. if (Integer.parseInt(grade) > 6) {
  75. JOptionPane.showMessageDialog(null, "你已经选满6学分,系统将退出");
  76. this.setVisible(false);
  77. return false;
  78. }
  79. //无效操作3:课程该学生已经选择了某课程,则不能再选该课程了。
  80. sql = "select * from choicesxx where sname=? and cno=?";
  81. boolean flag = false;
  82. try {
  83. ps = con.prepareStatement(sql);
  84. ps.setString(1, this.getUsrName());
  85. ps.setString(2, userInput);
  86. rs = ps.executeQuery();
  87. flag = rs.next();
  88. } catch (Exception eaa) {
  89. eaa.printStackTrace();
  90. }
  91. if (flag) {
  92. JOptionPane.showMessageDialog(null, "你已经选择了该课程。请另选课程");
  93. return false;}
  94. //如果以上无效操作都不存在,则返回true,意为这是一个准有效操作
  95. return true;
  96. }
  97.  
  98. //对有效的提交操作的进行处理
  99. public void tryCommit() {
  100. // userInput为用户输入的课程ID.
  101. String userInput = jtfSelectedCourse.getText().toString().trim()
  102. .toLowerCase();
  103. // if course still available(count<MAX_STUDENT),save result.
  104. // else if course not available,show Message to student.
  105. String sql = "select (Max-selectedCount) as RemainedCount "
  106. + "from Coursexx where cno=?";
  107. try {
  108. ps = con.prepareStatement(sql);
  109. // 取得学生ID或名字,将课程ID存入学生选课表choicesxx
  110. ps.setString(1, userInput);
  111. rs = ps.executeQuery();
  112. ResultSetMetaData meta = rs.getMetaData();
  113. int cols = meta.getColumnCount();
  114. int RemainedCount = -1;
  115. while (rs.next()) {
  116. RemainedCount = rs.getInt(1);
  117. System.out.println("RemainedCount:" + RemainedCount);
  118. }
  119. //如果该课程还有选择的名额,提示单项选课操作成功。
  120. if (RemainedCount > 0) {
  121. // save studentId and courseId to student-course table.
  122. // this.getUsrName();userInput
  123. sql = "insert into choicesxx values(?,?)";
  124. ps = con.prepareStatement(sql);
  125. ps.setString(1, this.getUsrName());
  126. ps.setString(2, userInput);
  127. ps.executeUpdate();
  128. JOptionPane.showMessageDialog(null, "选课成功: " + this.getUsrName()
  129. + " 选了" + userInput + "." + "" + " 还有 " + RemainedCount
  130. + " 人可以选该课程。");
  131. // 更新课程中已选该课程的人数:即将可选该课程的人数减去1个人。
  132. sql = "update CourseXX set selectedCount=selectedCount+1 where cno=?";
  133. ps = con.prepareStatement(sql);
  134. ps.setString(1, userInput);
  135. ps.executeUpdate();
  136. con.commit();
  137. //如果该课程已经没有选择名额,提示重新选课
  138. }
  139. } catch (Exception es) {
  140. es.printStackTrace();
  141. try {
  142. con.rollback();
  143. } catch (Exception ey) {
  144. ey.printStackTrace();
  145. }
  146. }
  147. }

回复 "学生那个选课(其二)"

这儿你可以回复上面这条便签

captcha