前言:
上个月学习了javafx和mysql数据库,于是写了一个学生管理系统,因为上个月在复习并且有一些事情,比较忙,所以没有更新博客了,这个项目页面虽然看着有点简陋了,但是大致内容还是比较简单的,于是现在跟大家分享一下我的学生管理系统,希望对这方面有兴趣的同学提供一些帮助
🥰个人主页:心.c
🤓文章专题:javafx+mysql管理系统
👐欢迎大家点赞👍 收藏😽
目录
页面展示:
包的创建:
登录界面:
学生登录界面:
学生信息查询:
编辑
学生成绩查询:
管理员登录界面:
管理员学生信息管理界面:
管理员课程信息管理界面:
管理员成绩信息管理界面:
修改学生界面:
代码展示:
主窗口(endView):
登录界面和提示窗口(otherView):
学生:
学生界面(studentView):
学生Dao语句:
utilDao:
studentDao*:
管理员:
管理员界面(magView):
管理员工具类:
学生(studentUtil):
课程(courseUtil):
分数(scoreUtil):
判断工具类(judgeUtil):
管理员Dao类:
utilDao:
studentDao*:
courseDao:
scoreDao:
judgeDao:
domain(对象类):
student:
course:
score:
结尾
页面展示:
包的创建:
(在这里我用了一些mvc的框架结构,虽然我的代码不是很多,但是我觉得这样写可以让我们的代码变的更加简洁易懂,很有结构层次)
登录界面:
关于登录界面(关于登录界面,写了一些关于文本输入框的判断--数据库判断是否正确和一些非空判断--)
学生登录界面:
学生信息查询:
学生成绩查询:
管理员登录界面:
管理员学生信息管理界面:
管理员课程信息管理界面:
管理员成绩信息管理界面:
修改学生界面:
(以为这几个页面都差不多,所以在这里我就只展示一个添加学生界面了)
代码展示:
主窗口(endView):
关于主窗口,我定义了一个静态舞台来当我的主舞台,将我的start方法中的舞台赋值给静态舞台,我设置这个舞台的目的是为了将我写的其他方法,比如登录界面等的方法里面的scene场景放到我的静态stage当中,这样我的代码在执行的时候我的页面就可以一直用主舞台了也不会显得界面很乱了,而且非常方便,简单易懂
public class endView extends Application {public static Stage stage;public static student student0;public static course course0;public static score score0;@Overridepublic void start(Stage stage) {endView.stage=stage;//这里将主舞台赋值给静态舞台stage.setTitle("学生管理系统");stage.setResizable(false);otherView.login();endView.stage.show();}public static void main(String[] args) {launch(args);}}
登录界面和提示窗口(otherView):
//设置登录界面public static void login() {//创建网格面板GridPane gp=new GridPane();gp.setVgap(10);gp.setHgap(10);//创建标签Label idL=new Label("id");Label passwordL=new Label("密码");//创建输入框TextField tf=new TextField();PasswordField pf=new PasswordField();tf.setPromptText("请输入您的id号");pf.setPromptText("请输入6位数密码");//创建单选按钮RadioButton stuB=new RadioButton("学生");RadioButton teaB=new RadioButton("管理员");//创建单选按钮组ToggleGroup tg=new ToggleGroup();stuB.setToggleGroup(tg);teaB.setToggleGroup(tg);//创建单行面板HBox hBox1=new HBox();hBox1.setAlignment(Pos.CENTER);hBox1.setSpacing(10);hBox1.getChildren().addAll(stuB,teaB);//创建登录注册按钮Button loginB=new Button("登录");//给单选按钮组添加监听事件tg.selectedToggleProperty().addListener(((observableValue, toggle, t1) -> {if(t1.equals(stuB)){idL.setText("id");}else if(t1.equals(teaB)){idL.setText("姓名");}}));loginB.setOnAction(actionEvent -> {// 如果内容不为空try {String idS=tf.getText().trim();String passwordS=pf.getText().trim();Toggle selectedToggle = tg.getSelectedToggle(); // 获取选中的单选按钮if (selectedToggle != null && selectedToggle.equals(stuB)) { // 检查选中的按钮是否为学生按钮if (studentDao.login(idS,passwordS)) {studentView.stu_login(Integer.parseInt(idS));}else if((idS.isEmpty()||passwordS.isEmpty())){tipJframe("id或密码不能为空");}else{tipJframe("id或密码输入错误");}}else if(selectedToggle !=null && selectedToggle.equals(teaB)){if(judgeDao.magLogin(idS,passwordS)){magView.mag_login();}else if((idS.isEmpty()||passwordS.isEmpty())){tipJframe("姓名或密码不可为空");}else {tipJframe("姓名或密码输入错误");}}else {tipJframe("你是什么人");}} catch (Exception e) {e.printStackTrace();}});HBox hBox2=new HBox();hBox2.setAlignment(Pos.CENTER);hBox2.setSpacing(10);hBox2.getChildren().add(loginB);//将节点添加到面板当中gp.add(idL,0,0);gp.add(passwordL,0,1);gp.add(tf,1,0);gp.add(pf,1,1);gp.add(hBox1,1,2);gp.add(hBox2,1,3);gp.setAlignment(Pos.BASELINE_LEFT);//设置面板内边距gp.setPadding(new Insets(40,20,10,70));//设置面板垂直间距gp.setVgap(16);Scene scene=new Scene(gp,360,240);endView.stage.setScene(scene);}//增加弹出界面public static void tipJframe(String str){Label label=new Label(str);Button rebackB=new Button("返回");VBox vBox=new VBox(label,rebackB);vBox.setSpacing(10);vBox.setAlignment(Pos.CENTER);Scene scene=new Scene(vBox,150,100);Stage stage1=new Stage();stage1.setScene(scene);stage1.setTitle("提示");stage1.show();rebackB.setOnAction(actionEvent -> {stage1.close();});}
学生:
学生界面(studentView):
下面是我写的三个界面,和上面学生登录的图片对照,一共对应三个方法,当然后面两个方法(成绩查询和信息查询)会放到第一个stu_login的方法当中,根据按钮的动作监听而起作用
//学生登录后界面public static void stu_login(int stu_Id){Label label=new Label("学生查询系统");Button informationB=new Button("个人信息");Button gradeB=new Button("成绩查询");Button returnLogin=new Button("返回登录");//设置按钮字体颜色informationB.setTextFill(Color.BLUE);gradeB.setTextFill(Color.BLUE);returnLogin.setTextFill(Color.BLUE);//设置字体样式Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);label.setFont(font);informationB.setFont(font);gradeB.setFont(font);returnLogin.setFont(font);//为按钮添加监听事件//信息查询informationB.setOnAction(actionEvent -> {try {informationInquire(stu_Id);} catch (Exception e) {e.printStackTrace();}});//成绩查询gradeB.setOnAction(actionEvent -> {try {gradeInquire(stu_Id);} catch (Exception e) {throw new RuntimeException(e);}});//返回登录returnLogin.setOnAction(actionEvent -> otherView.login());//添加竖直方向面板VBox vBox=new VBox(20,label,informationB,gradeB,returnLogin);vBox.setAlignment(Pos.CENTER);Scene scene=new Scene(vBox,400,300);endView.stage.setScene(scene);}//学生信息查询public static void informationInquire(int stu_Id) throws Exception {//调用inquiry的返回值studentstudent student= studentDao.returnStudent(stu_Id);Label idL=new Label("学号:"+student.getStu_id());Label classL=new Label("班级:"+student.getStu_class());Label nameL=new Label("姓名:"+student.getStu_name());Label genderL=new Label("性别:"+student.getStu_gender());Label birthL=new Label("出生日期:"+student.getStu_birth());Label majorL=new Label("专业:"+student.getStu_major());//设置字体样式Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);idL.setFont(font);classL.setFont(font);nameL.setFont(font);genderL.setFont(font);birthL.setFont(font);majorL.setFont(font);Button backB=new Button("返回");backB.setOnAction(actionEvent -> {stu_login(stu_Id);});VBox vBox=new VBox(10);vBox.getChildren().addAll(idL,classL,nameL,genderL,birthL,majorL,backB);vBox.setAlignment(Pos.CENTER);vBox.setSpacing(10);Scene scene=new Scene(vBox,360,340);endView.stage.setScene(scene);}//学生成绩查询public static void gradeInquire(int stu_Id) throws Exception {int grade=studentDao.gradeOneself(stu_Id);Label gradeL=new Label("您的总学分为:"+grade);Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);gradeL.setFont(font);VBox vBox=new VBox();vBox.setAlignment(Pos.CENTER);vBox.setPadding(new Insets(0,0,20,0));Button back=new Button("返回");back.setMinWidth(50);back.setMinHeight(30);back.setOnAction(actionEvent -> {stu_login(stu_Id);});vBox.getChildren().addAll(gradeL,back);Scene scene=new Scene(vBox,340,150);endView.stage.setScene(scene);}
学生Dao语句:
utilDao:
private static final String URL = "jdbc:mysql:///studentMs";private static final String USER = "root";private static final String PASSWORD = "123321";//获取连接对象public static Connection getCon() {try {return DriverManager.getConnection(URL, USER, PASSWORD);} catch (SQLException e) {e.printStackTrace();return null;}}//关闭资源public static void close(ResultSet rs, PreparedStatement ps, Connection con) {try {if (rs != null) {rs.close();}if (ps != null) {ps.close();}if (con != null) {con.close();}} catch (SQLException e) {e.printStackTrace();}}//执行mysqlpublic static boolean exeUpdate(String sql, Object... params) {//获取连接对象Connection con = getCon();PreparedStatement ps = null;try {//获取编译对象ps = con.prepareStatement(sql);//判断参数是否为空if (Objects.nonNull(params)) {for (int i = 0; i < params.length; i++) {//实现占位赋值ps.setObject(i + 1, params[i]);}}//执行更新return ps.executeUpdate() > 0;} catch (Exception e) {e.printStackTrace();} finally {close(null, ps, con);}return false;}
studentDao*:
上面只展示了关于学生登录的jdbc语句,而不是我的studentDao中的所有语句,这样写是为让思路更加清晰,如果觉得很简单可以看我下面的总代码
//学生登录判断public static boolean login(String idStr, String passwordStr) throws Exception {String sql = "select * from student where stuID =? and stuPassword=?";PreparedStatement ps = utilDao.getCon().prepareStatement(sql);ps.setString(1,idStr);ps.setString(2, passwordStr);ResultSet rs = ps.executeQuery();if (rs.next()) {utilDao.close(rs,ps, utilDao.getCon());return true;} else {utilDao.close(rs,ps,utilDao.getCon());return false;}}//学生查询自己成绩public static Integer gradeOneself(int stu_id) throws Exception {String sql = "SELECT SUM(credit) AS gradesum FROM score WHERE stuID=?";try (Connection connection =utilDao.getCon();PreparedStatement ps = connection.prepareStatement(sql)) {ps.setInt(1, stu_id);try (ResultSet rs = ps.executeQuery()) {if (rs.next()) {int gradesum = rs.getInt("gradesum");return gradesum;} else {// 如果没有查询到结果,可以返回 null 或者其他合适的值return null;}}}}//返回学生信息public static student returnStudent(int stu_id)throws Exception{student student=new student();String sql="select stuID,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword from student where stuID=?";PreparedStatement ps=utilDao.getCon().prepareStatement(sql);//过滤其他的只留下ID为1的学生ps.setString(1,String.valueOf(stu_id));ResultSet rs=ps.executeQuery();if(rs.next()){student.setStu_id(rs.getInt("stuID"));student.setStu_class(rs.getString("stuClass"));student.setStu_name(rs.getString("stuName"));student.setStu_gender(rs.getString("stuSex"));student.setStu_birth(rs.getString("stuBirth"));student.setStu_major(rs.getString("stuMajor"));student.setStu_password(rs.getString("stuPassword"));}rs.close();ps.close();return student;}
--- 关于学生登录的代码就这些 ---
管理员:
管理员界面(magView):
下面是我关于管理员设置的界面方法,一共四个管理员的主界面,和页面展示中管理员登录的前四个相对应,下面一共7个方法,关于createTableView方法就是我写的用我的student等的对象添加到tableview中的一部分代码,只是拿出来重新定义了一个方法,大家不用在意,所以其他的四个创建界面的方法刚好与页面展示中管理员登录的前四个相对应,第一个是登录后的界面方法,后三个是登录后的面板中前三个的按钮的动作监听所调用的方法
//管理员登录界面public static void mag_login(){Label magL=new Label("管理员查询系统");VBox vBox=new VBox();vBox.setAlignment(Pos.CENTER);vBox.setSpacing(30);Button informationB= new Button("查询学生信息");Button courseB=new Button("查看课程");Button gradeB=new Button("查看学生成绩");Button rebackB=new Button("返回登录界面");rebackB.setOnAction(actionEvent -> {otherView.login();});informationB.setOnAction(actionEvent -> {try {magStudent();} catch (Exception e) {e.printStackTrace();}});courseB.setOnAction(actionEvent -> {try {magCourse();} catch (Exception e) {e.printStackTrace();}});gradeB.setOnAction(actionEvent -> {try {magScore();} catch (Exception e) {e.printStackTrace();}});rebackB.setOnAction(actionEvent -> {try {otherView.login();} catch (Exception e) {e.printStackTrace();}});Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);magL.setFont(font);informationB.setFont(font);courseB.setFont(font);gradeB.setFont(font);rebackB.setFont(font);magL.setTextFill(Color.BLACK);informationB.setTextFill(Color.BLUE);courseB.setTextFill(Color.BLUE);gradeB.setTextFill(Color.BLUE);rebackB.setTextFill(Color.BLUE);vBox.getChildren().addAll(magL,informationB,courseB,gradeB,rebackB);Scene scene=new Scene(vBox,360,400);endView.stage.setScene(scene);}//管理员查看学生信息界面public static void magStudent() throws Exception {BorderPane bp=new BorderPane();TableView<student> tableView = createTableView1();Label studentL=new Label("学生信息管理");Button addB=new Button("添加学生");Button updateB=new Button("修改学生");Button deleteB=new Button("删除学生");Button backB=new Button("返回");addB.setOnAction(actionEvent -> {try {studentUtil.studentAdd();} catch (Exception e) {throw new RuntimeException(e);}});updateB.setOnAction(actionEvent -> {try {studentUtil.studentUpdate();} catch (Exception e) {throw new RuntimeException(e);}});deleteB.setOnAction(actionEvent -> {try {studentUtil.studentDelete();} catch (Exception e) {throw new RuntimeException(e);}});backB.setOnAction(actionEvent -> {mag_login();});addB.setMinWidth(70);updateB.setMinWidth(70);deleteB.setMinWidth(70);addB.setMinHeight(35);updateB.setMinHeight(35);deleteB.setMinHeight(35);addB.setTextFill(Color.INDIANRED);updateB.setTextFill(Color.INDIANRED);deleteB.setTextFill(Color.INDIANRED);backB.setTextFill(Color.INDIANRED);addB.setBorder(Border.stroke(Color.LIGHTPINK));updateB.setBorder(Border.stroke(Color.LIGHTPINK));deleteB.setBorder(Border.stroke(Color.LIGHTPINK));backB.setBorder(Border.stroke(Color.LIGHTPINK));backB.setOnAction(actionEvent -> {mag_login();});Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);studentL.setFont(font);studentL.setTextFill(Color.RED);VBox vBox=new VBox();vBox.getChildren().addAll(studentL,addB,updateB,deleteB,backB);vBox.setAlignment(Pos.CENTER);vBox.setSpacing(20);vBox.setPadding(new Insets(50));BorderPane bp1=new BorderPane();bp1.setTop(vBox);BorderPane bp2=new BorderPane();bp2.setRight(tableView);// 获取学生列表,将集合转换成ObservableListArrayList<student> students = studentDao.initStudent();// 将学生列表转换为ObservableListObservableList<student> stuObs = FXCollections.observableArrayList(students);// 将 ObservableList 设置为 TableView 的数据源tableView.setItems(stuObs);bp.setLeft(bp1);bp.setCenter(bp2);Scene scene = new Scene(bp, 650, 350);endView.stage.setScene(scene);}//绑定学生面板public static TableView<student> createTableView1() {TableView<student> tableView = new TableView<>();TableColumn<student, Integer> idCol = new TableColumn<>("ID");idCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getStu_id()).asObject());TableColumn<student, String> classCol = new TableColumn<>("班级");classCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStu_class()));TableColumn<student, String> nameCol = new TableColumn<>("姓名");nameCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStu_name()));TableColumn<student, String> sexCol = new TableColumn<>("性别");sexCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStu_gender()));TableColumn<student, String> birthCol = new TableColumn<>("出生日期");birthCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStu_birth()));TableColumn<student, String> majorCol = new TableColumn<>("所在班级");majorCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStu_major()));// 添加其他表格列...tableView.getColumns().addAll(idCol, classCol, nameCol,sexCol,birthCol,majorCol);return tableView;}//管理员查看课程界面public static void magCourse() throws Exception {//创建面板BorderPane bp=new BorderPane();//创建第二面板TableView<course> tableView = createTableView2();Label studentL=new Label("学生课程管理");Button addB=new Button("添加课程");Button updateB=new Button("修改课程");Button deleteB=new Button("删除课程");Button backB=new Button("返回");addB.setOnAction(actionEvent -> {try {courseUtil.courseAdd();} catch (Exception e) {throw new RuntimeException(e);}});updateB.setOnAction(actionEvent -> {courseUtil.courseUpdate();});deleteB.setOnAction(actionEvent -> {courseUtil.courseDelete();});backB.setOnAction(actionEvent -> {mag_login();});addB.setMinWidth(70);updateB.setMinWidth(70);deleteB.setMinWidth(70);addB.setMinHeight(35);updateB.setMinHeight(35);deleteB.setMinHeight(35);addB.setTextFill(Color.INDIANRED);updateB.setTextFill(Color.INDIANRED);deleteB.setTextFill(Color.INDIANRED);backB.setTextFill(Color.INDIANRED);addB.setBorder(Border.stroke(Color.LIGHTPINK));updateB.setBorder(Border.stroke(Color.LIGHTPINK));deleteB.setBorder(Border.stroke(Color.LIGHTPINK));backB.setBorder(Border.stroke(Color.LIGHTPINK));Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);studentL.setFont(font);studentL.setTextFill(Color.RED);VBox vBox=new VBox();vBox.getChildren().addAll(studentL,addB,updateB,deleteB,backB);vBox.setAlignment(Pos.CENTER);vBox.setSpacing(20);vBox.setPadding(new Insets(50));BorderPane bp1=new BorderPane();bp1.setTop(vBox);BorderPane bp2=new BorderPane();bp2.setRight(tableView);// 获取学生列表ArrayList<course> courses = courseDao.initCourse();// 将学生列表转换为ObservableListObservableList<course> stuObs = FXCollections.observableArrayList(courses);// 将 ObservableList 设置为 TableView 的数据源tableView.setItems(stuObs);bp.setLeft(bp1);bp.setCenter(bp2);Scene scene = new Scene(bp, 700, 350);endView.stage.setScene(scene);}//绑定课程面板public static TableView<course> createTableView2() {TableView<course> tableView = new TableView<>();TableColumn<course, Integer> idCol = new TableColumn<>("课程号");idCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getCou_id()).asObject());TableColumn<course, String> majorCol = new TableColumn<>("所属专业");majorCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCou_major()));TableColumn<course, String> nameCol = new TableColumn<>("课程名称");nameCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCou_name()));TableColumn<course, String> typeCol = new TableColumn<>("课程类型");typeCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCou_type()));TableColumn<course, String> beginCol = new TableColumn<>("开课学期");beginCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCou_beginTime()));TableColumn<course, Integer> studyCol = new TableColumn<>("学时数");studyCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getCou_studyTime()).asObject());TableColumn<course, Integer> scoreCol = new TableColumn<>("学分");scoreCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getCou_score()).asObject());tableView.getColumns().addAll(idCol, majorCol, nameCol,typeCol,beginCol,studyCol,scoreCol);return tableView;}//管理员查看学生成绩界面public static void magScore() throws Exception {BorderPane bp=new BorderPane();TableView<score> tableView = createTableView3();Label studentL=new Label("学生成绩管理");Button addB=new Button("添加成绩");Button updateB=new Button("修改成绩");Button deleteB=new Button("删除成绩");Button rebackB=new Button("返回");addB.setOnAction(actionEvent -> {scoreUtil.scoreAdd();});updateB.setOnAction(actionEvent -> {scoreUtil.scoreUpdate();});deleteB.setOnAction(actionEvent -> {scoreUtil.scoreDelete();});rebackB.setOnAction(actionEvent -> {mag_login();});addB.setMinWidth(70);updateB.setMinWidth(70);deleteB.setMinWidth(70);addB.setMinHeight(35);updateB.setMinHeight(35);deleteB.setMinHeight(35);addB.setTextFill(Color.INDIANRED);updateB.setTextFill(Color.INDIANRED);deleteB.setTextFill(Color.INDIANRED);rebackB.setTextFill(Color.INDIANRED);addB.setBorder(Border.stroke(Color.LIGHTPINK));updateB.setBorder(Border.stroke(Color.LIGHTPINK));deleteB.setBorder(Border.stroke(Color.LIGHTPINK));rebackB.setBorder(Border.stroke(Color.LIGHTPINK));Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 20);studentL.setFont(font);studentL.setTextFill(Color.RED);VBox vBox=new VBox();vBox.getChildren().addAll(studentL,addB,updateB,deleteB,rebackB);vBox.setAlignment(Pos.CENTER);vBox.setSpacing(10);vBox.setPadding(new Insets(40));BorderPane bp1=new BorderPane();bp1.setTop(vBox);BorderPane bp2=new BorderPane();bp2.setRight(tableView);// 获取学生列表ArrayList<score> scores = scoreDao.initScore();// 将学生列表转换为ObservableListObservableList<score> stuObs = FXCollections.observableArrayList(scores);// 将 ObservableList 设置为 TableView 的数据源tableView.setItems(stuObs);bp.setLeft(bp1);bp.setCenter(bp2);Scene scene = new Scene(bp, 450, 300);endView.stage.setScene(scene);}//绑定成绩面板public static TableView<score> createTableView3() {TableView<score> tableView = new TableView<>();TableColumn<score, Integer> stuCol = new TableColumn<>("学号");stuCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getScore_id()).asObject());TableColumn<score, Integer> idCol = new TableColumn<>("课程号");idCol.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getScore_cid()).asObject());TableColumn<score, String> studyCol = new TableColumn<>("学数");studyCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getSocre_score()));TableColumn<score, String> scoreCol = new TableColumn<>("学分");scoreCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getScore_credit()));tableView.getColumns().addAll(stuCol,idCol,studyCol,scoreCol);return tableView;}
}
管理员工具类:
管理员工具类是对进入操作页面中的添加学生,修改学生,删除学生,添加课程...的操作
学生(studentUtil):
//添加学生信息public static void studentAdd() throws Exception {//添加面板GridPane gp=new GridPane();Label idL=new Label("学号");Label classL=new Label("班级");Label nameL=new Label("姓名");Label sexL=new Label("性别");Label birthL=new Label("出生日期");Label majorL=new Label("所在专业");Label passwordL=new Label("密码");//添加按钮Button admitB=new Button("提交");Button backB=new Button("返回");//设置字体样式Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);idL.setFont(font);classL.setFont(font);nameL.setFont(font);sexL.setFont(font);birthL.setFont(font);majorL.setFont(font);//添加输入框TextField idT=new TextField(String.valueOf(judgeDao.maxID()+1));TextField classT=new TextField();TextField nameT=new TextField();TextField sexT=new TextField();TextField birthT=new TextField();TextField majorT=new TextField();TextField passwordT=new TextField();//将组件添加到面板当中gp.add(idL,0,0);gp.add(idT,1,0);gp.add(classL,0,1);gp.add(classT,1,1);gp.add(nameL,0,2);gp.add(nameT,1,2);gp.add(sexL,0,3);gp.add(sexT,1,3);gp.add(birthL,0,4);gp.add(birthT,1,4);gp.add(majorL,0,5);gp.add(majorT,1,5);gp.add(passwordL,0,6);gp.add(passwordT,1,6);gp.add(admitB,0,7);gp.add(backB,1,7);//设置面板样式gp.setPadding(new Insets(30));gp.setVgap(20);gp.setHgap(10);Scene scene=new Scene(gp,300,400);Stage stage=new Stage();stage.setScene(scene);stage.setTitle("添加学生信息");stage.show();backB.setOnAction(actionEvent -> {try {stage.close();} catch (Exception e) {throw new RuntimeException(e);}});//给提交按钮添加监听事件admitB.setOnAction(actionEvent -> {try {// 获取文本框中的内容并去除首尾空白String id = idT.getText().trim();String clazz = classT.getText().trim();String name = nameT.getText().trim();String sex = sexT.getText().trim();String birth = birthT.getText().trim();String major = majorT.getText().trim();String password = passwordT.getText().trim();// 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作if (id.isEmpty() || clazz.isEmpty() || name.isEmpty() || sex.isEmpty() || birth.isEmpty() || major.isEmpty() || password.isEmpty()) {otherView.tipJframe("字段不能为空");return;}else if(!((sex.equals("男")||sex.equals("女")))){otherView.tipJframe("请输入正常性别");return;}else if(!judgeUtil.isValidDate(birth)){otherView.tipJframe("日期格式不正确,请重新输入");return;}else if(!clazz.equals(judgeUtil.isClass(id))){otherView.tipJframe("班级格式不正确,请重新输入");return;}else if(!password.equals(judgeUtil.isPassword(id))){otherView.tipJframe("密码格式错误,请重新输入");return;}int studentId = Integer.parseInt(id);// 调用Dao层方法尝试添加学生信息if (studentDao.addStudent(studentId, clazz, name, sex, birth, major, password)) {// 添加成功后的操作studentDao.initStudent();magView.magStudent();otherView.tipJframe("添加成功");}else {otherView.tipJframe("添加失败");}} catch (Exception e) {// 捕获其他异常并打印堆栈信息e.printStackTrace();otherView.tipJframe("操作失败:" + e.getMessage()); // 提示操作失败,并显示具体的异常信息}});}//修改学生信息public static void studentUpdate(){//创建第一个面板GridPane gp = new GridPane();Label idl = new Label("ID");TextField idt = new TextField();//创建第一个界面的按钮Button admitb = new Button("提交");Button backb = new Button("返回");gp.add(idl,0,0);gp.add(idt,1,0);gp.add(admitb,0,1);gp.add(backb,0,2);gp.setHgap(20);gp.setVgap(10);gp.setAlignment(Pos.CENTER);Scene scene=new Scene(gp,240,140);//创建第一个舞台Stage stage=new Stage();stage.setScene(scene);stage.setResizable(false);stage.setTitle("修改学生信息");stage.show();backb.setOnAction(actionEvent -> {try {stage.close();} catch (Exception e) {throw new RuntimeException(e);}});// 创建第二个舞台Stage stage1 = new Stage();//创建只能存储一个字符的字符串final int[] id = new int[1];final String[] clas = new String[1];final String[] names = new String[1];final String[] sexs = new String[1];final String[] births = new String[1];final String[] majors = new String[1];final String[] passwords=new String[1];GridPane gp1=new GridPane();Label idL=new Label("学号");Label classL=new Label("班级");Label nameL=new Label("姓名");Label sexL=new Label("性别");Label birthL=new Label("出生日期");Label majorL=new Label("所在专业");Label passwordL=new Label("密码");Button admitB=new Button("提交");Button rebackB=new Button("返回");rebackB.setOnAction(actionEvent -> {stage1.close();});//创建显示信息文本框TextField idT=new TextField(String.valueOf(id[0]));TextField classT=new TextField(clas[0]);TextField nameT=new TextField(names[0]);TextField sexT=new TextField(sexs[0]);TextField birthT=new TextField(births[0]);TextField majorT=new TextField(majors[0]);TextField passwordT=new TextField(passwords[0]);Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);idL.setFont(font);classL.setFont(font);nameL.setFont(font);sexL.setFont(font);birthL.setFont(font);majorL.setFont(font);gp1.add(idL,0,0);gp1.add(idT,1,0);gp1.add(classL,0,1);gp1.add(classT,1,1);gp1.add(nameL,0,2);gp1.add(nameT,1,2);gp1.add(sexL,0,3);gp1.add(sexT,1,3);gp1.add(birthL,0,4);gp1.add(birthT,1,4);gp1.add(majorL,0,5);gp1.add(majorT,1,5);gp1.add(passwordL,0,6);gp1.add(passwordT,1,6);gp1.add(admitB,0,7);gp1.add(rebackB,1,7);gp1.setPadding(new Insets(30));gp1.setVgap(20);gp1.setHgap(10);Scene scene1=new Scene(gp1,300,400);stage1.setScene(scene1);stage1.setResizable(false);stage1.setTitle("修改学生信息");stage1.close();//第一个界面的提交按钮admitb.setOnAction(actionEvent -> {String idText = idt.getText().trim();if (!idText.isEmpty()) {try {if (studentDao.isStudent(Integer.parseInt(idText))) {endView.student0=studentDao.returnStudent(Integer.parseInt(idText));id[0] = endView.student0.getStu_id();clas[0] = endView.student0.getStu_class();names[0] = endView.student0.getStu_name();sexs[0] = endView.student0.getStu_gender();births[0] = endView.student0.getStu_birth();majors[0] = endView.student0.getStu_major();passwords[0]=endView.student0.getStu_password();// 将值设置到文本框中idT.setText(String.valueOf(id[0]));classT.setText(clas[0]);nameT.setText(names[0]);sexT.setText(sexs[0]);birthT.setText(births[0]);majorT.setText(majors[0]);passwordT.setText(passwords[0]);// 显示舞台stage1.show();} else {otherView.tipJframe("不存在该学生");}} catch (Exception e) {throw new RuntimeException(e);}} else {otherView.tipJframe("ID不能为空");}});//第二个界面的提交按钮admitB.setOnAction(actionEvent -> {try {String idd = idT.getText().trim();String clazz = classT.getText().trim();String name = nameT.getText().trim();String sex = sexT.getText().trim();String birth = birthT.getText().trim();String major = majorT.getText().trim();String password=passwordT.getText().trim();// 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作if (idd.isEmpty() || clazz.isEmpty() || name.isEmpty() || sex.isEmpty() || birth.isEmpty() || major.isEmpty()) {otherView.tipJframe("所有字段不能为空");return;}else if(!((sex.equals("男")||sex.equals("女")))){otherView.tipJframe("请输入正常性别");return;}else if(!judgeUtil.isValidDate(birth)){otherView.tipJframe("日期格式不正确,请重新输入");return;}else if(!clazz.equals(judgeUtil.isClass(idd))){otherView.tipJframe("班级格式不正确,请重新输入");return;}else if(!password.equals(judgeUtil.isPassword(idd))){otherView.tipJframe("密码格式错误,请重新输入");return;}int studentId = Integer.parseInt(idd);//判断如果学号发生改变,则进行添加学生操作,否者进行修改操作//如果发生修改if(id[0]!=Integer.valueOf(idT.getText().trim())){if (studentDao.addStudent(studentId, clazz, name, sex, birth, major,password)) {// 添加成功后的操作if(studentDao.deleteStudent(id[0])){studentDao.initStudent();magView.magStudent();otherView.tipJframe("修改成功");}} else {otherView.tipJframe("修改失败");}}//如果没有发生修改,那么进行修改操作else {if (studentDao.updateStudent(studentId, clazz, name, sex, birth, major,password)) {// 添加成功后的操作studentDao.initStudent();magView.magStudent();otherView.tipJframe("修改成功");} else {otherView.tipJframe("修改失败");}}} catch (Exception e) {e.printStackTrace();otherView.tipJframe("操作失败:" + e.getMessage());}});}//删除学生信息public static void studentDelete(){GridPane gp = new GridPane();Label idL = new Label("ID");TextField idT = new TextField();Button admitB = new Button("提交");Button backB = new Button("返回");// 将按钮事件处理移到这里,确保在点击按钮时获取最新的文本框内容gp.add(idL, 0, 0);gp.add(idT, 1, 0);gp.add(admitB, 0, 1);gp.add(backB, 1, 1);gp.setAlignment(Pos.CENTER);gp.setHgap(10);gp.setVgap(30);//创建一个删除学生的界面舞台Stage stage = new Stage();Scene scene = new Scene(gp, 240, 100);stage.setScene(scene);stage.setTitle("删除学生信息");stage.setResizable(false);stage.show();//如果舞台退出,则界面关闭backB.setOnAction(actionEvent -> {try {stage.close();} catch (Exception e) {throw new RuntimeException(e);}});//对按钮添加点击事件admitB.setOnAction(actionEvent -> {String ids = idT.getText().trim();if (!ids.isEmpty()) {try {int id = Integer.parseInt(ids);if (studentDao.isStudent(id)) {if (studentDao.deleteStudent(id)) {otherView.tipJframe("学生删除成功");//更新数据库信息studentDao.initStudent();//更新面板信息magView.magStudent();} else {otherView.tipJframe("学生删除失败");}} else {otherView.tipJframe("未找到该学生");}} catch (NumberFormatException e) {otherView.tipJframe("请输入有效的数字ID");} catch (Exception e) {e.printStackTrace();otherView.tipJframe("数据库操作失败");}} else {otherView.tipJframe("请输入学生ID");}});}
课程(courseUtil):
//添加课程public static void courseAdd() throws Exception {//创建面板GridPane gp=new GridPane();Label cIDL=new Label("课程号");Label cMajorL=new Label("所属专业");Label cNameL=new Label("课程名称");Label cTypeL=new Label("课程类型");Label cBeginL=new Label("开课学期");Label cStudyL=new Label("学时数");Label cCreditL=new Label("学分");//创建按钮Button admitB=new Button("提交");Button backB=new Button("返回");//创建字体样式Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);cIDL.setFont(font);cMajorL.setFont(font);cNameL.setFont(font);cTypeL.setFont(font);cBeginL.setFont(font);cStudyL.setFont(font);cCreditL.setFont(font);//创建文本输入框TextField cIDT=new TextField(String.valueOf(judgeDao.maxcID()+1));TextField cMajorT=new TextField();TextField cNameT=new TextField();TextField cTypeT=new TextField();TextField cBeginT=new TextField();TextField cStudyT=new TextField();TextField cCreditT=new TextField();//将节点添加到面板当中gp.add(cIDL,0,0);gp.add(cIDT,1,0);gp.add(cMajorL,0,1);gp.add(cMajorT,1,1);gp.add(cNameL,0,2);gp.add(cNameT,1,2);gp.add(cTypeL,0,3);gp.add(cTypeT,1,3);gp.add(cBeginL,0,4);gp.add(cBeginT,1,4);gp.add(cStudyL,0,5);gp.add(cStudyT,1,5);gp.add(cCreditL,0,6);gp.add(cCreditT,1,6);gp.add(admitB,0,7);gp.add(backB,1,7);//设置面板gp.setPadding(new Insets(30));gp.setVgap(20);gp.setHgap(10);Scene scene=new Scene(gp,350,400);Stage stage=new Stage();stage.setResizable(false);stage.setTitle("添加课程");stage.setScene(scene);stage.show();backB.setOnAction(actionEvent -> {try {stage.close();} catch (Exception e) {throw new RuntimeException(e);}});admitB.setOnAction(actionEvent -> {try {// 获取文本框中的内容并去除首尾空白String id = cIDT.getText().trim();String major = cMajorT.getText().trim();String name = cNameT.getText().trim();String type = cTypeT.getText().trim();String begin = cBeginT.getText().trim();String study = cStudyT.getText().trim();String credit = cCreditT.getText().trim();// 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作if ((id.isEmpty() || major.isEmpty() || name.isEmpty() || type.isEmpty() || begin.isEmpty() || study.isEmpty() || credit.isEmpty())) {otherView.tipJframe("字段不能为空");return;}// 尝试将id和password转换为整数,如果格式不正确会抛出NumberFormatExceptionint cId = Integer.parseInt(id);int studytime=Integer.parseInt(study);int creditt=Integer.parseInt(credit);if (courseDao.addCourse(cId, major, name, type, begin, studytime, creditt)) {// 添加成功后的操作courseDao.initCourse();magView.magCourse();otherView.tipJframe("课程添加成功");} else {otherView.tipJframe("课程添加失败");}} catch (Exception e) {// 捕获其他异常并打印堆栈信息e.printStackTrace();otherView.tipJframe("操作失败:" + e.getMessage());}});}//修改课程public static void courseUpdate(){//创建第一个面板GridPane gp = new GridPane();Label idl = new Label("cID");TextField idt = new TextField();//创建第一个界面的按钮Button admitb = new Button("提交");Button backB = new Button("返回");gp.add(idl,0,0);gp.add(idt,1,0);gp.add(admitb,0,1);gp.add(backB,0,2);gp.setHgap(20);gp.setVgap(10);gp.setAlignment(Pos.CENTER);Scene scene=new Scene(gp,250,140);Stage stage=new Stage();stage.setTitle("修改课程");stage.setScene(scene);stage.show();backB.setOnAction(actionEvent -> {try {stage.close();} catch (Exception e) {throw new RuntimeException(e);}});//第二个舞台Stage stage1 = new Stage();//创建只能存储一个字符串的数组final int[] id = new int[1];final String[] major = new String[1];final String[] name = new String[1];final String[] type = new String[1];final String[] begin = new String[1];final int[] studytime = new int[1];final int[] credit = new int[1];// 创建显示信息文本框TextField idT = new TextField(String.valueOf(id[0]));TextField majorT = new TextField(major[0]);TextField nameT = new TextField(name[0]);TextField typeT = new TextField(type[0]);TextField beginT = new TextField(begin[0]);TextField studyTime = new TextField(String.valueOf(studytime[0]));TextField creditT = new TextField(String.valueOf(credit[0]));//创建第二个面板GridPane gp1=new GridPane();Label cID=new Label("课程号");Label cMajor=new Label("所属专业");Label cName=new Label("课程名称");Label cType=new Label("课程类型");Label cBegin=new Label("开课学期");Label cStudy=new Label("学时数");Label cCredit=new Label("学分");Button admitB=new Button("提交");Button rebackB=new Button("返回");rebackB.setOnAction(actionEvent -> {try {stage1.close();} catch (Exception e) {throw new RuntimeException(e);}});//创建字体样式Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);cID.setFont(font);cMajor.setFont(font);cName.setFont(font);cType.setFont(font);cBegin.setFont(font);cStudy.setFont(font);cCredit.setFont(font);gp1.add(cID,0,0);gp1.add(idT,1,0);gp1.add(cMajor,0,1);gp1.add(majorT,1,1);gp1.add(cName,0,2);gp1.add(nameT,1,2);gp1.add(cType,0,3);gp1.add(typeT,1,3);gp1.add(cBegin,0,4);gp1.add(beginT,1,4);gp1.add(cStudy,0,5);gp1.add(studyTime,1,5);gp1.add(cCredit,0,6);gp1.add(creditT,1,6);gp1.add(admitB,0,7);gp1.add(rebackB,1,7);gp1.setPadding(new Insets(30));gp1.setVgap(20);gp1.setHgap(10);Scene scene1=new Scene(gp1,300,400);stage1.setScene(scene1);stage1.setTitle("修改课程");stage1.setResizable(false);stage1.close();String idText = idt.getText().trim();//创建第一个按钮的点击事件admitb.setOnAction(actionEvent -> {if (idText.isEmpty()) {otherView.tipJframe("id不能为空");}else {try {if (courseDao.isCourse(Integer.parseInt(idText))) {endView.course0 = courseDao.returnCourse(Integer.parseInt(idText));//给数组字符串赋值id[0] = endView.course0.getCou_id();major[0] = endView.course0.getCou_major();name[0] = endView.course0.getCou_name();type[0] = endView.course0.getCou_type();begin[0] = endView.course0.getCou_beginTime();studytime[0] = endView.course0.getCou_studyTime();credit[0] = endView.course0.getCou_score();// 更新界面元素,确保在JavaFX应用线程上更新Platform.runLater(() -> {idT.setText(String.valueOf(id[0]));majorT.setText(major[0]);nameT.setText(name[0]);typeT.setText(type[0]);beginT.setText(begin[0]);studyTime.setText(String.valueOf(studytime[0]));creditT.setText(String.valueOf(credit[0]));});// 显示舞台stage1.show();} else {otherView.tipJframe("不存在该课程");}} catch (Exception e) {otherView.tipJframe("发生异常: " + e.getMessage());}}});//创建第二个面板的按钮的点击事件admitB.setOnAction(actionEvent -> {try {// 获取文本框中的内容并去除首尾空白String ids = idT.getText().trim();String majors = majorT.getText().trim();String names = nameT.getText().trim();String types = typeT.getText().trim();String begins = beginT.getText().trim();String studys = studyTime.getText().trim();String credits = creditT.getText().trim();// 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作if (ids.isEmpty() || majors.isEmpty() || names.isEmpty() || types.isEmpty() || begins.isEmpty() || studys.isEmpty() || credits.isEmpty()) {otherView.tipJframe("字段不能为空");return;}int cId = Integer.parseInt(ids);int study=Integer.parseInt(studys);int creditt=Integer.parseInt(credits);if(!(id[0]==Integer.valueOf(idText))){if(courseDao.addCourse(cId, majors, names, types, begins, study, creditt)){courseDao.deleteCourse(id[0]);courseDao.initCourse();magView.magCourse();otherView.tipJframe("课程修改成功");}else {// 添加失败的提示otherView.tipJframe("课程修改失败");}}else if(id[0]==Integer.valueOf(idText)){// 调用Dao层方法尝试添加学生信息if (courseDao.updateCourse(cId, majors, names, types, begins, study, creditt)) {// 添加成功后的操作courseDao.initCourse();magView.magCourse();otherView.tipJframe("课程修改成功");} else {// 添加失败的提示otherView.tipJframe("课程修改失败");}}} catch (Exception e) {e.printStackTrace();otherView.tipJframe("操作失败:" + e.getMessage()); // 提示操作失败,并显示具体的异常信息}});}//删除课程public static void courseDelete(){GridPane gp = new GridPane();Label idL = new Label("ID");TextField idT = new TextField();Button admitB = new Button("提交");Button backB = new Button("返回");gp.add(idL, 0, 0);gp.add(idT, 1, 0);gp.add(admitB, 0, 1);gp.add(backB, 1, 1);gp.setAlignment(Pos.CENTER);gp.setHgap(10);gp.setVgap(30);Stage stage = new Stage();Scene scene = new Scene(gp, 240, 100);stage.setTitle("删除课程");stage.setScene(scene);stage.show();backB.setOnAction(actionEvent -> {try {stage.close();} catch (Exception e) {throw new RuntimeException(e);}});// 将按钮事件处理移到这里,确保在点击按钮时获取最新的文本框内容admitB.setOnAction(actionEvent -> {String ids = idT.getText().trim(); // 获取文本框内容if (!ids.isEmpty()) {try {int id = Integer.parseInt(ids);if (courseDao.isCourse(id)) {if (courseDao.deleteCourse(id)) {otherView.tipJframe("课程删除成功");courseDao.initCourse(); // 更新数据magView.magCourse(); // 刷新界面信息} else {otherView.tipJframe("课程删除失败");}} else {otherView.tipJframe("未找到该课程");}} catch (Exception e) {e.printStackTrace();otherView.tipJframe("数据库操作失败");}} else {otherView.tipJframe("请输入课程ID");}});}
分数(scoreUtil):
//添加成绩public static void scoreAdd(){GridPane gp=new GridPane();Label sID=new Label("学号");Label sCID=new Label("课程号");Label sStudy=new Label("成绩");Label sCredit=new Label("学分");Button admitB=new Button("提交");Button rebackB=new Button("返回");Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);sID.setFont(font);sCID.setFont(font);sStudy.setFont(font);sCredit.setFont(font);TextField sIDT=new TextField();TextField scIDT=new TextField();TextField sStudyT=new TextField();TextField sCreditT=new TextField();gp.add(sID,0,0);gp.add(sIDT,1,0);gp.add(sCID,0,1);gp.add(scIDT,1,1);gp.add(sStudy,0,2);gp.add(sStudyT,1,2);gp.add(sCredit,0,3);gp.add(sCreditT,1,3);gp.add(admitB,0,7);gp.add(rebackB,1,7);gp.setPadding(new Insets(30));gp.setVgap(20);gp.setHgap(10);Scene scene=new Scene(gp,280,300);Stage stage1=new Stage();stage1.setScene(scene);stage1.setTitle("添加分数");stage1.show();rebackB.setOnAction(actionEvent -> {try {stage1.close();} catch (Exception e) {throw new RuntimeException(e);}});admitB.setOnAction(actionEvent -> {try {// 获取文本框中的内容并去除首尾空白String id = sIDT.getText().trim();String cid = scIDT.getText().trim();String study = sStudyT.getText().trim();String credit = sCreditT.getText().trim();// 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作if ((id.isEmpty() || cid.isEmpty() || study.isEmpty() || credit.isEmpty())) {otherView.tipJframe("字段不能为空");return;}else if(!studentDao.isStudent(Integer.parseInt(id))){otherView.tipJframe("不存在该学生");return;}else if(!courseDao.isCourse(Integer.parseInt(cid))){otherView.tipJframe("不存在该课程");return;}int Id=Integer.parseInt(id);int cId = Integer.parseInt(cid);if (scoreDao.addScore(Id, cId, study, credit)&&scoreDao.isScore(Id,cId)) {scoreDao.initScore();magView.magScore();otherView.tipJframe("成绩添加成功");} else {// 添加失败的提示otherView.tipJframe("成绩添加失败");}} catch (NumberFormatException e) {otherView.tipJframe("ID格式错误,请输入有效的整数");} catch (Exception e) {// 捕获其他异常并打印堆栈信息e.printStackTrace();otherView.tipJframe("操作失败:" + e.getMessage());}});}//修改成绩public static void scoreUpdate(){GridPane gp=new GridPane();Label idl = new Label("ID");Label cidl=new Label("cID");TextField idt = new TextField();TextField cidt=new TextField();Button admitb = new Button("提交");Button backB=new Button("返回");gp.add(idl,0,0);gp.add(idt,1,0);gp.add(cidl,0,1);gp.add(cidt,1,1);gp.add(admitb,0,2);gp.add(backB,1,2);gp.setHgap(10);gp.setVgap(20);gp.setAlignment(Pos.CENTER);Scene scene=new Scene(gp,300,240);//创建第一个舞台Stage stage=new Stage();stage.setScene(scene);stage.setResizable(false);stage.setTitle("修改分数");stage.show();backB.setOnAction(actionEvent -> {stage.close();});//创建第二个面板GridPane gp1 = new GridPane();final int[] id = new int[1];final int[] cid = new int[1];TextField sIDT=new TextField(String.valueOf(id[0]));TextField scIDT=new TextField(String.valueOf(cid[0]));TextField sStudyT=new TextField();TextField sCreditT=new TextField();Label sID=new Label("学号");Label sCID=new Label("课程号");Label sStudy=new Label("成绩");Label sCredit=new Label("学分");Button admitB=new Button("提交");Button rebackB=new Button("返回");Font font = Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 15);sID.setFont(font);sCID.setFont(font);sStudy.setFont(font);sCredit.setFont(font);gp1.add(sID,0,0);gp1.add(sIDT,1,0);gp1.add(sCID,0,1);gp1.add(scIDT,1,1);gp1.add(sStudy,0,2);gp1.add(sStudyT,1,2);gp1.add(sCredit,0,3);gp1.add(sCreditT,1,3);gp1.add(admitB,0,7);gp1.add(rebackB,1,7);gp1.setPadding(new Insets(30));gp1.setVgap(20);gp1.setHgap(10);Scene scene1=new Scene(gp1,280,300);//创建第二个舞台Stage stage1 = new Stage();stage1.setScene(scene1);stage1.setTitle("修改分数");stage1.setResizable(false);stage1.close();rebackB.setOnAction(actionEvent -> {try {stage1.close();} catch (Exception e) {throw new RuntimeException(e);}});// 登录按钮的事件处理器admitb.setOnAction(actionEvent -> {String idText = idt.getText().trim();String cidText = cidt.getText().trim();if (!(idText.isEmpty() || cidText.isEmpty())) {try {if (judgeUtil.isDigits(idText) && judgeUtil.isDigits(cidText)) {if (scoreDao.isScore(Integer.parseInt(idText), Integer.parseInt(cidText))) {endView.score0 = scoreDao.returnScore(Integer.parseInt(idText),Integer.parseInt(cidText));id[0] = endView.score0.getScore_id();cid[0] = endView.score0.getScore_cid();// 更新TextField显示sIDT.setText(String.valueOf(id[0]));scIDT.setText(String.valueOf(cid[0]));// 显示舞台stage1.show();stage.close();} else if (!studentDao.isStudent(Integer.parseInt(idText))) {otherView.tipJframe("不存在该学生");} else if (!courseDao.isCourse(Integer.parseInt(cidText))) {otherView.tipJframe("不存在该课程");} else {otherView.tipJframe("信息输入错误");}} else {otherView.tipJframe("格式错误");}} catch (Exception e) {throw new RuntimeException(e);}} else {otherView.tipJframe("id不能为空");}});//第二个按钮点击事件admitB.setOnAction(actionEvent -> {try {// 获取文本框中的内容并去除首尾空白String ids = sIDT.getText().trim();String cids = scIDT.getText().trim();String studys = sStudyT.getText().trim();String credits = sCredit.getText().trim();// 首先进行基本的非空验证,如果有任何一个字段为空,则不进行添加操作if (ids.isEmpty() || cids.isEmpty() || studys.isEmpty() || credits.isEmpty()) {otherView.tipJframe("字段不能为空");return;}// 尝试将id和password转换为整数,如果格式不正确会抛出NumberFormatExceptionint Id=Integer.parseInt(ids);int cId = Integer.parseInt(cids);// 调用Dao层方法尝试添加学生信息if (scoreDao.updateScore(Id, cId, studys, credits)) {// 添加成功后的操作scoreDao.initScore();magView.magScore();otherView.tipJframe("成绩添加成功");} else {otherView.tipJframe("成绩添加失败");}} catch (NumberFormatException e) {otherView.tipJframe("ID格式错误,请输入有效的整数");} catch (Exception e) {e.printStackTrace();otherView.tipJframe("操作失败:" + e.getMessage());}});}//删除成绩public static void scoreDelete(){GridPane gp = new GridPane();Label idL = new Label("ID");Label cidL=new Label("cID");TextField idT = new TextField();TextField cidT=new TextField();Button admitB = new Button("提交");Button backB = new Button("返回");gp.add(idL, 0, 0);gp.add(idT, 1, 0);gp.add(cidL,0,1);gp.add(cidT,1,1);gp.add(admitB, 0, 2);gp.add(backB, 1, 2);gp.setAlignment(Pos.CENTER);gp.setHgap(10);gp.setVgap(30);Stage stage = new Stage();Scene scene = new Scene(gp, 240, 200);stage.setScene(scene);stage.setTitle("删除分数");stage.show();backB.setOnAction(actionEvent -> {try {stage.close();} catch (Exception e) {throw new RuntimeException(e);}});// 将按钮事件处理移到这里,确保在点击按钮时获取最新的文本框内容admitB.setOnAction(actionEvent -> {String ids = idT.getText().trim();String cids=cidT.getText().trim();if (!ids.isEmpty()) {try {int id = Integer.parseInt(ids);int cid=Integer.parseInt(cids);if (studentDao.isStudent(id)) {if (scoreDao.deleteScore(id,cid)) {otherView.tipJframe("课程删除成功");scoreDao.initScore();magView.magScore();} else {otherView.tipJframe("课程删除失败");}} else {otherView.tipJframe("未找到该课程");}} catch (NumberFormatException e) {otherView.tipJframe("请输入有效的数字ID");} catch (Exception e) {e.printStackTrace();otherView.tipJframe("数据库操作失败");}} else {otherView.tipJframe("请输入课程ID");}});}
判断工具类(judgeUtil):
这个工具类是对添加数据时一些文本框内字符串的判断,是在上面三个util中要用到的
//判断日期格式public static boolean isValidDate(String dateStr) {try {DateTimeFormatter.ofPattern("yyyy-MM-dd").parse(dateStr);return true;} catch (DateTimeParseException e) {return false;}}//判断班级格式public static String isClass(String s){String numberString = s;int numberOfCharsToTake = 6; // 指定你想截取的字符数量String prefix = numberString.substring(0, numberOfCharsToTake);return prefix;}//判断密码格式public static String isPassword(String s){String numberString = s;int numberOfCharsToTake = 6; // 指定你想截取的字符数量从末尾开始int startIndex = numberString.length() - numberOfCharsToTake;String suffix = numberString.substring(startIndex);return suffix;}//判断字符串是否为数字public static boolean isDigits(String str) {return str.matches("\\d+");}
管理员Dao类:
utilDao:
由于Connection,close,exeupdate会被经常用到,所以在这里我就直接写成三个方法了
private static final String URL = "jdbc:mysql:///studentMs";private static final String USER = "root";private static final String PASSWORD = "123321";//获取连接对象public static Connection getCon() {try {return DriverManager.getConnection(URL, USER, PASSWORD);} catch (SQLException e) {e.printStackTrace();return null;}}//关闭资源public static void close(ResultSet rs, PreparedStatement ps, Connection con) {try {if (rs != null) {rs.close();}if (ps != null) {ps.close();}if (con != null) {con.close();}} catch (SQLException e) {e.printStackTrace();}}//执行mysqlpublic static boolean exeUpdate(String sql, Object... params) {//获取连接对象Connection con = getCon();PreparedStatement ps = null;try {//获取编译对象ps = con.prepareStatement(sql);//判断参数是否为空if (Objects.nonNull(params)) {for (int i = 0; i < params.length; i++) {//实现占位赋值ps.setObject(i + 1, params[i]);}}//执行更新return ps.executeUpdate() > 0;} catch (Exception e) {e.printStackTrace();} finally {close(null, ps, con);}return false;}
studentDao*:
下面的studentDao只与管理员的界面操作有关
//返回学生信息public static student returnStudent(int stu_id)throws Exception{student student=new student();String sql="select stuID,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword from student where stuID=?";PreparedStatement ps=utilDao.getCon().prepareStatement(sql);//过滤其他的只留下ID为1的学生ps.setString(1,String.valueOf(stu_id));ResultSet rs=ps.executeQuery();if(rs.next()){student.setStu_id(rs.getInt("stuID"));student.setStu_class(rs.getString("stuClass"));student.setStu_name(rs.getString("stuName"));student.setStu_gender(rs.getString("stuSex"));student.setStu_birth(rs.getString("stuBirth"));student.setStu_major(rs.getString("stuMajor"));student.setStu_password(rs.getString("stuPassword"));}rs.close();ps.close();return student;}//判断是否存在该学生public static boolean isStudent(int stu_id) throws SQLException {String sql="select stuID,stuClass,stuName,stuSex,stuBirth,stuMajor from student where stuID=?";PreparedStatement ps=utilDao.getCon().prepareStatement(sql);//过滤其他的只留下ID为1的学生ps.setString(1,String.valueOf(stu_id));ResultSet rs=ps.executeQuery();if(rs.next()){return true;}else {return false;}}// 学生类信息初始化public static ArrayList<student> initStudent() throws Exception {ArrayList<student> students = new ArrayList<>();String sql = "select * from student order by stuID asc";PreparedStatement ps = utilDao.getCon().prepareStatement(sql);ResultSet rs = ps.executeQuery();// 使用 while 循环遍历 ResultSet 中的所有行while (rs.next()) {student student = new student();student.setStu_id(rs.getInt("stuID"));student.setStu_class(rs.getString("stuClass"));student.setStu_name(rs.getString("stuName"));student.setStu_gender(rs.getString("stuSex"));student.setStu_birth(rs.getString("stuBirth"));student.setStu_major(rs.getString("stuMajor"));students.add(student);}// 关闭资源rs.close();ps.close();return students;}//添加学生public static boolean addStudent(int stuId,String stuClass,String stuName, String stuSex, String stuBirth, String stuMajor,String stuPassword)throws Exception{String sql="insert into student(stuID,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword )values (?,?,?,?,?,?,?) ";return utilDao.exeUpdate(sql,stuId,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword);}//修改学生public static boolean updateStudent(int stuId,String stuClass,String stuName, String stuSex, String stuBirth, String stuMajor,String stuPassword)throws Exception{String sql="update student set stuID=?,stuClass=?,stuName=?,stuSex=?,stuBirth=?,stuMajor=?,stuPassword=?";return utilDao.exeUpdate(sql,stuId,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword);}//删除学生public static boolean deleteStudent(int stuId){String sql="delete from student where stuID=?";return utilDao.exeUpdate(sql,stuId);}
courseDao:
//返回课程public static course returnCourse(int cID)throws Exception{course course=new course();String sql="select cID,cMajor,cName,cType,cStartTerm,cPeriod,cCredit from course where cID=?";PreparedStatement ps=utilDao.getCon().prepareStatement(sql);//过滤其他的只留下ID为1的学生ps.setString(1,String.valueOf(cID));ResultSet rs=ps.executeQuery();if(rs.next()){course.setCou_id(rs.getInt("cID"));course.setCou_major(rs.getString("cMajor"));course.setCou_name(rs.getString("cName"));course.setCou_type(rs.getString("cType"));course.setCou_beginTime(rs.getString("cStartTerm"));course.setCou_studyTime(rs.getInt("cPeriod"));course.setCou_score(rs.getInt("cCredit"));}rs.close();ps.close();return course;}//判断是否存在该课程public static boolean isCourse(int cID) throws Exception {String sql="select cID,cMajor,cName,cType,cStartTerm,cPeriod,cCredit from course where cID=?";PreparedStatement ps=utilDao.getCon().prepareStatement(sql);//过滤其他的只留下ID为1的学生ps.setInt(1,cID);ResultSet rs=ps.executeQuery();if(rs.next()){return true;} else {return false;}}// 学生类集合课程初始化public static ArrayList<course> initCourse() throws Exception {ArrayList<course> courses = new ArrayList<>();String sql = "select * from course order by cID asc";PreparedStatement ps = utilDao.getCon().prepareStatement(sql);ResultSet rs = ps.executeQuery();// 使用 while 循环遍历 ResultSet 中的所有行while (rs.next()) {course course = new course();course.setCou_id(rs.getInt("cID"));course.setCou_major(rs.getString("cMajor"));course.setCou_name(rs.getString("cName"));course.setCou_type(rs.getString("cType"));course.setCou_beginTime(rs.getString("cStartTerm"));course.setCou_studyTime(rs.getInt("cPeriod"));course.setCou_score(rs.getInt("cCredit"));courses.add(course);}// 关闭资源rs.close();ps.close();return courses;}//增加课程public static boolean addCourse(int cID, String cMajor, String cName, String cType, String cStartTerm, int cPeriod, int cCredit){String sql="insert into course(cID,cMajor,cName,cType,cStartTerm,cPeriod,cCredit)values (?,?,?,?,?,?,?) ";return utilDao.exeUpdate(sql,cID,cMajor,cName,cType,cStartTerm,cPeriod,cCredit);}//修改课程public static boolean updateCourse(int cID, String cMajor, String cName, String cType, String cStartTerm, int cPeriod, int cCredit){String sql="update course set cID=?,cMajor=?,cName=?,cType=?,cStartTerm=?,cPeriod=?,cCredit=? ";return utilDao.exeUpdate(sql,cID,cMajor,cName,cType,cStartTerm,cPeriod,cCredit);}//删除课程public static boolean deleteCourse(int cID){String sql="delete from course where cID=?";return utilDao.exeUpdate(sql,cID);}
scoreDao:
//判断是否存在该成绩public static boolean isScore(int stu_id,int cid) throws SQLException {String sql="select * from score where stuID=? and cID=?";PreparedStatement ps=utilDao.getCon().prepareStatement(sql);//过滤其他的只留下ID为1的学生ps.setString(1,String.valueOf(stu_id));ps.setString(2,String.valueOf(cid));ResultSet rs=ps.executeQuery();if(rs.next()){return true;}return false;}// 学生类成绩初始化public static ArrayList<score> initScore() throws Exception {ArrayList<score> scores = new ArrayList<>();String sql = "select * from score order by stuID asc ";PreparedStatement ps = utilDao.getCon().prepareStatement(sql);ResultSet rs = ps.executeQuery();// 使用 while 循环遍历 ResultSet 中的所有行while (rs.next()) {score score = new score();score.setScore_id(rs.getInt("stuID"));score.setScore_cid(rs.getInt("cID"));score.setSocre_score(rs.getString("score"));score.setScore_credit(rs.getString("credit"));scores.add(score);}// 关闭资源rs.close();ps.close();return scores;}//返回学生成绩public static score returnScore(int stuID,int cID)throws Exception{score score=new score();String sql="select * from score where stuID=? and cID=? ";PreparedStatement ps=utilDao.getCon().prepareStatement(sql);//过滤其他的只留下ID为1的学生ps.setString(1,String.valueOf(stuID));ps.setString(2,String.valueOf(cID));ResultSet rs=ps.executeQuery();while (rs.next()){score.setScore_id(rs.getInt("stuID"));score.setScore_cid(rs.getInt("cID"));score.setSocre_score(rs.getString("score"));score.setScore_credit(rs.getString("credit"));}rs.close();ps.close();return score;}//增加成绩public static boolean addScore(int stuID,int cID,String score,String credit){String sql="insert into score(stuID,cID,score,credit)values (?,?,?,?) ";return utilDao.exeUpdate(sql,stuID,cID,score,credit);}//修改成绩public static boolean updateScore(int stuID,int cID,String score,String credit){String sql="update score set stuID=?,cID=?,score=?,credit=? ";return utilDao.exeUpdate(sql,stuID,cID,score,credit);}//删除成绩public static boolean deleteScore(int stuID,int cID){String sql="delete from score where stuID=? and cID=?";return utilDao.exeUpdate(sql,stuID,cID);}
judgeDao:
//找出最大学生idpublic static int maxID() throws Exception {String sql = "select max(stuID) as maxId from student";PreparedStatement ps = utilDao.getCon().prepareStatement(sql);ResultSet rs = ps.executeQuery();// 默认值或者根据需求设定初始值int maxId = 0;//如果存在if (rs.next()) {maxId = rs.getInt("maxId"); // 从结果集中获取名为 maxId 的列的值}return maxId;}//找出最大课程idpublic static int maxcID()throws Exception{String sql = "select max(cID) as maxId from course";PreparedStatement ps = utilDao.getCon().prepareStatement(sql);ResultSet rs = ps.executeQuery();int maxId = 0; // 默认值或者根据需求设定初始值if (rs.next()) {maxId = rs.getInt("maxId"); // 从结果集中获取名为 maxId 的列的值}return maxId;}//判断是否为管理员public static boolean magLogin(String name,String password)throws Exception{String sql="select * from manager where name=? and password=?";PreparedStatement ps=utilDao.getCon().prepareStatement(sql);ps.setString(1,name);ps.setString(2,password);ResultSet rs=ps.executeQuery();if (rs.next()) {utilDao.close(rs,ps,utilDao.getCon());return true;} else {utilDao.close(rs,ps,utilDao.getCon());return false;}}
domain(对象类):
student:
public class student {private int stu_id;private String stu_class;private String stu_name;private String stu_gender;private String stu_birth;private String stu_major;private String stu_password;public student() {}public int getStu_id() {return stu_id;}public void setStu_id(int stu_id) {this.stu_id = stu_id;}public String getStu_class() {return stu_class;}public void setStu_class(String stu_class) {this.stu_class = stu_class;}public String getStu_name() {return stu_name;}public void setStu_name(String stu_name) {this.stu_name = stu_name;}public String getStu_gender() {return stu_gender;}public void setStu_gender(String stu_gender) {this.stu_gender = stu_gender;}public String getStu_birth() {return stu_birth;}public void setStu_birth(String stu_birth) {this.stu_birth = stu_birth;}public String getStu_major() {return stu_major;}public void setStu_major(String stu_major) {this.stu_major = stu_major;}public String getStu_password() {return stu_password;}public void setStu_password(String stu_password) {this.stu_password = stu_password;}
course:
public class course {private int cou_id;private String cou_major;private String cou_name;private String cou_type;private String cou_beginTime;private int cou_studyTime;private int cou_score;public course() {}public int getCou_id() {return cou_id;}public void setCou_id(int cou_id) {this.cou_id = cou_id;}public String getCou_major() {return cou_major;}public void setCou_major(String cou_major) {this.cou_major = cou_major;}public String getCou_name() {return cou_name;}public void setCou_name(String cou_name) {this.cou_name = cou_name;}public String getCou_type() {return cou_type;}public void setCou_type(String cou_type) {this.cou_type = cou_type;}public String getCou_beginTime() {return cou_beginTime;}public void setCou_beginTime(String cou_beginTime) {this.cou_beginTime = cou_beginTime;}public int getCou_studyTime() {return cou_studyTime;}public void setCou_studyTime(int cou_studyTime) {this.cou_studyTime = cou_studyTime;}public void setCou_score(int cou_score) {this.cou_score = cou_score;}public int getCou_score() {return cou_score;}
score:
private int score_id;private int score_cid;private String socre_score;private String score_credit;public score() {}public int getScore_id() {return score_id;}public String getSocre_score() {return socre_score;}public void setSocre_score(String socre_score) {this.socre_score = socre_score;}public String getScore_credit() {return score_credit;}public void setScore_credit(String score_credit) {this.score_credit = score_credit;}public void setScore_id(int score_id) {this.score_id = score_id;}public int getScore_cid() {return score_cid;}
结尾
我的代码到这里就分享完了,感谢大家的观看看到这里,由于我的代码结构比较多,这里关于上面的每一个类都是完整的,除了studentDao分开写了,我这里在重新展示一下我的studentDao代码,其他的都一样就不展示了
studentDao(完整):
//学生登录判断public static boolean login(String idStr, String passwordStr) throws Exception {String sql = "select * from student where stuID =? and stuPassword=?";PreparedStatement ps = utilDao.getCon().prepareStatement(sql);ps.setString(1,idStr);ps.setString(2, passwordStr);ResultSet rs = ps.executeQuery();if (rs.next()) {utilDao.close(rs,ps, utilDao.getCon());return true;} else {utilDao.close(rs,ps,utilDao.getCon());return false;}}//学生查询自己成绩public static Integer gradeOneself(int stu_id) throws Exception {String sql = "SELECT SUM(credit) AS gradesum FROM score WHERE stuID=?";try (Connection connection =utilDao.getCon();PreparedStatement ps = connection.prepareStatement(sql)) {ps.setInt(1, stu_id);try (ResultSet rs = ps.executeQuery()) {if (rs.next()) {int gradesum = rs.getInt("gradesum");return gradesum;} else {// 如果没有查询到结果,可以返回 null 或者其他合适的值return null;}}}}//返回学生信息public static student returnStudent(int stu_id)throws Exception{student student=new student();String sql="select stuID,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword from student where stuID=?";PreparedStatement ps=utilDao.getCon().prepareStatement(sql);//过滤其他的只留下ID为1的学生ps.setString(1,String.valueOf(stu_id));ResultSet rs=ps.executeQuery();if(rs.next()){student.setStu_id(rs.getInt("stuID"));student.setStu_class(rs.getString("stuClass"));student.setStu_name(rs.getString("stuName"));student.setStu_gender(rs.getString("stuSex"));student.setStu_birth(rs.getString("stuBirth"));student.setStu_major(rs.getString("stuMajor"));student.setStu_password(rs.getString("stuPassword"));}rs.close();ps.close();return student;}//判断是否存在该学生public static boolean isStudent(int stu_id) throws SQLException {String sql="select stuID,stuClass,stuName,stuSex,stuBirth,stuMajor from student where stuID=?";PreparedStatement ps=utilDao.getCon().prepareStatement(sql);//过滤其他的只留下ID为1的学生ps.setString(1,String.valueOf(stu_id));ResultSet rs=ps.executeQuery();if(rs.next()){return true;}else {return false;}}// 学生类信息初始化public static ArrayList<student> initStudent() throws Exception {ArrayList<student> students = new ArrayList<>();String sql = "select * from student order by stuID asc";PreparedStatement ps = utilDao.getCon().prepareStatement(sql);ResultSet rs = ps.executeQuery();// 使用 while 循环遍历 ResultSet 中的所有行while (rs.next()) {student student = new student();student.setStu_id(rs.getInt("stuID"));student.setStu_class(rs.getString("stuClass"));student.setStu_name(rs.getString("stuName"));student.setStu_gender(rs.getString("stuSex"));student.setStu_birth(rs.getString("stuBirth"));student.setStu_major(rs.getString("stuMajor"));students.add(student);}// 关闭资源rs.close();ps.close();return students;}//添加学生public static boolean addStudent(int stuId,String stuClass,String stuName, String stuSex, String stuBirth, String stuMajor,String stuPassword)throws Exception{String sql="insert into student(stuID,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword )values (?,?,?,?,?,?,?) ";return utilDao.exeUpdate(sql,stuId,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword);}//修改学生public static boolean updateStudent(int stuId,String stuClass,String stuName, String stuSex, String stuBirth, String stuMajor,String stuPassword)throws Exception{String sql="update student set stuID=?,stuClass=?,stuName=?,stuSex=?,stuBirth=?,stuMajor=?,stuPassword=?";return utilDao.exeUpdate(sql,stuId,stuClass,stuName,stuSex,stuBirth,stuMajor,stuPassword);}//删除学生public static boolean deleteStudent(int stuId){String sql="delete from student where stuID=?";return utilDao.exeUpdate(sql,stuId);}
感谢大家的观看,如果有不懂的地方可以给我留言哦,冲! ! !