Partage de technologie

Système de gestion des étudiants JavaFx MySql

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Avant-propos :

J'ai appris les bases de données javafx et mysql le mois dernier, j'ai donc écrit un système de gestion des étudiants. Parce que j'étais en train de réviser le mois dernier et que j'avais des choses à faire, j'étais assez occupé, donc je n'ai pas mis à jour le blog. C'est un peu simple, c'est à peu près la même chose. Le contenu est relativement simple, je vais donc maintenant partager avec vous mon système de gestion des étudiants. J'espère que les étudiants intéressés par cet aspect pourront m'aider.

🥰个人主页:coeur.c

🤓文章专题:système de gestion javafx+mysql

👐欢迎大家点赞👍 收藏😽

 

9a7940c627094e34946debbd787da24e.jpg

487ac90190f44b238ebafae9d457463b.png

 

Table des matières

Affichage des pages :

Création du package :

interface de connexion :

Interface de connexion étudiant :

Demande d'informations sur les étudiants :

Modifier

Enquête sur les performances des étudiants :

Interface de connexion administrateur :

Interface de gestion des informations sur les étudiants administrateurs :

Interface de gestion des informations de cours administrateur :

Interface de gestion des informations sur les scores de l'administrateur :

Modifier l'interface étudiant :

Affichage des codes :

Fenêtre principale (endView) :

Interface de connexion et fenêtre d'invite (otherView) :

étudiant:

Interface étudiant (studentView) :

Déclaration de l'étudiant Dao :

utilDao:

étudiantDao*:

administrateur:

Interface administrateur (magView) :

Outils d'administration :

étudiant (étudiantUtil):

Cours(courseUtil):

Score(scoreUtil) :

Classe d'outils de jugement (judgeUtil) :

Classe Administrateur Dao :

utilDao:

étudiantDao*:

coursDao:

scoreDao:

jugeDao:

domaine (classe d'objet) :

étudiant:

cours:

score:

fin


 

 

Affichage des pages :

Création du package :

(Ici, j'utilise certaines structures de framework MVC. Bien que je n'aie pas beaucoup de code, je pense qu'écrire de cette façon peut rendre notre code plus concis, facile à comprendre et hautement structuré)

4033c3dc2d754754a7b3f2454d5e9845.png

interface de connexion :

Concernant l'interface de connexion (à propos de l'interface de connexion, j'ai écrit quelques jugements sur la zone de saisie de texte--Le jugement de la base de données est correct et certains jugements non nuls--)

55d6138971d64cfbb14cf719a47454e5.png

Interface de connexion étudiant :

87787ba5c3924a76a98926dd8515df68.png

Demande d'informations sur les étudiants :

8e9905fc895a44c982abbc457be7a80a.png

Enquête sur les performances des étudiants :

97cefd35d8ad47399c72b79e965a7d4d.png

Interface de connexion administrateur :

a5114f0d903747b197c0ad925c424043.png

Interface de gestion des informations sur les étudiants administrateurs :

49cd3ef4185041aa8d83bb07e3cc417e.png

Interface de gestion des informations de cours administrateur :

d7d32fcf511e4703a0701d4b44141a7f.png

Interface de gestion des informations sur les scores de l'administrateur :

584f66d36cdf4bb8a2c0bb845fb20d84.png

Modifier l'interface étudiant :

(Je pensais que ces pages étaient toutes similaires, donc je montre ici uniquement une interface pour ajouter des étudiants)

9f338156d1d740fca1f060ce0ede42c8.png

 

Affichage des codes :

 

Fenêtre principale (endView) :

Concernant la fenêtre principale, j'ai défini une étape statique comme étant ma scène principale et j'ai attribué la valeur d'étape dans ma méthode de démarrage à l'étape statique. Le but de définir cette étape est d'intégrer d'autres méthodes que j'ai écrites, telles que l'interface de connexion, etc. La scène de la méthode est placée dans ma scène statique, de sorte que lorsque mon code est exécuté, ma page puisse toujours utiliser la scène principale et l'interface ne semble pas compliquée. C'est également très pratique et facile à comprendre.

  1. public class endView extends Application {
  2. public static Stage stage;
  3. public static student student0;
  4. public static course course0;
  5. public static score score0;
  6. @Override
  7. public void start(Stage stage) {
  8. endView.stage=stage;//这里将主舞台赋值给静态舞台
  9. stage.setTitle("学生管理系统");
  10. stage.setResizable(false);
  11. otherView.login();
  12. endView.stage.show();
  13. }
  14. public static void main(String[] args) {
  15. launch(args);
  16. }
  17. }

Interface de connexion et fenêtre d'invite (otherView) :

  1. //设置登录界面
  2. public static void login() {
  3. //创建网格面板
  4. GridPane gp=new GridPane();
  5. gp.setVgap(10);
  6. gp.setHgap(10);
  7. //创建标签
  8. Label idL=new Label("id");
  9. Label passwordL=new Label("密码");
  10. //创建输入框
  11. TextField tf=new TextField();
  12. PasswordField pf=new PasswordField();
  13. tf.setPromptText("请输入您的id号");
  14. pf.setPromptText("请输入6位数密码");
  15. //创建单选按钮
  16. RadioButton stuB=new RadioButton("学生");
  17. RadioButton teaB=new RadioButton("管理员");
  18. //创建单选按钮组
  19. ToggleGroup tg=new ToggleGroup();
  20. stuB.setToggleGroup(tg);
  21. teaB.setToggleGroup(tg);
  22. //创建单行面板
  23. HBox hBox1=new HBox();
  24. hBox1.setAlignment(Pos.CENTER);
  25. hBox1.setSpacing(10);
  26. hBox1.getChildren().addAll(stuB,teaB);
  27. //创建登录注册按钮
  28. Button loginB=new Button("登录");
  29. //给单选按钮组添加监听事件
  30. tg.selectedToggleProperty().addListener(((observableValue, toggle, t1) -> {
  31. if(t1.equals(stuB)){
  32. idL.setText("id");
  33. }else if(t1.equals(teaB)){
  34. idL.setText("姓名");
  35. }
  36. }));
  37. loginB.setOnAction(actionEvent -> {
  38. // 如果内容不为空
  39. try {
  40. String idS=tf.getText().trim();
  41. String passwordS=pf.getText().trim();
  42. Toggle selectedToggle = tg.getSelectedToggle(); // 获取选中的单选按钮
  43. if (selectedToggle != null && selectedToggle.equals(stuB)) { // 检查选中的按钮是否为学生按钮
  44. if (studentDao.login(idS,passwordS)) {
  45. studentView.stu_login(Integer.parseInt(idS));
  46. }else if((idS.isEmpty()||passwordS.isEmpty())){
  47. tipJframe("id或密码不能为空");
  48. }else{
  49. tipJframe("id或密码输入错误");
  50. }
  51. }
  52. else if(selectedToggle !=null && selectedToggle.equals(teaB)){
  53. if(judgeDao.magLogin(idS,passwordS)){
  54. magView.mag_login();
  55. }else if((idS.isEmpty()||passwordS.isEmpty())){
  56. tipJframe("姓名或密码不可为空");
  57. }else {
  58. tipJframe("姓名或密码输入错误");
  59. }
  60. }else {
  61. tipJframe("你是什么人");
  62. }
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. });
  67. HBox hBox2=new HBox();
  68. hBox2.setAlignment(Pos.CENTER);
  69. hBox2.setSpacing(10);
  70. hBox2.getChildren().add(loginB);
  71. //将节点添加到面板当中
  72. gp.add(idL,0,0);
  73. gp.add(passwordL,0,1);
  74. gp.add(tf,1,0);
  75. gp.add(pf,1,1);
  76. gp.add(hBox1,1,2);
  77. gp.add(hBox2,1,3);
  78. gp.setAlignment(Pos.BASELINE_LEFT);
  79. //设置面板内边距
  80. gp.setPadding(new Insets(40,20,10,70));
  81. //设置面板垂直间距
  82. gp.setVgap(16);
  83. Scene scene=new Scene(gp,360,240);
  84. endView.stage.setScene(scene);
  85. }
  86. //增加弹出界面
  87. public static void tipJframe(String str){
  88. Label label=new Label(str);
  89. Button rebackB=new Button("返回");
  90. VBox vBox=new VBox(label,rebackB);
  91. vBox.setSpacing(10);
  92. vBox.setAlignment(Pos.CENTER);
  93. Scene scene=new Scene(vBox,150,100);
  94. Stage stage1=new Stage();
  95. stage1.setScene(scene);
  96. stage1.setTitle("提示");
  97. stage1.show();
  98. rebackB.setOnAction(actionEvent -> {
  99. stage1.close();
  100. });
  101. }

étudiant:

Interface étudiant (studentView) :

Voici les trois interfaces que j'ai écrites. Par rapport à l'image de connexion des étudiants ci-dessus, elles correspondent à trois méthodes au total, bien entendu, les deux dernières méthodes (requête de score et requête d'informations) seront placées dans la première méthode stu_login. l'action du bouton moniteur et fonction

  1. //学生登录后界面
  2. public static void stu_login(int stu_Id){
  3. Label label=new Label("学生查询系统");
  4. Button informationB=new Button("个人信息");
  5. Button gradeB=new Button("成绩查询");
  6. Button returnLogin=new Button("返回登录");
  7. //设置按钮字体颜色
  8. informationB.setTextFill(Color.BLUE);
  9. gradeB.setTextFill(Color.BLUE);
  10. returnLogin.setTextFill(Color.BLUE);
  11. //设置字体样式
  12. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);
  13. label.setFont(font);
  14. informationB.setFont(font);
  15. gradeB.setFont(font);
  16. returnLogin.setFont(font);
  17. //为按钮添加监听事件
  18. //信息查询
  19. informationB.setOnAction(actionEvent -> {
  20. try {
  21. informationInquire(stu_Id);
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. });
  26. //成绩查询
  27. gradeB.setOnAction(actionEvent -> {
  28. try {
  29. gradeInquire(stu_Id);
  30. } catch (Exception e) {
  31. throw new RuntimeException(e);
  32. }
  33. });
  34. //返回登录
  35. returnLogin.setOnAction(actionEvent -> otherView.login());
  36. //添加竖直方向面板
  37. VBox vBox=new VBox(20,label,informationB,gradeB,returnLogin);
  38. vBox.setAlignment(Pos.CENTER);
  39. Scene scene=new Scene(vBox,400,300);
  40. endView.stage.setScene(scene);
  41. }
  42. //学生信息查询
  43. public static void informationInquire(int stu_Id) throws Exception {
  44. //调用inquiry的返回值student
  45. student student= studentDao.returnStudent(stu_Id);
  46. Label idL=new Label("学号:"+student.getStu_id());
  47. Label classL=new Label("班级:"+student.getStu_class());
  48. Label nameL=new Label("姓名:"+student.getStu_name());
  49. Label genderL=new Label("性别:"+student.getStu_gender());
  50. Label birthL=new Label("出生日期:"+student.getStu_birth());
  51. Label majorL=new Label("专业:"+student.getStu_major());
  52. //设置字体样式
  53. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);
  54. idL.setFont(font);
  55. classL.setFont(font);
  56. nameL.setFont(font);
  57. genderL.setFont(font);
  58. birthL.setFont(font);
  59. majorL.setFont(font);
  60. Button backB=new Button("返回");
  61. backB.setOnAction(actionEvent -> {
  62. stu_login(stu_Id);
  63. });
  64. VBox vBox=new VBox(10);
  65. vBox.getChildren().addAll(idL,classL,nameL,genderL,birthL,majorL,backB);
  66. vBox.setAlignment(Pos.CENTER);
  67. vBox.setSpacing(10);
  68. Scene scene=new Scene(vBox,360,340);
  69. endView.stage.setScene(scene);
  70. }
  71. //学生成绩查询
  72. public static void gradeInquire(int stu_Id) throws Exception {
  73. int grade=studentDao.gradeOneself(stu_Id);
  74. Label gradeL=new Label("您的总学分为:"+grade);
  75. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);
  76. gradeL.setFont(font);
  77. VBox vBox=new VBox();
  78. vBox.setAlignment(Pos.CENTER);
  79. vBox.setPadding(new Insets(0,0,20,0));
  80. Button back=new Button("返回");
  81. back.setMinWidth(50);
  82. back.setMinHeight(30);
  83. back.setOnAction(actionEvent -> {
  84. stu_login(stu_Id);
  85. });
  86. vBox.getChildren().addAll(gradeL,back);
  87. Scene scene=new Scene(vBox,340,150);
  88. endView.stage.setScene(scene);
  89. }

Déclaration de l'étudiant Dao :

utilDao:

  1. private static final String URL = "jdbc:mysql:///studentMs";
  2. private static final String USER = "root";
  3. private static final String PASSWORD = "123321";
  4. //获取连接对象
  5. public static Connection getCon() {
  6. try {
  7. return DriverManager.getConnection(URL, USER, PASSWORD);
  8. } catch (SQLException e) {
  9. e.printStackTrace();
  10. return null;
  11. }
  12. }
  13. //关闭资源
  14. public static void close(ResultSet rs, PreparedStatement ps, Connection con) {
  15. try {
  16. if (rs != null) {
  17. rs.close();
  18. }
  19. if (ps != null) {
  20. ps.close();
  21. }
  22. if (con != null) {
  23. con.close();
  24. }
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. //执行mysql
  30. public static boolean exeUpdate(String sql, Object... params) {
  31. //获取连接对象
  32. Connection con = getCon();
  33. PreparedStatement ps = null;
  34. try {
  35. //获取编译对象
  36. ps = con.prepareStatement(sql);
  37. //判断参数是否为空
  38. if (Objects.nonNull(params)) {
  39. for (int i = 0; i < params.length; i++) {
  40. //实现占位赋值
  41. ps.setObject(i + 1, params[i]);
  42. }
  43. }
  44. //执行更新
  45. return ps.executeUpdate() > 0;
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. } finally {
  49. close(null, ps, con);
  50. }
  51. return false;
  52. }

étudiantDao*:

au-dessus deSeules les déclarations jdbc concernant la connexion des étudiants sont affichées, pas toutes les déclarations de mon studentDao, ceci est écrit pour rendre l'idée plus claire. Si vous pensez que c'est très simple, vous pouvez voir mon code total ci-dessous.

  1. //学生登录判断
  2. public static boolean login(String idStr, String passwordStr) throws Exception {
  3. String sql = "select * from student where stuID =? and stuPassword=?";
  4. PreparedStatement ps = utilDao.getCon().prepareStatement(sql);
  5. ps.setString(1,idStr);
  6. ps.setString(2, passwordStr);
  7. ResultSet rs = ps.executeQuery();
  8. if (rs.next()) {
  9. utilDao.close(rs,ps, utilDao.getCon());
  10. return true;
  11. } else {
  12. utilDao.close(rs,ps,utilDao.getCon());
  13. return false;
  14. }
  15. }
  16. //学生查询自己成绩
  17. public static Integer gradeOneself(int stu_id) throws Exception {
  18. String sql = "SELECT SUM(credit) AS gradesum FROM score WHERE stuID=?";
  19. try (Connection connection =utilDao.getCon();
  20. PreparedStatement ps = connection.prepareStatement(sql)) {
  21. ps.setInt(1, stu_id);
  22. try (ResultSet rs = ps.executeQuery()) {
  23. if (rs.next()) {
  24. int gradesum = rs.getInt("gradesum");
  25. return gradesum;
  26. } else {
  27. // 如果没有查询到结果,可以返回 null 或者其他合适的值
  28. return null;
  29. }
  30. }
  31. }
  32. }
  33. //返回学生信息
  34. public static student returnStudent(int stu_id)throws Exception{
  35. student student=new student();
  36. String sql="select stuID,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword from student where stuID=?";
  37. PreparedStatement ps=utilDao.getCon().prepareStatement(sql);
  38. //过滤其他的只留下ID为1的学生
  39. ps.setString(1,String.valueOf(stu_id));
  40. ResultSet rs=ps.executeQuery();
  41. if(rs.next()){
  42. student.setStu_id(rs.getInt("stuID"));
  43. student.setStu_class(rs.getString("stuClass"));
  44. student.setStu_name(rs.getString("stuName"));
  45. student.setStu_gender(rs.getString("stuSex"));
  46. student.setStu_birth(rs.getString("stuBirth"));
  47. student.setStu_major(rs.getString("stuMajor"));
  48. student.setStu_password(rs.getString("stuPassword"));
  49. }
  50. rs.close();
  51. ps.close();
  52. return student;
  53. }

---Voici le code de connexion des étudiants---

administrateur:

Interface administrateur (magView) :

Voici ma méthode d'interface pour les paramètres d'administrateur. Il y a quatre interfaces principales d'administrateur au total, qui correspondent aux quatre premières connexions d'administrateur dans l'affichage de la page. Il y a 7 méthodes au total ci-dessous concernant la méthode createTableView, je l'ai écrite et utilisée. le mien. L'objet tel que student est ajouté à une partie du code dans la tableview. Il supprime et redéfinit simplement une méthode. Vous n'avez pas besoin de vous en soucier. Par conséquent, les quatre autres méthodes de création de l'interface correspondent simplement. les quatre premières connexions d'administrateur dans l'affichage de la page. Les quatre premières méthodes sont utilisées pour créer l'interface. L'une est la méthode d'interface après la connexion, et les trois dernières sont les méthodes appelées par la surveillance des actions des trois premiers boutons du panneau après. se connecter.

  1. //管理员登录界面
  2. public static void mag_login(){
  3. Label magL=new Label("管理员查询系统");
  4. VBox vBox=new VBox();
  5. vBox.setAlignment(Pos.CENTER);
  6. vBox.setSpacing(30);
  7. Button informationB= new Button("查询学生信息");
  8. Button courseB=new Button("查看课程");
  9. Button gradeB=new Button("查看学生成绩");
  10. Button rebackB=new Button("返回登录界面");
  11. rebackB.setOnAction(actionEvent -> {
  12. otherView.login();
  13. });
  14. informationB.setOnAction(actionEvent -> {
  15. try {
  16. magStudent();
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. });
  21. courseB.setOnAction(actionEvent -> {
  22. try {
  23. magCourse();
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. });
  28. gradeB.setOnAction(actionEvent -> {
  29. try {
  30. magScore();
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. });
  35. rebackB.setOnAction(actionEvent -> {
  36. try {
  37. otherView.login();
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. });
  42. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);
  43. magL.setFont(font);
  44. informationB.setFont(font);
  45. courseB.setFont(font);
  46. gradeB.setFont(font);
  47. rebackB.setFont(font);
  48. magL.setTextFill(Color.BLACK);
  49. informationB.setTextFill(Color.BLUE);
  50. courseB.setTextFill(Color.BLUE);
  51. gradeB.setTextFill(Color.BLUE);
  52. rebackB.setTextFill(Color.BLUE);
  53. vBox.getChildren().addAll(magL,informationB,courseB,gradeB,rebackB);
  54. Scene scene=new Scene(vBox,360,400);
  55. endView.stage.setScene(scene);
  56. }
  57. //管理员查看学生信息界面
  58. public static void magStudent() throws Exception {
  59. BorderPane bp=new BorderPane();
  60. TableView<student> tableView = createTableView1();
  61. Label studentL=new Label("学生信息管理");
  62. Button addB=new Button("添加学生");
  63. Button updateB=new Button("修改学生");
  64. Button deleteB=new Button("删除学生");
  65. Button backB=new Button("返回");
  66. addB.setOnAction(actionEvent -> {
  67. try {
  68. studentUtil.studentAdd();
  69. } catch (Exception e) {
  70. throw new RuntimeException(e);
  71. }
  72. });
  73. updateB.setOnAction(actionEvent -> {
  74. try {
  75. studentUtil.studentUpdate();
  76. } catch (Exception e) {
  77. throw new RuntimeException(e);
  78. }
  79. });
  80. deleteB.setOnAction(actionEvent -> {
  81. try {
  82. studentUtil.studentDelete();
  83. } catch (Exception e) {
  84. throw new RuntimeException(e);
  85. }
  86. });
  87. backB.setOnAction(actionEvent -> {
  88. mag_login();
  89. });
  90. addB.setMinWidth(70);
  91. updateB.setMinWidth(70);
  92. deleteB.setMinWidth(70);
  93. addB.setMinHeight(35);
  94. updateB.setMinHeight(35);
  95. deleteB.setMinHeight(35);
  96. addB.setTextFill(Color.INDIANRED);
  97. updateB.setTextFill(Color.INDIANRED);
  98. deleteB.setTextFill(Color.INDIANRED);
  99. backB.setTextFill(Color.INDIANRED);
  100. addB.setBorder(Border.stroke(Color.LIGHTPINK));
  101. updateB.setBorder(Border.stroke(Color.LIGHTPINK));
  102. deleteB.setBorder(Border.stroke(Color.LIGHTPINK));
  103. backB.setBorder(Border.stroke(Color.LIGHTPINK));
  104. backB.setOnAction(actionEvent -> {
  105. mag_login();
  106. });
  107. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);
  108. studentL.setFont(font);
  109. studentL.setTextFill(Color.RED);
  110. VBox vBox=new VBox();
  111. vBox.getChildren().addAll(studentL,addB,updateB,deleteB,backB);
  112. vBox.setAlignment(Pos.CENTER);
  113. vBox.setSpacing(20);
  114. vBox.setPadding(new Insets(50));
  115. BorderPane bp1=new BorderPane();
  116. bp1.setTop(vBox);
  117. BorderPane bp2=new BorderPane();
  118. bp2.setRight(tableView);
  119. // 获取学生列表,将集合转换成ObservableList
  120. ArrayList<student> students = studentDao.initStudent();
  121. // 将学生列表转换为ObservableList
  122. ObservableList<student> stuObs = FXCollections.observableArrayList(students);
  123. // 将 ObservableList 设置为 TableView 的数据源
  124. tableView.setItems(stuObs);
  125. bp.setLeft(bp1);
  126. bp.setCenter(bp2);
  127. Scene scene = new Scene(bp, 650, 350);
  128. endView.stage.setScene(scene);
  129. }
  130. //绑定学生面板
  131. public static TableView<student> createTableView1() {
  132. TableView<student> tableView = new TableView<>();
  133. TableColumn<student, Integer> idCol = new TableColumn<>("ID");
  134. idCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getStu_id()).asObject());
  135. TableColumn<student, String> classCol = new TableColumn<>("班级");
  136. classCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStu_class()));
  137. TableColumn<student, String> nameCol = new TableColumn<>("姓名");
  138. nameCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStu_name()));
  139. TableColumn<student, String> sexCol = new TableColumn<>("性别");
  140. sexCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStu_gender()));
  141. TableColumn<student, String> birthCol = new TableColumn<>("出生日期");
  142. birthCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStu_birth()));
  143. TableColumn<student, String> majorCol = new TableColumn<>("所在班级");
  144. majorCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStu_major()));
  145. // 添加其他表格列...
  146. tableView.getColumns().addAll(idCol, classCol, nameCol,sexCol,birthCol,majorCol);
  147. return tableView;
  148. }
  149. //管理员查看课程界面
  150. public static void magCourse() throws Exception {
  151. //创建面板
  152. BorderPane bp=new BorderPane();
  153. //创建第二面板
  154. TableView<course> tableView = createTableView2();
  155. Label studentL=new Label("学生课程管理");
  156. Button addB=new Button("添加课程");
  157. Button updateB=new Button("修改课程");
  158. Button deleteB=new Button("删除课程");
  159. Button backB=new Button("返回");
  160. addB.setOnAction(actionEvent -> {
  161. try {
  162. courseUtil.courseAdd();
  163. } catch (Exception e) {
  164. throw new RuntimeException(e);
  165. }
  166. });
  167. updateB.setOnAction(actionEvent -> {
  168. courseUtil.courseUpdate();
  169. });
  170. deleteB.setOnAction(actionEvent -> {
  171. courseUtil.courseDelete();
  172. });
  173. backB.setOnAction(actionEvent -> {
  174. mag_login();
  175. });
  176. addB.setMinWidth(70);
  177. updateB.setMinWidth(70);
  178. deleteB.setMinWidth(70);
  179. addB.setMinHeight(35);
  180. updateB.setMinHeight(35);
  181. deleteB.setMinHeight(35);
  182. addB.setTextFill(Color.INDIANRED);
  183. updateB.setTextFill(Color.INDIANRED);
  184. deleteB.setTextFill(Color.INDIANRED);
  185. backB.setTextFill(Color.INDIANRED);
  186. addB.setBorder(Border.stroke(Color.LIGHTPINK));
  187. updateB.setBorder(Border.stroke(Color.LIGHTPINK));
  188. deleteB.setBorder(Border.stroke(Color.LIGHTPINK));
  189. backB.setBorder(Border.stroke(Color.LIGHTPINK));
  190. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);
  191. studentL.setFont(font);
  192. studentL.setTextFill(Color.RED);
  193. VBox vBox=new VBox();
  194. vBox.getChildren().addAll(studentL,addB,updateB,deleteB,backB);
  195. vBox.setAlignment(Pos.CENTER);
  196. vBox.setSpacing(20);
  197. vBox.setPadding(new Insets(50));
  198. BorderPane bp1=new BorderPane();
  199. bp1.setTop(vBox);
  200. BorderPane bp2=new BorderPane();
  201. bp2.setRight(tableView);
  202. // 获取学生列表
  203. ArrayList<course> courses = courseDao.initCourse();
  204. // 将学生列表转换为ObservableList
  205. ObservableList<course> stuObs = FXCollections.observableArrayList(courses);
  206. // 将 ObservableList 设置为 TableView 的数据源
  207. tableView.setItems(stuObs);
  208. bp.setLeft(bp1);
  209. bp.setCenter(bp2);
  210. Scene scene = new Scene(bp, 700, 350);
  211. endView.stage.setScene(scene);
  212. }
  213. //绑定课程面板
  214. public static TableView<course> createTableView2() {
  215. TableView<course> tableView = new TableView<>();
  216. TableColumn<course, Integer> idCol = new TableColumn<>("课程号");
  217. idCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getCou_id()).asObject());
  218. TableColumn<course, String> majorCol = new TableColumn<>("所属专业");
  219. majorCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCou_major()));
  220. TableColumn<course, String> nameCol = new TableColumn<>("课程名称");
  221. nameCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCou_name()));
  222. TableColumn<course, String> typeCol = new TableColumn<>("课程类型");
  223. typeCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCou_type()));
  224. TableColumn<course, String> beginCol = new TableColumn<>("开课学期");
  225. beginCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCou_beginTime()));
  226. TableColumn<course, Integer> studyCol = new TableColumn<>("学时数");
  227. studyCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getCou_studyTime()).asObject());
  228. TableColumn<course, Integer> scoreCol = new TableColumn<>("学分");
  229. scoreCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getCou_score()).asObject());
  230. tableView.getColumns().addAll(idCol, majorCol, nameCol,typeCol,beginCol,studyCol,scoreCol);
  231. return tableView;
  232. }
  233. //管理员查看学生成绩界面
  234. public static void magScore() throws Exception {
  235. BorderPane bp=new BorderPane();
  236. TableView<score> tableView = createTableView3();
  237. Label studentL=new Label("学生成绩管理");
  238. Button addB=new Button("添加成绩");
  239. Button updateB=new Button("修改成绩");
  240. Button deleteB=new Button("删除成绩");
  241. Button rebackB=new Button("返回");
  242. addB.setOnAction(actionEvent -> {
  243. scoreUtil.scoreAdd();
  244. });
  245. updateB.setOnAction(actionEvent -> {
  246. scoreUtil.scoreUpdate();
  247. });
  248. deleteB.setOnAction(actionEvent -> {
  249. scoreUtil.scoreDelete();
  250. });
  251. rebackB.setOnAction(actionEvent -> {
  252. mag_login();
  253. });
  254. addB.setMinWidth(70);
  255. updateB.setMinWidth(70);
  256. deleteB.setMinWidth(70);
  257. addB.setMinHeight(35);
  258. updateB.setMinHeight(35);
  259. deleteB.setMinHeight(35);
  260. addB.setTextFill(Color.INDIANRED);
  261. updateB.setTextFill(Color.INDIANRED);
  262. deleteB.setTextFill(Color.INDIANRED);
  263. rebackB.setTextFill(Color.INDIANRED);
  264. addB.setBorder(Border.stroke(Color.LIGHTPINK));
  265. updateB.setBorder(Border.stroke(Color.LIGHTPINK));
  266. deleteB.setBorder(Border.stroke(Color.LIGHTPINK));
  267. rebackB.setBorder(Border.stroke(Color.LIGHTPINK));
  268. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);
  269. studentL.setFont(font);
  270. studentL.setTextFill(Color.RED);
  271. VBox vBox=new VBox();
  272. vBox.getChildren().addAll(studentL,addB,updateB,deleteB,rebackB);
  273. vBox.setAlignment(Pos.CENTER);
  274. vBox.setSpacing(10);
  275. vBox.setPadding(new Insets(40));
  276. BorderPane bp1=new BorderPane();
  277. bp1.setTop(vBox);
  278. BorderPane bp2=new BorderPane();
  279. bp2.setRight(tableView);
  280. // 获取学生列表
  281. ArrayList<score> scores = scoreDao.initScore();
  282. // 将学生列表转换为ObservableList
  283. ObservableList<score> stuObs = FXCollections.observableArrayList(scores);
  284. // 将 ObservableList 设置为 TableView 的数据源
  285. tableView.setItems(stuObs);
  286. bp.setLeft(bp1);
  287. bp.setCenter(bp2);
  288. Scene scene = new Scene(bp, 450, 300);
  289. endView.stage.setScene(scene);
  290. }
  291. //绑定成绩面板
  292. public static TableView<score> createTableView3() {
  293. TableView<score> tableView = new TableView<>();
  294. TableColumn<score, Integer> stuCol = new TableColumn<>("学号");
  295. stuCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getScore_id()).asObject());
  296. TableColumn<score, Integer> idCol = new TableColumn<>("课程号");
  297. idCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getScore_cid()).asObject());
  298. TableColumn<score, String> studyCol = new TableColumn<>("学数");
  299. studyCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getSocre_score()));
  300. TableColumn<score, String> scoreCol = new TableColumn<>("学分");
  301. scoreCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getScore_credit()));
  302. tableView.getColumns().addAll(stuCol,idCol,studyCol,scoreCol);
  303. return tableView;
  304. }
  305. }

Outils d'administration :

La classe de l'outil administrateur permet d'ajouter des étudiants, de modifier des étudiants, de supprimer des étudiants, d'ajouter des cours... dans la page de fonctionnement.


étudiant (étudiantUtil):

  1. //添加学生信息
  2. public static void studentAdd() throws Exception {
  3. //添加面板
  4. GridPane gp=new GridPane();
  5. Label idL=new Label("学号");
  6. Label classL=new Label("班级");
  7. Label nameL=new Label("姓名");
  8. Label sexL=new Label("性别");
  9. Label birthL=new Label("出生日期");
  10. Label majorL=new Label("所在专业");
  11. Label passwordL=new Label("密码");
  12. //添加按钮
  13. Button admitB=new Button("提交");
  14. Button backB=new Button("返回");
  15. //设置字体样式
  16. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);
  17. idL.setFont(font);
  18. classL.setFont(font);
  19. nameL.setFont(font);
  20. sexL.setFont(font);
  21. birthL.setFont(font);
  22. majorL.setFont(font);
  23. //添加输入框
  24. TextField idT=new TextField(String.valueOf(judgeDao.maxID()+1));
  25. TextField classT=new TextField();
  26. TextField nameT=new TextField();
  27. TextField sexT=new TextField();
  28. TextField birthT=new TextField();
  29. TextField majorT=new TextField();
  30. TextField passwordT=new TextField();
  31. //将组件添加到面板当中
  32. gp.add(idL,0,0);
  33. gp.add(idT,1,0);
  34. gp.add(classL,0,1);
  35. gp.add(classT,1,1);
  36. gp.add(nameL,0,2);
  37. gp.add(nameT,1,2);
  38. gp.add(sexL,0,3);
  39. gp.add(sexT,1,3);
  40. gp.add(birthL,0,4);
  41. gp.add(birthT,1,4);
  42. gp.add(majorL,0,5);
  43. gp.add(majorT,1,5);
  44. gp.add(passwordL,0,6);
  45. gp.add(passwordT,1,6);
  46. gp.add(admitB,0,7);
  47. gp.add(backB,1,7);
  48. //设置面板样式
  49. gp.setPadding(new Insets(30));
  50. gp.setVgap(20);
  51. gp.setHgap(10);
  52. Scene scene=new Scene(gp,300,400);
  53. Stage stage=new Stage();
  54. stage.setScene(scene);
  55. stage.setTitle("添加学生信息");
  56. stage.show();
  57. backB.setOnAction(actionEvent -> {
  58. try {
  59. stage.close();
  60. } catch (Exception e) {
  61. throw new RuntimeException(e);
  62. }
  63. });
  64. //给提交按钮添加监听事件
  65. admitB.setOnAction(actionEvent -> {
  66. try {
  67. // 获取文本框中的内容并去除首尾空白
  68. String id = idT.getText().trim();
  69. String clazz = classT.getText().trim();
  70. String name = nameT.getText().trim();
  71. String sex = sexT.getText().trim();
  72. String birth = birthT.getText().trim();
  73. String major = majorT.getText().trim();
  74. String password = passwordT.getText().trim();
  75. // 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作
  76. if (id.isEmpty() || clazz.isEmpty() || name.isEmpty() || sex.isEmpty() || birth.isEmpty() || major.isEmpty() || password.isEmpty()) {
  77. otherView.tipJframe("字段不能为空");
  78. return;
  79. }else if(!((sex.equals("男")||sex.equals("女")))){
  80. otherView.tipJframe("请输入正常性别");
  81. return;
  82. }else if(!judgeUtil.isValidDate(birth)){
  83. otherView.tipJframe("日期格式不正确,请重新输入");
  84. return;
  85. }else if(!clazz.equals(judgeUtil.isClass(id))){
  86. otherView.tipJframe("班级格式不正确,请重新输入");
  87. return;
  88. }else if(!password.equals(judgeUtil.isPassword(id))){
  89. otherView.tipJframe("密码格式错误,请重新输入");
  90. return;
  91. }
  92. int studentId = Integer.parseInt(id);
  93. // 调用Dao层方法尝试添加学生信息
  94. if (studentDao.addStudent(studentId, clazz, name, sex, birth, major, password)) {
  95. // 添加成功后的操作
  96. studentDao.initStudent();
  97. magView.magStudent();
  98. otherView.tipJframe("添加成功");
  99. }else {
  100. otherView.tipJframe("添加失败");
  101. }
  102. } catch (Exception e) {
  103. // 捕获其他异常并打印堆栈信息
  104. e.printStackTrace();
  105. otherView.tipJframe("操作失败:" + e.getMessage()); // 提示操作失败,并显示具体的异常信息
  106. }
  107. });
  108. }
  109. //修改学生信息
  110. public static void studentUpdate(){
  111. //创建第一个面板
  112. GridPane gp = new GridPane();
  113. Label idl = new Label("ID");
  114. TextField idt = new TextField();
  115. //创建第一个界面的按钮
  116. Button admitb = new Button("提交");
  117. Button backb = new Button("返回");
  118. gp.add(idl,0,0);
  119. gp.add(idt,1,0);
  120. gp.add(admitb,0,1);
  121. gp.add(backb,0,2);
  122. gp.setHgap(20);
  123. gp.setVgap(10);
  124. gp.setAlignment(Pos.CENTER);
  125. Scene scene=new Scene(gp,240,140);
  126. //创建第一个舞台
  127. Stage stage=new Stage();
  128. stage.setScene(scene);
  129. stage.setResizable(false);
  130. stage.setTitle("修改学生信息");
  131. stage.show();
  132. backb.setOnAction(actionEvent -> {
  133. try {
  134. stage.close();
  135. } catch (Exception e) {
  136. throw new RuntimeException(e);
  137. }
  138. });
  139. // 创建第二个舞台
  140. Stage stage1 = new Stage();
  141. //创建只能存储一个字符的字符串
  142. final int[] id = new int[1];
  143. final String[] clas = new String[1];
  144. final String[] names = new String[1];
  145. final String[] sexs = new String[1];
  146. final String[] births = new String[1];
  147. final String[] majors = new String[1];
  148. final String[] passwords=new String[1];
  149. GridPane gp1=new GridPane();
  150. Label idL=new Label("学号");
  151. Label classL=new Label("班级");
  152. Label nameL=new Label("姓名");
  153. Label sexL=new Label("性别");
  154. Label birthL=new Label("出生日期");
  155. Label majorL=new Label("所在专业");
  156. Label passwordL=new Label("密码");
  157. Button admitB=new Button("提交");
  158. Button rebackB=new Button("返回");
  159. rebackB.setOnAction(actionEvent -> {
  160. stage1.close();
  161. });
  162. //创建显示信息文本框
  163. TextField idT=new TextField(String.valueOf(id[0]));
  164. TextField classT=new TextField(clas[0]);
  165. TextField nameT=new TextField(names[0]);
  166. TextField sexT=new TextField(sexs[0]);
  167. TextField birthT=new TextField(births[0]);
  168. TextField majorT=new TextField(majors[0]);
  169. TextField passwordT=new TextField(passwords[0]);
  170. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);
  171. idL.setFont(font);
  172. classL.setFont(font);
  173. nameL.setFont(font);
  174. sexL.setFont(font);
  175. birthL.setFont(font);
  176. majorL.setFont(font);
  177. gp1.add(idL,0,0);
  178. gp1.add(idT,1,0);
  179. gp1.add(classL,0,1);
  180. gp1.add(classT,1,1);
  181. gp1.add(nameL,0,2);
  182. gp1.add(nameT,1,2);
  183. gp1.add(sexL,0,3);
  184. gp1.add(sexT,1,3);
  185. gp1.add(birthL,0,4);
  186. gp1.add(birthT,1,4);
  187. gp1.add(majorL,0,5);
  188. gp1.add(majorT,1,5);
  189. gp1.add(passwordL,0,6);
  190. gp1.add(passwordT,1,6);
  191. gp1.add(admitB,0,7);
  192. gp1.add(rebackB,1,7);
  193. gp1.setPadding(new Insets(30));
  194. gp1.setVgap(20);
  195. gp1.setHgap(10);
  196. Scene scene1=new Scene(gp1,300,400);
  197. stage1.setScene(scene1);
  198. stage1.setResizable(false);
  199. stage1.setTitle("修改学生信息");
  200. stage1.close();
  201. //第一个界面的提交按钮
  202. admitb.setOnAction(actionEvent -> {
  203. String idText = idt.getText().trim();
  204. if (!idText.isEmpty()) {
  205. try {
  206. if (studentDao.isStudent(Integer.parseInt(idText))) {
  207. endView.student0=studentDao.returnStudent(Integer.parseInt(idText));
  208. id[0] = endView.student0.getStu_id();
  209. clas[0] = endView.student0.getStu_class();
  210. names[0] = endView.student0.getStu_name();
  211. sexs[0] = endView.student0.getStu_gender();
  212. births[0] = endView.student0.getStu_birth();
  213. majors[0] = endView.student0.getStu_major();
  214. passwords[0]=endView.student0.getStu_password();
  215. // 将值设置到文本框中
  216. idT.setText(String.valueOf(id[0]));
  217. classT.setText(clas[0]);
  218. nameT.setText(names[0]);
  219. sexT.setText(sexs[0]);
  220. birthT.setText(births[0]);
  221. majorT.setText(majors[0]);
  222. passwordT.setText(passwords[0]);
  223. // 显示舞台
  224. stage1.show();
  225. } else {
  226. otherView.tipJframe("不存在该学生");
  227. }
  228. } catch (Exception e) {
  229. throw new RuntimeException(e);
  230. }
  231. } else {
  232. otherView.tipJframe("ID不能为空");
  233. }
  234. });
  235. //第二个界面的提交按钮
  236. admitB.setOnAction(actionEvent -> {
  237. try {
  238. String idd = idT.getText().trim();
  239. String clazz = classT.getText().trim();
  240. String name = nameT.getText().trim();
  241. String sex = sexT.getText().trim();
  242. String birth = birthT.getText().trim();
  243. String major = majorT.getText().trim();
  244. String password=passwordT.getText().trim();
  245. // 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作
  246. if (idd.isEmpty() || clazz.isEmpty() || name.isEmpty() || sex.isEmpty() || birth.isEmpty() || major.isEmpty()) {
  247. otherView.tipJframe("所有字段不能为空");
  248. return;
  249. }else if(!((sex.equals("男")||sex.equals("女")))){
  250. otherView.tipJframe("请输入正常性别");
  251. return;
  252. }else if(!judgeUtil.isValidDate(birth)){
  253. otherView.tipJframe("日期格式不正确,请重新输入");
  254. return;
  255. }else if(!clazz.equals(judgeUtil.isClass(idd))){
  256. otherView.tipJframe("班级格式不正确,请重新输入");
  257. return;
  258. }else if(!password.equals(judgeUtil.isPassword(idd))){
  259. otherView.tipJframe("密码格式错误,请重新输入");
  260. return;
  261. }
  262. int studentId = Integer.parseInt(idd);
  263. //判断如果学号发生改变,则进行添加学生操作,否者进行修改操作
  264. //如果发生修改
  265. if(id[0]!=Integer.valueOf(idT.getText().trim())){
  266. if (studentDao.addStudent(studentId, clazz, name, sex, birth, major,password)) {
  267. // 添加成功后的操作
  268. if(studentDao.deleteStudent(id[0])){
  269. studentDao.initStudent();
  270. magView.magStudent();
  271. otherView.tipJframe("修改成功");
  272. }
  273. } else {
  274. otherView.tipJframe("修改失败");
  275. }
  276. }
  277. //如果没有发生修改,那么进行修改操作
  278. else {
  279. if (studentDao.updateStudent(studentId, clazz, name, sex, birth, major,password)) {
  280. // 添加成功后的操作
  281. studentDao.initStudent();
  282. magView.magStudent();
  283. otherView.tipJframe("修改成功");
  284. } else {
  285. otherView.tipJframe("修改失败");
  286. }
  287. }
  288. } catch (Exception e) {
  289. e.printStackTrace();
  290. otherView.tipJframe("操作失败:" + e.getMessage());
  291. }
  292. });
  293. }
  294. //删除学生信息
  295. public static void studentDelete(){
  296. GridPane gp = new GridPane();
  297. Label idL = new Label("ID");
  298. TextField idT = new TextField();
  299. Button admitB = new Button("提交");
  300. Button backB = new Button("返回");
  301. // 将按钮事件处理移到这里,确保在点击按钮时获取最新的文本框内容
  302. gp.add(idL, 0, 0);
  303. gp.add(idT, 1, 0);
  304. gp.add(admitB, 0, 1);
  305. gp.add(backB, 1, 1);
  306. gp.setAlignment(Pos.CENTER);
  307. gp.setHgap(10);
  308. gp.setVgap(30);
  309. //创建一个删除学生的界面舞台
  310. Stage stage = new Stage();
  311. Scene scene = new Scene(gp, 240, 100);
  312. stage.setScene(scene);
  313. stage.setTitle("删除学生信息");
  314. stage.setResizable(false);
  315. stage.show();
  316. //如果舞台退出,则界面关闭
  317. backB.setOnAction(actionEvent -> {
  318. try {
  319. stage.close();
  320. } catch (Exception e) {
  321. throw new RuntimeException(e);
  322. }
  323. });
  324. //对按钮添加点击事件
  325. admitB.setOnAction(actionEvent -> {
  326. String ids = idT.getText().trim();
  327. if (!ids.isEmpty()) {
  328. try {
  329. int id = Integer.parseInt(ids);
  330. if (studentDao.isStudent(id)) {
  331. if (studentDao.deleteStudent(id)) {
  332. otherView.tipJframe("学生删除成功");
  333. //更新数据库信息
  334. studentDao.initStudent();
  335. //更新面板信息
  336. magView.magStudent();
  337. } else {
  338. otherView.tipJframe("学生删除失败");
  339. }
  340. } else {
  341. otherView.tipJframe("未找到该学生");
  342. }
  343. } catch (NumberFormatException e) {
  344. otherView.tipJframe("请输入有效的数字ID");
  345. } catch (Exception e) {
  346. e.printStackTrace();
  347. otherView.tipJframe("数据库操作失败");
  348. }
  349. } else {
  350. otherView.tipJframe("请输入学生ID");
  351. }
  352. });
  353. }

Cours(courseUtil):

  1. //添加课程
  2. public static void courseAdd() throws Exception {
  3. //创建面板
  4. GridPane gp=new GridPane();
  5. Label cIDL=new Label("课程号");
  6. Label cMajorL=new Label("所属专业");
  7. Label cNameL=new Label("课程名称");
  8. Label cTypeL=new Label("课程类型");
  9. Label cBeginL=new Label("开课学期");
  10. Label cStudyL=new Label("学时数");
  11. Label cCreditL=new Label("学分");
  12. //创建按钮
  13. Button admitB=new Button("提交");
  14. Button backB=new Button("返回");
  15. //创建字体样式
  16. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);
  17. cIDL.setFont(font);
  18. cMajorL.setFont(font);
  19. cNameL.setFont(font);
  20. cTypeL.setFont(font);
  21. cBeginL.setFont(font);
  22. cStudyL.setFont(font);
  23. cCreditL.setFont(font);
  24. //创建文本输入框
  25. TextField cIDT=new TextField(String.valueOf(judgeDao.maxcID()+1));
  26. TextField cMajorT=new TextField();
  27. TextField cNameT=new TextField();
  28. TextField cTypeT=new TextField();
  29. TextField cBeginT=new TextField();
  30. TextField cStudyT=new TextField();
  31. TextField cCreditT=new TextField();
  32. //将节点添加到面板当中
  33. gp.add(cIDL,0,0);
  34. gp.add(cIDT,1,0);
  35. gp.add(cMajorL,0,1);
  36. gp.add(cMajorT,1,1);
  37. gp.add(cNameL,0,2);
  38. gp.add(cNameT,1,2);
  39. gp.add(cTypeL,0,3);
  40. gp.add(cTypeT,1,3);
  41. gp.add(cBeginL,0,4);
  42. gp.add(cBeginT,1,4);
  43. gp.add(cStudyL,0,5);
  44. gp.add(cStudyT,1,5);
  45. gp.add(cCreditL,0,6);
  46. gp.add(cCreditT,1,6);
  47. gp.add(admitB,0,7);
  48. gp.add(backB,1,7);
  49. //设置面板
  50. gp.setPadding(new Insets(30));
  51. gp.setVgap(20);
  52. gp.setHgap(10);
  53. Scene scene=new Scene(gp,350,400);
  54. Stage stage=new Stage();
  55. stage.setResizable(false);
  56. stage.setTitle("添加课程");
  57. stage.setScene(scene);
  58. stage.show();
  59. backB.setOnAction(actionEvent -> {
  60. try {
  61. stage.close();
  62. } catch (Exception e) {
  63. throw new RuntimeException(e);
  64. }
  65. });
  66. admitB.setOnAction(actionEvent -> {
  67. try {
  68. // 获取文本框中的内容并去除首尾空白
  69. String id = cIDT.getText().trim();
  70. String major = cMajorT.getText().trim();
  71. String name = cNameT.getText().trim();
  72. String type = cTypeT.getText().trim();
  73. String begin = cBeginT.getText().trim();
  74. String study = cStudyT.getText().trim();
  75. String credit = cCreditT.getText().trim();
  76. // 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作
  77. if ((id.isEmpty() || major.isEmpty() || name.isEmpty() || type.isEmpty() || begin.isEmpty() || study.isEmpty() || credit.isEmpty())) {
  78. otherView.tipJframe("字段不能为空");
  79. return;
  80. }
  81. // 尝试将id和password转换为整数,如果格式不正确会抛出NumberFormatException
  82. int cId = Integer.parseInt(id);
  83. int studytime=Integer.parseInt(study);
  84. int creditt=Integer.parseInt(credit);
  85. if (courseDao.addCourse(cId, major, name, type, begin, studytime, creditt)) {
  86. // 添加成功后的操作
  87. courseDao.initCourse();
  88. magView.magCourse();
  89. otherView.tipJframe("课程添加成功");
  90. } else {
  91. otherView.tipJframe("课程添加失败");
  92. }
  93. } catch (Exception e) {
  94. // 捕获其他异常并打印堆栈信息
  95. e.printStackTrace();
  96. otherView.tipJframe("操作失败:" + e.getMessage());
  97. }
  98. });
  99. }
  100. //修改课程
  101. public static void courseUpdate(){
  102. //创建第一个面板
  103. GridPane gp = new GridPane();
  104. Label idl = new Label("cID");
  105. TextField idt = new TextField();
  106. //创建第一个界面的按钮
  107. Button admitb = new Button("提交");
  108. Button backB = new Button("返回");
  109. gp.add(idl,0,0);
  110. gp.add(idt,1,0);
  111. gp.add(admitb,0,1);
  112. gp.add(backB,0,2);
  113. gp.setHgap(20);
  114. gp.setVgap(10);
  115. gp.setAlignment(Pos.CENTER);
  116. Scene scene=new Scene(gp,250,140);
  117. Stage stage=new Stage();
  118. stage.setTitle("修改课程");
  119. stage.setScene(scene);
  120. stage.show();
  121. backB.setOnAction(actionEvent -> {
  122. try {
  123. stage.close();
  124. } catch (Exception e) {
  125. throw new RuntimeException(e);
  126. }
  127. });
  128. //第二个舞台
  129. Stage stage1 = new Stage();
  130. //创建只能存储一个字符串的数组
  131. final int[] id = new int[1];
  132. final String[] major = new String[1];
  133. final String[] name = new String[1];
  134. final String[] type = new String[1];
  135. final String[] begin = new String[1];
  136. final int[] studytime = new int[1];
  137. final int[] credit = new int[1];
  138. // 创建显示信息文本框
  139. TextField idT = new TextField(String.valueOf(id[0]));
  140. TextField majorT = new TextField(major[0]);
  141. TextField nameT = new TextField(name[0]);
  142. TextField typeT = new TextField(type[0]);
  143. TextField beginT = new TextField(begin[0]);
  144. TextField studyTime = new TextField(String.valueOf(studytime[0]));
  145. TextField creditT = new TextField(String.valueOf(credit[0]));
  146. //创建第二个面板
  147. GridPane gp1=new GridPane();
  148. Label cID=new Label("课程号");
  149. Label cMajor=new Label("所属专业");
  150. Label cName=new Label("课程名称");
  151. Label cType=new Label("课程类型");
  152. Label cBegin=new Label("开课学期");
  153. Label cStudy=new Label("学时数");
  154. Label cCredit=new Label("学分");
  155. Button admitB=new Button("提交");
  156. Button rebackB=new Button("返回");
  157. rebackB.setOnAction(actionEvent -> {
  158. try {
  159. stage1.close();
  160. } catch (Exception e) {
  161. throw new RuntimeException(e);
  162. }
  163. });
  164. //创建字体样式
  165. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);
  166. cID.setFont(font);
  167. cMajor.setFont(font);
  168. cName.setFont(font);
  169. cType.setFont(font);
  170. cBegin.setFont(font);
  171. cStudy.setFont(font);
  172. cCredit.setFont(font);
  173. gp1.add(cID,0,0);
  174. gp1.add(idT,1,0);
  175. gp1.add(cMajor,0,1);
  176. gp1.add(majorT,1,1);
  177. gp1.add(cName,0,2);
  178. gp1.add(nameT,1,2);
  179. gp1.add(cType,0,3);
  180. gp1.add(typeT,1,3);
  181. gp1.add(cBegin,0,4);
  182. gp1.add(beginT,1,4);
  183. gp1.add(cStudy,0,5);
  184. gp1.add(studyTime,1,5);
  185. gp1.add(cCredit,0,6);
  186. gp1.add(creditT,1,6);
  187. gp1.add(admitB,0,7);
  188. gp1.add(rebackB,1,7);
  189. gp1.setPadding(new Insets(30));
  190. gp1.setVgap(20);
  191. gp1.setHgap(10);
  192. Scene scene1=new Scene(gp1,300,400);
  193. stage1.setScene(scene1);
  194. stage1.setTitle("修改课程");
  195. stage1.setResizable(false);
  196. stage1.close();
  197. String idText = idt.getText().trim();
  198. //创建第一个按钮的点击事件
  199. admitb.setOnAction(actionEvent -> {
  200. if (idText.isEmpty()) {
  201. otherView.tipJframe("id不能为空");
  202. }
  203. else {
  204. try {
  205. if (courseDao.isCourse(Integer.parseInt(idText))) {
  206. endView.course0 = courseDao.returnCourse(Integer.parseInt(idText));
  207. //给数组字符串赋值
  208. id[0] = endView.course0.getCou_id();
  209. major[0] = endView.course0.getCou_major();
  210. name[0] = endView.course0.getCou_name();
  211. type[0] = endView.course0.getCou_type();
  212. begin[0] = endView.course0.getCou_beginTime();
  213. studytime[0] = endView.course0.getCou_studyTime();
  214. credit[0] = endView.course0.getCou_score();
  215. // 更新界面元素,确保在JavaFX应用线程上更新
  216. Platform.runLater(() -> {
  217. idT.setText(String.valueOf(id[0]));
  218. majorT.setText(major[0]);
  219. nameT.setText(name[0]);
  220. typeT.setText(type[0]);
  221. beginT.setText(begin[0]);
  222. studyTime.setText(String.valueOf(studytime[0]));
  223. creditT.setText(String.valueOf(credit[0]));
  224. });
  225. // 显示舞台
  226. stage1.show();
  227. } else {
  228. otherView.tipJframe("不存在该课程");
  229. }
  230. } catch (Exception e) {
  231. otherView.tipJframe("发生异常: " + e.getMessage());
  232. }
  233. }
  234. });
  235. //创建第二个面板的按钮的点击事件
  236. admitB.setOnAction(actionEvent -> {
  237. try {
  238. // 获取文本框中的内容并去除首尾空白
  239. String ids = idT.getText().trim();
  240. String majors = majorT.getText().trim();
  241. String names = nameT.getText().trim();
  242. String types = typeT.getText().trim();
  243. String begins = beginT.getText().trim();
  244. String studys = studyTime.getText().trim();
  245. String credits = creditT.getText().trim();
  246. // 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作
  247. if (ids.isEmpty() || majors.isEmpty() || names.isEmpty() || types.isEmpty() || begins.isEmpty() || studys.isEmpty() || credits.isEmpty()) {
  248. otherView.tipJframe("字段不能为空");
  249. return;
  250. }
  251. int cId = Integer.parseInt(ids);
  252. int study=Integer.parseInt(studys);
  253. int creditt=Integer.parseInt(credits);
  254. if(!(id[0]==Integer.valueOf(idText))){
  255. if(courseDao.addCourse(cId, majors, names, types, begins, study, creditt)){
  256. courseDao.deleteCourse(id[0]);
  257. courseDao.initCourse();
  258. magView.magCourse();
  259. otherView.tipJframe("课程修改成功");
  260. }else {
  261. // 添加失败的提示
  262. otherView.tipJframe("课程修改失败");
  263. }
  264. }else if(id[0]==Integer.valueOf(idText)){
  265. // 调用Dao层方法尝试添加学生信息
  266. if (courseDao.updateCourse(cId, majors, names, types, begins, study, creditt)) {
  267. // 添加成功后的操作
  268. courseDao.initCourse();
  269. magView.magCourse();
  270. otherView.tipJframe("课程修改成功");
  271. } else {
  272. // 添加失败的提示
  273. otherView.tipJframe("课程修改失败");
  274. }
  275. }
  276. } catch (Exception e) {
  277. e.printStackTrace();
  278. otherView.tipJframe("操作失败:" + e.getMessage()); // 提示操作失败,并显示具体的异常信息
  279. }
  280. });
  281. }
  282. //删除课程
  283. public static void courseDelete(){
  284. GridPane gp = new GridPane();
  285. Label idL = new Label("ID");
  286. TextField idT = new TextField();
  287. Button admitB = new Button("提交");
  288. Button backB = new Button("返回");
  289. gp.add(idL, 0, 0);
  290. gp.add(idT, 1, 0);
  291. gp.add(admitB, 0, 1);
  292. gp.add(backB, 1, 1);
  293. gp.setAlignment(Pos.CENTER);
  294. gp.setHgap(10);
  295. gp.setVgap(30);
  296. Stage stage = new Stage();
  297. Scene scene = new Scene(gp, 240, 100);
  298. stage.setTitle("删除课程");
  299. stage.setScene(scene);
  300. stage.show();
  301. backB.setOnAction(actionEvent -> {
  302. try {
  303. stage.close();
  304. } catch (Exception e) {
  305. throw new RuntimeException(e);
  306. }
  307. });
  308. // 将按钮事件处理移到这里,确保在点击按钮时获取最新的文本框内容
  309. admitB.setOnAction(actionEvent -> {
  310. String ids = idT.getText().trim(); // 获取文本框内容
  311. if (!ids.isEmpty()) {
  312. try {
  313. int id = Integer.parseInt(ids);
  314. if (courseDao.isCourse(id)) {
  315. if (courseDao.deleteCourse(id)) {
  316. otherView.tipJframe("课程删除成功");
  317. courseDao.initCourse(); // 更新数据
  318. magView.magCourse(); // 刷新界面信息
  319. } else {
  320. otherView.tipJframe("课程删除失败");
  321. }
  322. } else {
  323. otherView.tipJframe("未找到该课程");
  324. }
  325. } catch (Exception e) {
  326. e.printStackTrace();
  327. otherView.tipJframe("数据库操作失败");
  328. }
  329. } else {
  330. otherView.tipJframe("请输入课程ID");
  331. }
  332. });
  333. }

Score(scoreUtil) :

  1. //添加成绩
  2. public static void scoreAdd(){
  3. GridPane gp=new GridPane();
  4. Label sID=new Label("学号");
  5. Label sCID=new Label("课程号");
  6. Label sStudy=new Label("成绩");
  7. Label sCredit=new Label("学分");
  8. Button admitB=new Button("提交");
  9. Button rebackB=new Button("返回");
  10. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);
  11. sID.setFont(font);
  12. sCID.setFont(font);
  13. sStudy.setFont(font);
  14. sCredit.setFont(font);
  15. TextField sIDT=new TextField();
  16. TextField scIDT=new TextField();
  17. TextField sStudyT=new TextField();
  18. TextField sCreditT=new TextField();
  19. gp.add(sID,0,0);
  20. gp.add(sIDT,1,0);
  21. gp.add(sCID,0,1);
  22. gp.add(scIDT,1,1);
  23. gp.add(sStudy,0,2);
  24. gp.add(sStudyT,1,2);
  25. gp.add(sCredit,0,3);
  26. gp.add(sCreditT,1,3);
  27. gp.add(admitB,0,7);
  28. gp.add(rebackB,1,7);
  29. gp.setPadding(new Insets(30));
  30. gp.setVgap(20);
  31. gp.setHgap(10);
  32. Scene scene=new Scene(gp,280,300);
  33. Stage stage1=new Stage();
  34. stage1.setScene(scene);
  35. stage1.setTitle("添加分数");
  36. stage1.show();
  37. rebackB.setOnAction(actionEvent -> {
  38. try {
  39. stage1.close();
  40. } catch (Exception e) {
  41. throw new RuntimeException(e);
  42. }
  43. });
  44. admitB.setOnAction(actionEvent -> {
  45. try {
  46. // 获取文本框中的内容并去除首尾空白
  47. String id = sIDT.getText().trim();
  48. String cid = scIDT.getText().trim();
  49. String study = sStudyT.getText().trim();
  50. String credit = sCreditT.getText().trim();
  51. // 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作
  52. if ((id.isEmpty() || cid.isEmpty() || study.isEmpty() || credit.isEmpty())) {
  53. otherView.tipJframe("字段不能为空");
  54. return;
  55. }else if(!studentDao.isStudent(Integer.parseInt(id))){
  56. otherView.tipJframe("不存在该学生");
  57. return;
  58. }else if(!courseDao.isCourse(Integer.parseInt(cid))){
  59. otherView.tipJframe("不存在该课程");
  60. return;
  61. }
  62. int Id=Integer.parseInt(id);
  63. int cId = Integer.parseInt(cid);
  64. if (scoreDao.addScore(Id, cId, study, credit)&&scoreDao.isScore(Id,cId)) {
  65. scoreDao.initScore();
  66. magView.magScore();
  67. otherView.tipJframe("成绩添加成功");
  68. } else {
  69. // 添加失败的提示
  70. otherView.tipJframe("成绩添加失败");
  71. }
  72. } catch (NumberFormatException e) {
  73. otherView.tipJframe("ID格式错误,请输入有效的整数");
  74. } catch (Exception e) {
  75. // 捕获其他异常并打印堆栈信息
  76. e.printStackTrace();
  77. otherView.tipJframe("操作失败:" + e.getMessage());
  78. }
  79. });
  80. }
  81. //修改成绩
  82. public static void scoreUpdate(){
  83. GridPane gp=new GridPane();
  84. Label idl = new Label("ID");
  85. Label cidl=new Label("cID");
  86. TextField idt = new TextField();
  87. TextField cidt=new TextField();
  88. Button admitb = new Button("提交");
  89. Button backB=new Button("返回");
  90. gp.add(idl,0,0);
  91. gp.add(idt,1,0);
  92. gp.add(cidl,0,1);
  93. gp.add(cidt,1,1);
  94. gp.add(admitb,0,2);
  95. gp.add(backB,1,2);
  96. gp.setHgap(10);
  97. gp.setVgap(20);
  98. gp.setAlignment(Pos.CENTER);
  99. Scene scene=new Scene(gp,300,240);
  100. //创建第一个舞台
  101. Stage stage=new Stage();
  102. stage.setScene(scene);
  103. stage.setResizable(false);
  104. stage.setTitle("修改分数");
  105. stage.show();
  106. backB.setOnAction(actionEvent -> {
  107. stage.close();
  108. });
  109. //创建第二个面板
  110. GridPane gp1 = new GridPane();
  111. final int[] id = new int[1];
  112. final int[] cid = new int[1];
  113. TextField sIDT=new TextField(String.valueOf(id[0]));
  114. TextField scIDT=new TextField(String.valueOf(cid[0]));
  115. TextField sStudyT=new TextField();
  116. TextField sCreditT=new TextField();
  117. Label sID=new Label("学号");
  118. Label sCID=new Label("课程号");
  119. Label sStudy=new Label("成绩");
  120. Label sCredit=new Label("学分");
  121. Button admitB=new Button("提交");
  122. Button rebackB=new Button("返回");
  123. Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);
  124. sID.setFont(font);
  125. sCID.setFont(font);
  126. sStudy.setFont(font);
  127. sCredit.setFont(font);
  128. gp1.add(sID,0,0);
  129. gp1.add(sIDT,1,0);
  130. gp1.add(sCID,0,1);
  131. gp1.add(scIDT,1,1);
  132. gp1.add(sStudy,0,2);
  133. gp1.add(sStudyT,1,2);
  134. gp1.add(sCredit,0,3);
  135. gp1.add(sCreditT,1,3);
  136. gp1.add(admitB,0,7);
  137. gp1.add(rebackB,1,7);
  138. gp1.setPadding(new Insets(30));
  139. gp1.setVgap(20);
  140. gp1.setHgap(10);
  141. Scene scene1=new Scene(gp1,280,300);
  142. //创建第二个舞台
  143. Stage stage1 = new Stage();
  144. stage1.setScene(scene1);
  145. stage1.setTitle("修改分数");
  146. stage1.setResizable(false);
  147. stage1.close();
  148. rebackB.setOnAction(actionEvent -> {
  149. try {
  150. stage1.close();
  151. } catch (Exception e) {
  152. throw new RuntimeException(e);
  153. }
  154. });
  155. // 登录按钮的事件处理器
  156. admitb.setOnAction(actionEvent -> {
  157. String idText = idt.getText().trim();
  158. String cidText = cidt.getText().trim();
  159. if (!(idText.isEmpty() || cidText.isEmpty())) {
  160. try {
  161. if (judgeUtil.isDigits(idText) && judgeUtil.isDigits(cidText)) {
  162. if (scoreDao.isScore(Integer.parseInt(idText), Integer.parseInt(cidText))) {
  163. endView.score0 = scoreDao.returnScore(Integer.parseInt(idText),Integer.parseInt(cidText));
  164. id[0] = endView.score0.getScore_id();
  165. cid[0] = endView.score0.getScore_cid();
  166. // 更新TextField显示
  167. sIDT.setText(String.valueOf(id[0]));
  168. scIDT.setText(String.valueOf(cid[0]));
  169. // 显示舞台
  170. stage1.show();
  171. stage.close();
  172. } else if (!studentDao.isStudent(Integer.parseInt(idText))) {
  173. otherView.tipJframe("不存在该学生");
  174. } else if (!courseDao.isCourse(Integer.parseInt(cidText))) {
  175. otherView.tipJframe("不存在该课程");
  176. } else {
  177. otherView.tipJframe("信息输入错误");
  178. }
  179. } else {
  180. otherView.tipJframe("格式错误");
  181. }
  182. } catch (Exception e) {
  183. throw new RuntimeException(e);
  184. }
  185. } else {
  186. otherView.tipJframe("id不能为空");
  187. }
  188. });
  189. //第二个按钮点击事件
  190. admitB.setOnAction(actionEvent -> {
  191. try {
  192. // 获取文本框中的内容并去除首尾空白
  193. String ids = sIDT.getText().trim();
  194. String cids = scIDT.getText().trim();
  195. String studys = sStudyT.getText().trim();
  196. String credits = sCredit.getText().trim();
  197. // 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作
  198. if (ids.isEmpty() || cids.isEmpty() || studys.isEmpty() || credits.isEmpty()) {
  199. otherView.tipJframe("字段不能为空");
  200. return;
  201. }
  202. // 尝试将id和password转换为整数,如果格式不正确会抛出NumberFormatException
  203. int Id=Integer.parseInt(ids);
  204. int cId = Integer.parseInt(cids);
  205. // 调用Dao层方法尝试添加学生信息
  206. if (scoreDao.updateScore(Id, cId, studys, credits)) {
  207. // 添加成功后的操作
  208. scoreDao.initScore();
  209. magView.magScore();
  210. otherView.tipJframe("成绩添加成功");
  211. } else {
  212. otherView.tipJframe("成绩添加失败");
  213. }
  214. } catch (NumberFormatException e) {
  215. otherView.tipJframe("ID格式错误,请输入有效的整数");
  216. } catch (Exception e) {
  217. e.printStackTrace();
  218. otherView.tipJframe("操作失败:" + e.getMessage());
  219. }
  220. });
  221. }
  222. //删除成绩
  223. public static void scoreDelete(){
  224. GridPane gp = new GridPane();
  225. Label idL = new Label("ID");
  226. Label cidL=new Label("cID");
  227. TextField idT = new TextField();
  228. TextField cidT=new TextField();
  229. Button admitB = new Button("提交");
  230. Button backB = new Button("返回");
  231. gp.add(idL, 0, 0);
  232. gp.add(idT, 1, 0);
  233. gp.add(cidL,0,1);
  234. gp.add(cidT,1,1);
  235. gp.add(admitB, 0, 2);
  236. gp.add(backB, 1, 2);
  237. gp.setAlignment(Pos.CENTER);
  238. gp.setHgap(10);
  239. gp.setVgap(30);
  240. Stage stage = new Stage();
  241. Scene scene = new Scene(gp, 240, 200);
  242. stage.setScene(scene);
  243. stage.setTitle("删除分数");
  244. stage.show();
  245. backB.setOnAction(actionEvent -> {
  246. try {
  247. stage.close();
  248. } catch (Exception e) {
  249. throw new RuntimeException(e);
  250. }
  251. });
  252. // 将按钮事件处理移到这里,确保在点击按钮时获取最新的文本框内容
  253. admitB.setOnAction(actionEvent -> {
  254. String ids = idT.getText().trim();
  255. String cids=cidT.getText().trim();
  256. if (!ids.isEmpty()) {
  257. try {
  258. int id = Integer.parseInt(ids);
  259. int cid=Integer.parseInt(cids);
  260. if (studentDao.isStudent(id)) {
  261. if (scoreDao.deleteScore(id,cid)) {
  262. otherView.tipJframe("课程删除成功");
  263. scoreDao.initScore();
  264. magView.magScore();
  265. } else {
  266. otherView.tipJframe("课程删除失败");
  267. }
  268. } else {
  269. otherView.tipJframe("未找到该课程");
  270. }
  271. } catch (NumberFormatException e) {
  272. otherView.tipJframe("请输入有效的数字ID");
  273. } catch (Exception e) {
  274. e.printStackTrace();
  275. otherView.tipJframe("数据库操作失败");
  276. }
  277. } else {
  278. otherView.tipJframe("请输入课程ID");
  279. }
  280. });
  281. }

Classe d'outils de jugement (judgeUtil) :

Cette classe d'outils est utilisée pour juger les chaînes dans certaines zones de texte lors de l'ajout de données. Elle est utilisée dans les trois utilitaires ci-dessus.

  1. //判断日期格式
  2. public static boolean isValidDate(String dateStr) {
  3. try {
  4. DateTimeFormatter.ofPattern("yyyy-MM-dd").parse(dateStr);
  5. return true;
  6. } catch (DateTimeParseException e) {
  7. return false;
  8. }
  9. }
  10. //判断班级格式
  11. public static String isClass(String s){
  12. String numberString = s;
  13. int numberOfCharsToTake = 6; // 指定你想截取的字符数量
  14. String prefix = numberString.substring(0, numberOfCharsToTake);
  15. return prefix;
  16. }
  17. //判断密码格式
  18. public static String isPassword(String s){
  19. String numberString = s;
  20. int numberOfCharsToTake = 6; // 指定你想截取的字符数量从末尾开始
  21. int startIndex = numberString.length() - numberOfCharsToTake;
  22. String suffix = numberString.substring(startIndex);
  23. return suffix;
  24. }
  25. //判断字符串是否为数字
  26. public static boolean isDigits(String str) {
  27. return str.matches("\d+");
  28. }

Classe Administrateur Dao :

utilDao:


Étant donné que Connection, close et exeupdate seront fréquemment utilisés, j'écrirai trois méthodes directement ici.

  1. private static final String URL = "jdbc:mysql:///studentMs";
  2. private static final String USER = "root";
  3. private static final String PASSWORD = "123321";
  4. //获取连接对象
  5. public static Connection getCon() {
  6. try {
  7. return DriverManager.getConnection(URL, USER, PASSWORD);
  8. } catch (SQLException e) {
  9. e.printStackTrace();
  10. return null;
  11. }
  12. }
  13. //关闭资源
  14. public static void close(ResultSet rs, PreparedStatement ps, Connection con) {
  15. try {
  16. if (rs != null) {
  17. rs.close();
  18. }
  19. if (ps != null) {
  20. ps.close();
  21. }
  22. if (con != null) {
  23. con.close();
  24. }
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. //执行mysql
  30. public static boolean exeUpdate(String sql, Object... params) {
  31. //获取连接对象
  32. Connection con = getCon();
  33. PreparedStatement ps = null;
  34. try {
  35. //获取编译对象
  36. ps = con.prepareStatement(sql);
  37. //判断参数是否为空
  38. if (Objects.nonNull(params)) {
  39. for (int i = 0; i < params.length; i++) {
  40. //实现占位赋值
  41. ps.setObject(i + 1, params[i]);
  42. }
  43. }
  44. //执行更新
  45. return ps.executeUpdate() > 0;
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. } finally {
  49. close(null, ps, con);
  50. }
  51. return false;
  52. }

étudiantDao*:

Le studentDao suivant est uniquement lié au fonctionnement de l'interface de l'administrateur

  1. //返回学生信息
  2. public static student returnStudent(int stu_id)throws Exception{
  3. student student=new student();
  4. String sql="select stuID,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword from student where stuID=?";
  5. PreparedStatement ps=utilDao.getCon().prepareStatement(sql);
  6. //过滤其他的只留下ID为1的学生
  7. ps.setString(1,String.valueOf(stu_id));
  8. ResultSet rs=ps.executeQuery();
  9. if(rs.next()){
  10. student.setStu_id(rs.getInt("stuID"));
  11. student.setStu_class(rs.getString("stuClass"));
  12. student.setStu_name(rs.getString("stuName"));
  13. student.setStu_gender(rs.getString("stuSex"));
  14. student.setStu_birth(rs.getString("stuBirth"));
  15. student.setStu_major(rs.getString("stuMajor"));
  16. student.setStu_password(rs.getString("stuPassword"));
  17. }
  18. rs.close();
  19. ps.close();
  20. return student;
  21. }
  22. //判断是否存在该学生
  23. public static boolean isStudent(int stu_id) throws SQLException {
  24. String sql="select stuID,stuClass,stuName,stuSex,stuBirth,stuMajor from student where stuID=?";
  25. PreparedStatement ps=utilDao.getCon().prepareStatement(sql);
  26. //过滤其他的只留下ID为1的学生
  27. ps.setString(1,String.valueOf(stu_id));
  28. ResultSet rs=ps.executeQuery();
  29. if(rs.next()){
  30. return true;
  31. }
  32. else {
  33. return false;
  34. }
  35. }
  36. // 学生类信息初始化
  37. public static ArrayList<student> initStudent() throws Exception {
  38. ArrayList<student> students = new ArrayList<>();
  39. String sql = "select * from student order by stuID asc";
  40. PreparedStatement ps = utilDao.getCon().prepareStatement(sql);
  41. ResultSet rs = ps.executeQuery();
  42. // 使用 while 循环遍历 ResultSet 中的所有行
  43. while (rs.next()) {
  44. student student = new student();
  45. student.setStu_id(rs.getInt("stuID"));
  46. student.setStu_class(rs.getString("stuClass"));
  47. student.setStu_name(rs.getString("stuName"));
  48. student.setStu_gender(rs.getString("stuSex"));
  49. student.setStu_birth(rs.getString("stuBirth"));
  50. student.setStu_major(rs.getString("stuMajor"));
  51. students.add(student);
  52. }
  53. // 关闭资源
  54. rs.close();
  55. ps.close();
  56. return students;
  57. }
  58. //添加学生
  59. public static boolean addStudent(int stuId,String stuClass,String stuName, String stuSex, String stuBirth, String stuMajor,String stuPassword)throws Exception{
  60. String sql="insert into student(stuID,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword )values (?,?,?,?,?,?,?) ";
  61. return utilDao.exeUpdate(sql,stuId,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword);
  62. }
  63. //修改学生
  64. public static boolean updateStudent(int stuId,String stuClass,String stuName, String stuSex, String stuBirth, String stuMajor,String stuPassword)throws Exception{
  65. String sql="update student set stuID=?,stuClass=?,stuName=?,stuSex=?,stuBirth=?,stuMajor=?,stuPassword=?";
  66. return utilDao.exeUpdate(sql,stuId,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword);
  67. }
  68. //删除学生
  69. public static boolean deleteStudent(int stuId){
  70. String sql="delete from student where stuID=?";
  71. return utilDao.exeUpdate(sql,stuId);
  72. }

coursDao:

  1. //返回课程
  2. public static course returnCourse(int cID)throws Exception{
  3. course course=new course();
  4. String sql="select cID,cMajor,cName,cType,cStartTerm,cPeriod,cCredit from course where cID=?";
  5. PreparedStatement ps=utilDao.getCon().prepareStatement(sql);
  6. //过滤其他的只留下ID为1的学生
  7. ps.setString(1,String.valueOf(cID));
  8. ResultSet rs=ps.executeQuery();
  9. if(rs.next()){
  10. course.setCou_id(rs.getInt("cID"));
  11. course.setCou_major(rs.getString("cMajor"));
  12. course.setCou_name(rs.getString("cName"));
  13. course.setCou_type(rs.getString("cType"));
  14. course.setCou_beginTime(rs.getString("cStartTerm"));
  15. course.setCou_studyTime(rs.getInt("cPeriod"));
  16. course.setCou_score(rs.getInt("cCredit"));
  17. }
  18. rs.close();
  19. ps.close();
  20. return course;
  21. }
  22. //判断是否存在该课程
  23. public static boolean isCourse(int cID) throws Exception {
  24. String sql="select cID,cMajor,cName,cType,cStartTerm,cPeriod,cCredit from course where cID=?";
  25. PreparedStatement ps=utilDao.getCon().prepareStatement(sql);
  26. //过滤其他的只留下ID为1的学生
  27. ps.setInt(1,cID);
  28. ResultSet rs=ps.executeQuery();
  29. if(rs.next()){
  30. return true;
  31. } else {
  32. return false;
  33. }
  34. }
  35. // 学生类集合课程初始化
  36. public static ArrayList<course> initCourse() throws Exception {
  37. ArrayList<course> courses = new ArrayList<>();
  38. String sql = "select * from course order by cID asc";
  39. PreparedStatement ps = utilDao.getCon().prepareStatement(sql);
  40. ResultSet rs = ps.executeQuery();
  41. // 使用 while 循环遍历 ResultSet 中的所有行
  42. while (rs.next()) {
  43. course course = new course();
  44. course.setCou_id(rs.getInt("cID"));
  45. course.setCou_major(rs.getString("cMajor"));
  46. course.setCou_name(rs.getString("cName"));
  47. course.setCou_type(rs.getString("cType"));
  48. course.setCou_beginTime(rs.getString("cStartTerm"));
  49. course.setCou_studyTime(rs.getInt("cPeriod"));
  50. course.setCou_score(rs.getInt("cCredit"));
  51. courses.add(course);
  52. }
  53. // 关闭资源
  54. rs.close();
  55. ps.close();
  56. return courses;
  57. }
  58. //增加课程
  59. public static boolean addCourse(int cID, String cMajor, String cName, String cType, String cStartTerm, int cPeriod, int cCredit){
  60. String sql="insert into course(cID,cMajor,cName,cType,cStartTerm,cPeriod,cCredit)values (?,?,?,?,?,?,?) ";
  61. return utilDao.exeUpdate(sql,cID,cMajor,cName,cType,cStartTerm,cPeriod,cCredit);
  62. }
  63. //修改课程
  64. public static boolean updateCourse(int cID, String cMajor, String cName, String cType, String cStartTerm, int cPeriod, int cCredit){
  65. String sql="update course set cID=?,cMajor=?,cName=?,cType=?,cStartTerm=?,cPeriod=?,cCredit=? ";
  66. return utilDao.exeUpdate(sql,cID,cMajor,cName,cType,cStartTerm,cPeriod,cCredit);
  67. }
  68. //删除课程
  69. public static boolean deleteCourse(int cID){
  70. String sql="delete from course where cID=?";
  71. return utilDao.exeUpdate(sql,cID);
  72. }

scoreDao:

  1. //判断是否存在该成绩
  2. public static boolean isScore(int stu_id,int cid) throws SQLException {
  3. String sql="select * from score where stuID=? and cID=?";
  4. PreparedStatement ps=utilDao.getCon().prepareStatement(sql);
  5. //过滤其他的只留下ID为1的学生
  6. ps.setString(1,String.valueOf(stu_id));
  7. ps.setString(2,String.valueOf(cid));
  8. ResultSet rs=ps.executeQuery();
  9. if(rs.next()){
  10. return true;
  11. }
  12. return false;
  13. }
  14. // 学生类成绩初始化
  15. public static ArrayList<score> initScore() throws Exception {
  16. ArrayList<score> scores = new ArrayList<>();
  17. String sql = "select * from score order by stuID asc ";
  18. PreparedStatement ps = utilDao.getCon().prepareStatement(sql);
  19. ResultSet rs = ps.executeQuery();
  20. // 使用 while 循环遍历 ResultSet 中的所有行
  21. while (rs.next()) {
  22. score score = new score();
  23. score.setScore_id(rs.getInt("stuID"));
  24. score.setScore_cid(rs.getInt("cID"));
  25. score.setSocre_score(rs.getString("score"));
  26. score.setScore_credit(rs.getString("credit"));
  27. scores.add(score);
  28. }
  29. // 关闭资源
  30. rs.close();
  31. ps.close();
  32. return scores;
  33. }
  34. //返回学生成绩
  35. public static score returnScore(int stuID,int cID)throws Exception{
  36. score score=new score();
  37. String sql="select * from score where stuID=? and cID=? ";
  38. PreparedStatement ps=utilDao.getCon().prepareStatement(sql);
  39. //过滤其他的只留下ID为1的学生
  40. ps.setString(1,String.valueOf(stuID));
  41. ps.setString(2,String.valueOf(cID));
  42. ResultSet rs=ps.executeQuery();
  43. while (rs.next()){
  44. score.setScore_id(rs.getInt("stuID"));
  45. score.setScore_cid(rs.getInt("cID"));
  46. score.setSocre_score(rs.getString("score"));
  47. score.setScore_credit(rs.getString("credit"));
  48. }
  49. rs.close();
  50. ps.close();
  51. return score;
  52. }
  53. //增加成绩
  54. public static boolean addScore(int stuID,int cID,String score,String credit){
  55. String sql="insert into score(stuID,cID,score,credit)values (?,?,?,?) ";
  56. return utilDao.exeUpdate(sql,stuID,cID,score,credit);
  57. }
  58. //修改成绩
  59. public static boolean updateScore(int stuID,int cID,String score,String credit){
  60. String sql="update score set stuID=?,cID=?,score=?,credit=? ";
  61. return utilDao.exeUpdate(sql,stuID,cID,score,credit);
  62. }
  63. //删除成绩
  64. public static boolean deleteScore(int stuID,int cID){
  65. String sql="delete from score where stuID=? and cID=?";
  66. return utilDao.exeUpdate(sql,stuID,cID);
  67. }

jugeDao:

  1. //找出最大学生id
  2. public static int maxID() throws Exception {
  3. String sql = "select max(stuID) as maxId from student";
  4. PreparedStatement ps = utilDao.getCon().prepareStatement(sql);
  5. ResultSet rs = ps.executeQuery();
  6. // 默认值或者根据需求设定初始值
  7. int maxId = 0;
  8. //如果存在
  9. if (rs.next()) {
  10. maxId = rs.getInt("maxId"); // 从结果集中获取名为 maxId 的列的值
  11. }
  12. return maxId;
  13. }
  14. //找出最大课程id
  15. public static int maxcID()throws Exception{
  16. String sql = "select max(cID) as maxId from course";
  17. PreparedStatement ps = utilDao.getCon().prepareStatement(sql);
  18. ResultSet rs = ps.executeQuery();
  19. int maxId = 0; // 默认值或者根据需求设定初始值
  20. if (rs.next()) {
  21. maxId = rs.getInt("maxId"); // 从结果集中获取名为 maxId 的列的值
  22. }
  23. return maxId;
  24. }
  25. //判断是否为管理员
  26. public static boolean magLogin(String name,String password)throws Exception{
  27. String sql="select * from manager where name=? and password=?";
  28. PreparedStatement ps=utilDao.getCon().prepareStatement(sql);
  29. ps.setString(1,name);
  30. ps.setString(2,password);
  31. ResultSet rs=ps.executeQuery();
  32. if (rs.next()) {
  33. utilDao.close(rs,ps,utilDao.getCon());
  34. return true;
  35. } else {
  36. utilDao.close(rs,ps,utilDao.getCon());
  37. return false;
  38. }
  39. }

domaine (classe d'objet) :

étudiant:

  1. public class student {
  2. private int stu_id;
  3. private String stu_class;
  4. private String stu_name;
  5. private String stu_gender;
  6. private String stu_birth;
  7. private String stu_major;
  8. private String stu_password;
  9. public student() {
  10. }
  11. public int getStu_id() {
  12. return stu_id;
  13. }
  14. public void setStu_id(int stu_id) {
  15. this.stu_id = stu_id;
  16. }
  17. public String getStu_class() {
  18. return stu_class;
  19. }
  20. public void setStu_class(String stu_class) {
  21. this.stu_class = stu_class;
  22. }
  23. public String getStu_name() {
  24. return stu_name;
  25. }
  26. public void setStu_name(String stu_name) {
  27. this.stu_name = stu_name;
  28. }
  29. public String getStu_gender() {
  30. return stu_gender;
  31. }
  32. public void setStu_gender(String stu_gender) {
  33. this.stu_gender = stu_gender;
  34. }
  35. public String getStu_birth() {
  36. return stu_birth;
  37. }
  38. public void setStu_birth(String stu_birth) {
  39. this.stu_birth = stu_birth;
  40. }
  41. public String getStu_major() {
  42. return stu_major;
  43. }
  44. public void setStu_major(String stu_major) {
  45. this.stu_major = stu_major;
  46. }
  47. public String getStu_password() {
  48. return stu_password;
  49. }
  50. public void setStu_password(String stu_password) {
  51. this.stu_password = stu_password;
  52. }

cours:

  1. public class course {
  2. private int cou_id;
  3. private String cou_major;
  4. private String cou_name;
  5. private String cou_type;
  6. private String cou_beginTime;
  7. private int cou_studyTime;
  8. private int cou_score;
  9. public course() {
  10. }
  11. public int getCou_id() {
  12. return cou_id;
  13. }
  14. public void setCou_id(int cou_id) {
  15. this.cou_id = cou_id;
  16. }
  17. public String getCou_major() {
  18. return cou_major;
  19. }
  20. public void setCou_major(String cou_major) {
  21. this.cou_major = cou_major;
  22. }
  23. public String getCou_name() {
  24. return cou_name;
  25. }
  26. public void setCou_name(String cou_name) {
  27. this.cou_name = cou_name;
  28. }
  29. public String getCou_type() {
  30. return cou_type;
  31. }
  32. public void setCou_type(String cou_type) {
  33. this.cou_type = cou_type;
  34. }
  35. public String getCou_beginTime() {
  36. return cou_beginTime;
  37. }
  38. public void setCou_beginTime(String cou_beginTime) {
  39. this.cou_beginTime = cou_beginTime;
  40. }
  41. public int getCou_studyTime() {
  42. return cou_studyTime;
  43. }
  44. public void setCou_studyTime(int cou_studyTime) {
  45. this.cou_studyTime = cou_studyTime;
  46. }
  47. public void setCou_score(int cou_score) {
  48. this.cou_score = cou_score;
  49. }
  50. public int getCou_score() {
  51. return cou_score;
  52. }

score:

  1. private int score_id;
  2. private int score_cid;
  3. private String socre_score;
  4. private String score_credit;
  5. public score() {
  6. }
  7. public int getScore_id() {
  8. return score_id;
  9. }
  10. public String getSocre_score() {
  11. return socre_score;
  12. }
  13. public void setSocre_score(String socre_score) {
  14. this.socre_score = socre_score;
  15. }
  16. public String getScore_credit() {
  17. return score_credit;
  18. }
  19. public void setScore_credit(String score_credit) {
  20. this.score_credit = score_credit;
  21. }
  22. public void setScore_id(int score_id) {
  23. this.score_id = score_id;
  24. }
  25. public int getScore_cid() {
  26. return score_cid;
  27. }

fin

J'ai fini de partager mon code ici. Merci à tous d'avoir regardé. Comme ma structure de code est relativement volumineuse, chacune des classes ci-dessus est complète ici, à l'exception de studentDao, qui est écrit séparément, je vais le réafficher ici. , tout le reste est identique et ne sera pas affiché.

 

étudiantDao (complet):

  1. //学生登录判断
  2. public static boolean login(String idStr, String passwordStr) throws Exception {
  3. String sql = "select * from student where stuID =? and stuPassword=?";
  4. PreparedStatement ps = utilDao.getCon().prepareStatement(sql);
  5. ps.setString(1,idStr);
  6. ps.setString(2, passwordStr);
  7. ResultSet rs = ps.executeQuery();
  8. if (rs.next()) {
  9. utilDao.close(rs,ps, utilDao.getCon());
  10. return true;
  11. } else {
  12. utilDao.close(rs,ps,utilDao.getCon());
  13. return false;
  14. }
  15. }
  16. //学生查询自己成绩
  17. public static Integer gradeOneself(int stu_id) throws Exception {
  18. String sql = "SELECT SUM(credit) AS gradesum FROM score WHERE stuID=?";
  19. try (Connection connection =utilDao.getCon();
  20. PreparedStatement ps = connection.prepareStatement(sql)) {
  21. ps.setInt(1, stu_id);
  22. try (ResultSet rs = ps.executeQuery()) {
  23. if (rs.next()) {
  24. int gradesum = rs.getInt("gradesum");
  25. return gradesum;
  26. } else {
  27. // 如果没有查询到结果,可以返回 null 或者其他合适的值
  28. return null;
  29. }
  30. }
  31. }
  32. }
  33. //返回学生信息
  34. public static student returnStudent(int stu_id)throws Exception{
  35. student student=new student();
  36. String sql="select stuID,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword from student where stuID=?";
  37. PreparedStatement ps=utilDao.getCon().prepareStatement(sql);
  38. //过滤其他的只留下ID为1的学生
  39. ps.setString(1,String.valueOf(stu_id));
  40. ResultSet rs=ps.executeQuery();
  41. if(rs.next()){
  42. student.setStu_id(rs.getInt("stuID"));
  43. student.setStu_class(rs.getString("stuClass"));
  44. student.setStu_name(rs.getString("stuName"));
  45. student.setStu_gender(rs.getString("stuSex"));
  46. student.setStu_birth(rs.getString("stuBirth"));
  47. student.setStu_major(rs.getString("stuMajor"));
  48. student.setStu_password(rs.getString("stuPassword"));
  49. }
  50. rs.close();
  51. ps.close();
  52. return student;
  53. }
  54. //判断是否存在该学生
  55. public static boolean isStudent(int stu_id) throws SQLException {
  56. String sql="select stuID,stuClass,stuName,stuSex,stuBirth,stuMajor from student where stuID=?";
  57. PreparedStatement ps=utilDao.getCon().prepareStatement(sql);
  58. //过滤其他的只留下ID为1的学生
  59. ps.setString(1,String.valueOf(stu_id));
  60. ResultSet rs=ps.executeQuery();
  61. if(rs.next()){
  62. return true;
  63. }
  64. else {
  65. return false;
  66. }
  67. }
  68. // 学生类信息初始化
  69. public static ArrayList<student> initStudent() throws Exception {
  70. ArrayList<student> students = new ArrayList<>();
  71. String sql = "select * from student order by stuID asc";
  72. PreparedStatement ps = utilDao.getCon().prepareStatement(sql);
  73. ResultSet rs = ps.executeQuery();
  74. // 使用 while 循环遍历 ResultSet 中的所有行
  75. while (rs.next()) {
  76. student student = new student();
  77. student.setStu_id(rs.getInt("stuID"));
  78. student.setStu_class(rs.getString("stuClass"));
  79. student.setStu_name(rs.getString("stuName"));
  80. student.setStu_gender(rs.getString("stuSex"));
  81. student.setStu_birth(rs.getString("stuBirth"));
  82. student.setStu_major(rs.getString("stuMajor"));
  83. students.add(student);
  84. }
  85. // 关闭资源
  86. rs.close();
  87. ps.close();
  88. return students;
  89. }
  90. //添加学生
  91. public static boolean addStudent(int stuId,String stuClass,String stuName, String stuSex, String stuBirth, String stuMajor,String stuPassword)throws Exception{
  92. String sql="insert into student(stuID,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword )values (?,?,?,?,?,?,?) ";
  93. return utilDao.exeUpdate(sql,stuId,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword);
  94. }
  95. //修改学生
  96. public static boolean updateStudent(int stuId,String stuClass,String stuName, String stuSex, String stuBirth, String stuMajor,String stuPassword)throws Exception{
  97. String sql="update student set stuID=?,stuClass=?,stuName=?,stuSex=?,stuBirth=?,stuMajor=?,stuPassword=?";
  98. return utilDao.exeUpdate(sql,stuId,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword);
  99. }
  100. //删除学生
  101. public static boolean deleteStudent(int stuId){
  102. String sql="delete from student where stuID=?";
  103. return utilDao.exeUpdate(sql,stuId);
  104. }

Merci à tous d'avoir regardé. S'il y a quelque chose que vous ne comprenez pas, laissez-moi un message Rush !!!