目录
一、er图
二、主页面
/**
* 首页面
*/
public void begin() {
GridPane gridPane = new GridPane();
stage.setTitle("学生管理系统首页");
//设置gridPane 的一些属性
Utils.setGridPane(gridPane);
Button manageLog = new Button("管理员登录");
Button teacherLog = new Button("老师登录");
Button studentLog = new Button("学生登录");
Button studentSign = new Button("学生注册");
//点击管理员登录按钮
manageLog.setOnAction(actionEvent -> {
managerLog();
});
//点击老师登录按钮
teacherLog.setOnAction(actionEvent -> {
teacher_log();
});
//点击学生登录按钮
studentLog.setOnAction(actionEvent -> {
student_log();
});
//点击学生注册按钮
studentSign.setOnAction(actionEvent -> {
student_sign();
});
VBox vBox = Utils.getVBox("-fx-font-size: 18;", 20, Pos.CENTER, manageLog, teacherLog, studentLog, studentSign);
Scene scene = new Scene(vBox, 500, 400);
stage.setScene(scene);
}
三、学生操作
1.学生注册
/**
* 学生注册
*/
public void student_sign() {
stage.setTitle("学生注册");
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);//设置GridPane的一些属性
Label studentID = new Label("学生学号");
TextField studentIDTxt = new TextField();
Label pwd = new Label("学生密码");
TextField pwdTxt = new TextField();
Label name = new Label("学生姓名");
TextField nameTxt = new TextField();
Label sex = new Label("性别");
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label grade = new Label("学生年级");
TextField gradeTxt = new TextField();
Label classSchool = new Label("学生班级");
TextField classSchoolTxt = new TextField();
Label classTeacher = new Label("班主任");
TextField classTeacherTxt = new TextField();
Button sign = new Button("注册");
Button back = new Button("返回");
//点击注册按钮
sign.setOnAction(actionEvent -> {
//数据要符合规范,grade,classSchool成绩必须是数字的形式,并且1<=grade<=4,0<=成绩<=100,ID必须没有重复
//数据是否为空
boolean isEmpty = !studentIDTxt.getText().equals("") && !pwdTxt.getText().equals("") && !nameTxt.getText().equals("")
&& (men.isSelected() || women.isSelected()) && !classTeacherTxt.getText().equals("");
//部分数据是否是数字
boolean isNum = Utils.isStrToNum(gradeTxt.getText()) && Utils.isStrToNum(classSchoolTxt.getText());
//部分数据的范围是否合适
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(gradeTxt.getText()), 1, 4);
try {
//要注册的学生ID不能存在,学生修改的班主任必须存在
if (isEmpty && isNum && isSuitable && !JDBCOperate.judge_IDRepeat(studentIDTxt.getText(), true) && JDBCOperate.teacher_isExit(classTeacherTxt.getText())) {
String sexDemo = men.isSelected() ? "男" : "女";
try {
JDBCOperate.student_signOrAdd(studentIDTxt.getText(), pwdTxt.getText(), nameTxt.getText(), sexDemo,
Integer.parseInt(gradeTxt.getText()), Integer.parseInt(classSchoolTxt.getText()), classTeacherTxt.getText());
tips("注册成功");
begin();//注册成功后返回到主页面
} catch (Exception e) {
throw new RuntimeException(e);
}
} else if (!JDBCOperate.teacher_isExit(classTeacherTxt.getText())) {
tips("请检查该班主任是否存在");
} else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击取消按钮,返回首页面
back.setOnAction(actionEvent -> {
begin();
});
//将男,女单选添加到单行面板中
HBox hBox = new HBox(men, women);
gridPane.add(studentID, 0, 0);
gridPane.add(studentIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(sex, 0, 3);
gridPane.add(hBox, 1, 3);
gridPane.add(grade, 0, 4);
gridPane.add(gradeTxt, 1, 4);
gridPane.add(classSchool, 0, 5);
gridPane.add(classSchoolTxt, 1, 5);
gridPane.add(classTeacher, 0, 6);
gridPane.add(classTeacherTxt, 1, 6);
gridPane.add(sign, 0, 8);
gridPane.add(back, 1, 8);
//设置性别只能选择一个
//将单选按钮添加到到ToggleGroup对象,它将管理它们,使得一次只能选择一个单选按钮。
ToggleGroup toggleGroup = new ToggleGroup();
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 500, 400);
stage.setScene(scene);
}
2.学生登录
/**
* 学生登录页面
*/
public void student_log() {
stage.setTitle("学生登录");
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label studentIDLabel = new Label("学生学号");
TextField studentIDTxt = new TextField();
Label pwdLabel = new Label("学生密码");
TextField pwdTxt = new TextField();
Button log = new Button("登录");
Button back = new Button("返回");
//点击登录
log.setOnAction(actionEvent -> {
try {
if (JDBCOperate.studentOrTeacher_log(studentIDTxt.getText(), pwdTxt.getText(), true)) {
//进入学生选项
student_select(studentIDTxt.getText(), pwdTxt.getText());
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击取消
back.setOnAction(actionEvent -> {
begin();
});
//安排每个组件的位置
gridPane.add(studentIDLabel, 0, 0);
gridPane.add(studentIDTxt, 1, 0);
gridPane.add(pwdLabel, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(log, 0, 3);
gridPane.add(back, 1, 3);
Scene scene = new Scene(gridPane, 500, 400);
stage.setScene(scene);
}
3.学生查看分数
/**
* 学生查看分数页面
* @param ID : 传入学生ID
* @param pwd : 点击取消时,将ID和p 传入到student_select方法中
*/
public void student_checkScore(String ID, String pwd) {
stage.setTitle("学生查询成绩");
//定义Student对象
Student student = new Student();
//通过SQL语句为student对象赋值,获得学生的信息
try {
JDBCOperate.student_checkScore(student, ID, pwd);
} catch (Exception e) {
throw new RuntimeException(e);
}
Label studentID = new Label("学号 : " + ID);
Label p = new Label("密码 : " + pwd);
Label name = new Label("姓名 : " + student.getName());
Label sex = new Label("性别 : " + student.sex);
Label grade = new Label("年级 : " + student.grade);
Label classSchool = new Label("班级 : " + student.classSchool);
Label chinese = new Label("语文 : " + student.chinese);
Label math = new Label("数学 : " + student.math);
Label english = new Label("英语 : " + student.english);
Label chemistry = new Label("化学 : " + student.chemistry);
Label political = new Label("政治 : " + student.political);
Label history = new Label("历史 : " + student.history);
Label totalScore = new Label("总分 : " + student.totalScore);
Label classTeacher = new Label("班主任 : " + student.classTeacher);
Button back = new Button("返回");
//点击返回按钮,返回首页面
back.setOnAction(actionEvent -> {
student_select(ID, pwd);
});
//单列面板
VBox vBox = Utils.getVBox("-fx-font-size: 18;", 10, Pos.CENTER,studentID, p, name, sex, grade, classSchool, chinese, math,
english, chemistry, political, history, totalScore, classTeacher, back);
Scene scene = new Scene(vBox, 300, 530);
stage.setScene(scene);
}
4.学生修改自己的信息
/**
* 学生修改自己的信息
* @param ID : 通过ID来搜索到我们要修改的学生
* @param p : 点击取消时,将ID和p 传入到student_select方法中
*/
public void student_modify(String ID, String p) {
stage.setTitle("学生修改信息");
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label pwd = new Label("学生密码");
TextField pwdTxt = new TextField();
Label name = new Label("学生姓名");
TextField nameTxt = new TextField();
Label sex = new Label("性别");
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label grade = new Label("学生年级");
TextField gradeTxt = new TextField();
Label classSchool = new Label("学生班级");
TextField classSchoolTxt = new TextField();
Label classTeacher = new Label("班主任");
TextField classTeacherTxt = new TextField();
Button modify = new Button("修改");
Button back = new Button("返回");
//点击修改
modify.setOnAction(actionEvent -> {
//数据要符合规范,grade,classSchool,成绩必须是数字的形式,并且1<=grade<=4,0<=成绩<=100,ID必须没有重复;班主任必须存在
boolean isEmpty = !pwdTxt.getText().equals("") && !nameTxt.getText().equals("") && (men.isSelected() ||
women.isSelected()) && !classTeacherTxt.getText().equals("");
boolean isNum = Utils.isStrToNum(gradeTxt.getText()) && Utils.isStrToNum(classSchoolTxt.getText());
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(gradeTxt.getText()), 1, 4);
try {
if (isEmpty && isNum && isSuitable && JDBCOperate.teacher_isExit(classTeacherTxt.getText())) {//学生修改的班主任必须存在
String sexDemo = men.isSelected() ? "男" : "女";
JDBCOperate.student_modify(ID, pwdTxt.getText(), nameTxt.getText(), sexDemo, Integer.parseInt(gradeTxt.getText()), Integer.parseInt(classSchoolTxt.getText()), classTeacherTxt.getText());
tips("修改成功");
student_select(ID, pwdTxt.getText());
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击取消按钮,返回首页面
back.setOnAction(actionEvent -> {
student_select(ID, p);
});
//将男,女单选添加到单行面板中
HBox hBox = new HBox(men, women);
gridPane.add(pwd, 0, 0);
gridPane.add(pwdTxt, 1, 0);
gridPane.add(name, 0, 1);
gridPane.add(nameTxt, 1, 1);
gridPane.add(sex, 0, 2);
gridPane.add(hBox, 1, 2);
gridPane.add(grade, 0, 3);
gridPane.add(gradeTxt, 1, 3);
gridPane.add(classSchool, 0, 4);
gridPane.add(classSchoolTxt, 1, 4);
gridPane.add(classTeacher, 0, 5);
gridPane.add(classTeacherTxt, 1, 5);
gridPane.add(modify, 0, 7);
gridPane.add(back, 1, 7);
//设置性别只能选择一个
ToggleGroup toggleGroup = new ToggleGroup();//将单选按钮添加到到ToggleGroup对象,它将管理它们,使得一次只能选择一个单选按钮。
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 500, 400);
stage.setScene(scene);
}
四、教师操作
1.教师登录
/**
* 教师登录
*/
public void teacher_log() {
stage.setTitle("教师登录");
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label teacherID = new Label("教师工号");
TextField teacherIDTxt = new TextField();
Label pwd = new Label("教师密码");
TextField pwdTxt = new TextField();
Button log = new Button("登录");
Button cancel = new Button("取消");
//点击登录按钮
log.setOnAction(actionEvent -> {
try {
if ( JDBCOperate.studentOrTeacher_log(teacherIDTxt.getText(), pwdTxt.getText(), false)) {
manageStudent(true);
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击取消按钮,返回首页面
cancel.setOnAction(actionEvent -> {
begin();
});
gridPane.add(teacherID, 0, 0);
gridPane.add(teacherIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(log, 0, 2);
gridPane.add(cancel, 1, 2);
Scene scene = new Scene(gridPane, 500, 400);
stage.setScene(scene);
}
2.教师选项
/**
* 教师,管理员管理学生
* @param select : true.老师管理学生;false.管理员管理学生(因为老师和管理员的返回界面不同,所以要分两种情况)
*/
public void manageStudent(boolean select) {
//学生集合
ArrayList<Student> students = new ArrayList<>();
stage.setTitle("管理学生");
BorderPane borderPane = new BorderPane();
Button add = new Button("增加");
Button delete = new Button("删除");
Button update = new Button("修改");
Button query = new Button("查询");
Button refresh = new Button("刷新");
Button back = new Button("返回");
//点击增加学生
add.setOnAction(actionEvent -> {
addStudent(borderPane, students);
});
//点击删除学生
delete.setOnAction(actionEvent -> {
//删除学生和管理员删除老师的界面相同
//删除学生,true:删除学生,false:删除老师
deletePerson(borderPane, students, null, true);
});
//点击修改学生
update.setOnAction(actionEvent -> {
//先输入ID号码判断是否存在该学生,如果存在就修改学生
//true.老师或管理员修改学生;false.管理员修改老师
inputID(borderPane, students, null, true);
});
//点击查询学生
query.setOnAction(actionEvent -> {
queryStudent(borderPane, students);
});
//点击刷新
refresh.setOnAction(actionEvent -> {
refresh_studentTable(borderPane, students);
});
//点击返回
back.setOnAction(actionEvent -> {
//教师退出该页面
if (select) {
begin();
}else {
//管理员退出该页面
managerSelect();
}
});
//单行面板
HBox hBox = Utils.getHBox("-fx-font-size: 18;", 40, Pos.CENTER, true, add, delete, update, query, refresh, back);
//设置borderPane的背景颜色
borderPane.setBackground(new Background(new BackgroundFill(Color.rgb(200, 200, 200),null,null)));//设置hBox的背景颜色
//设置返回按钮的外边距
hBox.setMargin(back, new Insets(20, 20, 20, 60));
//将hBox 单行面板 添加到边界布局的顶部
borderPane.setTop(hBox);
//从数据库中把学生数据全部填写到students集合中
try {
JDBCOperate.showAll_studentTable(students);
} catch (Exception e) {
throw new RuntimeException(e);
}
//展示学生表格
studentTable(borderPane, students);
stage.setScene(new Scene(borderPane,919, 500));
}
3.填写学生的表格:
/**
* 学生表格
* @param borderPane : 边界布局
* @param students : 学生集合
*/
public void studentTable(BorderPane borderPane, ArrayList<Student> students) {
//学生的信息表格
TableView<Student> tableView = new TableView<>();
//TableColumn类 创建列。然后使用TableView 类的 getColumns()方法将创建的列添加到表中。
TableColumn studentID = new TableColumn<>("学号");
TableColumn pwd = new TableColumn<>("密码");
TableColumn name = new TableColumn<>("姓名");
TableColumn sex = new TableColumn<>("性别");
TableColumn grade = new TableColumn<>("年级");
TableColumn classSchool = new TableColumn<>("班级");
TableColumn chinese = new TableColumn<>("语文");
TableColumn math = new TableColumn<>("数学");
TableColumn english = new TableColumn<>("英语");
TableColumn chemistry = new TableColumn<>("化学");
TableColumn political = new TableColumn<>("政治");
TableColumn history = new TableColumn<>("历史");
TableColumn totalScore = new TableColumn<>("总分");
TableColumn classTeacher = new TableColumn<>("班主任");
//表格列宽宽度设置
studentID.setMinWidth(70);
pwd.setMinWidth(70);
name.setMinWidth(60);
sex.setMinWidth(60);
grade.setMinWidth(60);
classSchool.setMinWidth(60);
chinese.setMinWidth(60);
math.setMinWidth(60);
english.setMinWidth(60);
chemistry.setMinWidth(60);
political.setMinWidth(60);
history.setMinWidth(60);
totalScore.setMinWidth(60);
classTeacher.setMinWidth(70);
//确定数据导入的列
studentID.setCellValueFactory(new PropertyValueFactory<>("studentID"));
pwd.setCellValueFactory(new PropertyValueFactory<>("pwd"));
name.setCellValueFactory(new PropertyValueFactory<>("name"));
sex.setCellValueFactory(new PropertyValueFactory<>("sex"));
grade.setCellValueFactory(new PropertyValueFactory<>("grade"));
classSchool.setCellValueFactory(new PropertyValueFactory<>("classSchool"));
chinese.setCellValueFactory(new PropertyValueFactory<>("chinese"));
math.setCellValueFactory(new PropertyValueFactory<>("math"));
english.setCellValueFactory(new PropertyValueFactory<>("english"));
chemistry.setCellValueFactory(new PropertyValueFactory<>("chemistry"));
political.setCellValueFactory(new PropertyValueFactory<>("political"));
history.setCellValueFactory(new PropertyValueFactory<>("history"));
totalScore.setCellValueFactory(new PropertyValueFactory<>("totalScore"));
classTeacher.setCellValueFactory(new PropertyValueFactory<>("classTeacher"));
//将列添加到表格中
tableView.getColumns().addAll(studentID, pwd, name, sex, grade, classSchool, chinese, math, english, chemistry, political, history, totalScore, classTeacher);
//将学生数组集合添加到表格中
tableView.getItems().addAll(students);
//将表格添加到边界布局的正中心
borderPane.setCenter(tableView);
}
4.增加学生
/**
* 教师,管理员增加学生
* @param borderPane : 边界布局
* @param students : 学生集合
*/
public void addStudent(BorderPane borderPane, ArrayList<Student> students) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("增加学生",tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label studentID = new Label("学生学号");
TextField studentIDTxt = new TextField();
Label pwd = new Label("学生密码");
TextField pwdTxt = new TextField();
Label name = new Label("学生姓名");
TextField nameTxt = new TextField();
Label sex = new Label("性别");
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label grade = new Label("学生年级");
TextField gradeTxt = new TextField();
Label classSchool = new Label("学生班级");
TextField classSchoolTxt = new TextField();
Label classTeacher = new Label("班主任");
TextField classTeacherTxt = new TextField();
Button add = new Button("增加");
Button back = new Button("返回");
//单行面板
HBox hBox = new HBox(men, women);
gridPane.add(studentID, 0, 0);
gridPane.add(studentIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(sex, 0, 3);
gridPane.add(hBox, 1, 3);
gridPane.add(grade, 0, 4);
gridPane.add(gradeTxt, 1, 4);
gridPane.add(classSchool, 0, 5);
gridPane.add(classSchoolTxt, 1, 5);
gridPane.add(classTeacher, 0, 6);
gridPane.add(classTeacherTxt, 1, 6);
gridPane.add(add, 0, 8);
gridPane.add(back, 1, 8);
//点击增加学生
add.setOnAction(actionEvent -> {
//数据要符合规范
boolean isEmpty = !studentIDTxt.getText().equals("") && !pwdTxt.getText().equals("") && !nameTxt.getText().equals("")
&& (men.isSelected() || women.isSelected()) && !classTeacherTxt.getText().equals("");
boolean isNum = Utils.isStrToNum(gradeTxt.getText()) && Utils.isStrToNum(classSchoolTxt.getText());
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(gradeTxt.getText()), 1, 4);
try {
//学生ID不能重复;学生修改的班主任必须存在
if (isEmpty && isNum && isSuitable && !JDBCOperate.judge_IDRepeat(studentIDTxt.getText(), true) && JDBCOperate.teacher_isExit(classTeacherTxt.getText())) {
String sexDemo = men.isSelected() ? "男" : "女";
JDBCOperate.add_student(studentIDTxt.getText(), pwdTxt.getText(), nameTxt.getText(), sexDemo,
Integer.parseInt(gradeTxt.getText()), Integer.parseInt(classSchoolTxt.getText()), classTeacherTxt.getText());
tips("增加成功");
tipStage.close();
//刷新表格中的数据
refresh_studentTable(borderPane, students);
}else if (!JDBCOperate.teacher_isExit(classTeacherTxt.getText())) {
tips("请检查该班主任是否存在");
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
//设置性别只能选择一个
ToggleGroup toggleGroup = new ToggleGroup();//将单选按钮添加到到ToggleGroup对象,它将管理它们,使得一次只能选择一个单选按钮。
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 340, 400);
tipStage.setScene(scene);
tipStage.show();
}
5.删除学生
删除学生和删除教师的代码基本相同
/**
* 删除学生,老师
* @param borderPane : 边界布局
* @param students : 学生集合
* @param teachers : 教师集合
* @param b : true删除学生,false删除老师
*/
public void deletePerson(BorderPane borderPane, ArrayList<Student> students, ArrayList<Teacher> teachers, boolean b) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("删除人员", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label ID = new Label("ID号码 : ");
TextField IDTxt = new TextField();
Button delete = new Button("删除");
Button back = new Button("返回");
gridPane.add(ID, 0, 0);
gridPane.add(IDTxt, 1, 0);
gridPane.add(delete, 0, 2);
gridPane.add(back, 1, 2);
//点击删除按钮
delete.setOnAction(actionEvent -> {
try {
//删除学生,先判断是否存在该学生
if (b && JDBCOperate.judge_IDRepeat(IDTxt.getText(), true)) {
JDBCOperate.delete_studentAndTeacher(IDTxt.getText(), true);
tips("成功删除该学生");
tipStage.close();
//刷新表格中的数据
refresh_studentTable(borderPane, students);
}else if (!b && JDBCOperate.judge_IDRepeat(IDTxt.getText(), true)) { //删除老师,先判断是否存在该老师
JDBCOperate.delete_studentAndTeacher(IDTxt.getText(), false);
tips("成功删除该老师");
tipStage.close();
//刷新表格中的数据
refresh_teacherTable(borderPane, teachers);
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
Scene scene = new Scene(gridPane, 360, 200);
tipStage.setScene(scene);
tipStage.show();
}
6.修改学生
/**
* 教师或管理员修改学生,管理员修改教师:必须先输入正确的ID号码后,才能修改
* @param borderPane : 边界布局
* @param students : 学生集合
* @param teachers : 教师集合
* @param select : true.老师或管理员修改学生;false.管理员修改老师
*/
public void inputID(BorderPane borderPane, ArrayList<Student> students, ArrayList<Teacher> teachers, boolean select) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("搜索ID号码", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label ID = new Label("ID号码 : ");
TextField IDTxt = new TextField();
Button search = new Button("搜索");
Button back = new Button("返回");
gridPane.add(ID, 0, 0);
gridPane.add(IDTxt, 1, 0);
gridPane.add(search, 0, 2);
gridPane.add(back, 1, 2);
//点击搜索按钮
search.setOnAction(actionEvent -> {
try {
//修改学生,先查询该学生是否存在
if (select && JDBCOperate.judge_IDRepeat(IDTxt.getText(), true)) {
updateStudent(borderPane, students, IDTxt.getText());
tipStage.close();
}else if(!select && JDBCOperate.judge_IDRepeat(IDTxt.getText(), false)) { //修改老师,先查询该老师是否存在
updateTeacher(borderPane, teachers, IDTxt.getText());
tipStage.close();
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
Scene scene = new Scene(gridPane, 360, 200);
tipStage.setScene(scene);
tipStage.show();
}
7.查询学生
可以使用ID查询、班级查询、或者同时进行ID查询和班级查询、以及姓名模糊查询:
/**
* 教师,管理员查询学生
* @param borderPane : 边界布局
* @param students : 学生集合
*/
public void queryStudent(BorderPane borderPane, ArrayList<Student> students) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("可以进行ID查询、班级查询、ID和班级共同查询、姓名模糊查询", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label studentID = new Label("学号 : ");
TextField studentIDTxt = new TextField();
Label classSchool = new Label("班级 : ");
TextField gradeTxt = new TextField();
Label name = new Label("姓名 : ");
TextField nameTxt = new TextField();
Button query = new Button("查询");
Button back = new Button("返回");
gridPane.add(studentID, 0, 0);
gridPane.add(studentIDTxt, 1, 0);
gridPane.add(classSchool, 0, 1);
gridPane.add(gradeTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(query, 0, 4);
gridPane.add(back, 1, 4);
//点击查询按钮
query.setOnAction(actionEvent -> {
//只通过ID查询学生
if (!studentIDTxt.getText().equals("") && gradeTxt.getText().equals("") && nameTxt.getText().equals("")){
//将ID符合的学生添加到students集合中
try {
JDBCOperate.show_studentTableByID(students, studentIDTxt.getText());//students 集合里面的数据以及改变了
} catch (Exception e) {
throw new RuntimeException(e);
}
studentTable(borderPane, students);
tipStage.close();
} else if (studentIDTxt.getText().equals("") && !gradeTxt.getText().equals("") && Utils.isStrToNum(gradeTxt.getText()) && nameTxt.getText().equals("")) {//只通过班级查询学生,grade必须可以转换为数字,不然从数据库中查找的时候会报错
//将grade符合的学生添加到students集合中
try {
JDBCOperate.show_studentTableByClassSchool(students, Integer.parseInt(gradeTxt.getText()) );
} catch (Exception e) {
throw new RuntimeException(e);
}
studentTable(borderPane, students);
tipStage.close();
}else if (!studentIDTxt.getText().equals("") && !gradeTxt.getText().equals("") && Utils.isStrToNum(gradeTxt.getText()) && nameTxt.getText().equals("")){//同时通过ID和班级查询,grade必须可以转换为数字
//将ID和grade同时符合的学生添加到students集合中
try {
JDBCOperate.show_studentTableByIDAndClassSchool(students, studentIDTxt.getText(), Integer.parseInt(gradeTxt.getText()));
} catch (Exception e) {
throw new RuntimeException(e);
}
studentTable(borderPane, students);
tipStage.close();
} else if (studentIDTxt.getText().equals("") && gradeTxt.getText().equals("") && !nameTxt.getText().equals("")) {//只通过姓名模糊查询
//只通过姓名模糊查询
try {
JDBCOperate.show_studentTableByName(students, nameTxt.getText());
} catch (Exception e) {
throw new RuntimeException(e);
}
studentTable(borderPane, students);
tipStage.close();
} else { //没有输入数据
tips("请检查数据");
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
Scene scene = new Scene(gridPane, 480, 400);
tipStage.setScene(scene);
tipStage.show();
}
8.刷新学生表格
/**
* 刷新学生表格(增加,删除,修改后调用)
* @param borderPane : 边界布局
* @param students : 学生集合
*/
public void refresh_studentTable(BorderPane borderPane, ArrayList<Student> students) {
//填写学生数据
try {
JDBCOperate.showAll_studentTable(students);
} catch (Exception e) {
throw new RuntimeException(e);
}
//展示学生表格
studentTable(borderPane, students);
}
五、管理员操作
1.管理员登录
/**
* 管理员登录
*/
public void managerLog() {
stage.setTitle("管理员登录");
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label managerID = new Label("管理员ID");
TextField managerIDTxt = new TextField();
Label pwd = new Label("管理员密码");
TextField pwdTxt = new TextField();
Button log = new Button("登录");
Button cancel = new Button("取消");
//点击登录按钮
log.setOnAction(actionEvent -> {
try {
if (JDBCOperate.manager_log(managerIDTxt.getText(), pwdTxt.getText())) {
managerSelect();
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击取消按钮,返回首页面
cancel.setOnAction(actionEvent -> {
begin();
});
gridPane.add(managerID, 0, 0);
gridPane.add(managerIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(log, 0, 2);
gridPane.add(cancel, 1, 2);
Scene scene = new Scene(gridPane, 500, 400);
stage.setScene(scene);
}
2.管理员登录后选项
/**
* 管理员登录后的 选择
*/
public void managerSelect() {
stage.setTitle("管理员选项");
Button manageStudent = new Button("管理学生");
Button manageTeacher = new Button("管理教师");
Button dataStatistical = new Button("成绩统计");
Button back = new Button("退出系统");
//点击管理学生按钮
manageStudent.setOnAction(actionEvent -> {
manageStudent(false);
});
//点击管理教师按钮
manageTeacher.setOnAction(actionEvent -> {
manageTeacher();
});
//点击成绩统计
dataStatistical.setOnAction(actionEvent -> {
dataStatistical();
});
//点击退出按钮
back.setOnAction(actionEvent -> {
begin();
});
//单行面板
VBox vBox = Utils.getVBox("-fx-font-size: 18;", 40, Pos.CENTER, manageStudent, manageTeacher, dataStatistical, back);
stage.setScene(new Scene(vBox, 500, 400));
}
3.管理员管理学生
在文章上面(管理员管理学生和教师管理学生代码相同)
4.管理员管理教师
管理员管理教师和管理员管理学生代码大致相同
/**
* 管理员管理老师
*/
public void manageTeacher() {
//教师集合
ArrayList<Teacher> teachers = new ArrayList<>();
stage.setTitle("管理员管理老师");
BorderPane borderPane = new BorderPane();
Button add = new Button("增加");
Button delete = new Button("删除");
Button update = new Button("修改");
Button query = new Button("查询");
Button refresh = new Button("刷新");
Button back = new Button("返回");
//点击增加教师按钮
add.setOnAction(actionEvent -> {
addTeacher(borderPane, teachers);
});
//点击删除教师按钮
delete.setOnAction(actionEvent -> {
//删除学生和老师的界面相同
//删除老师
deletePerson(borderPane, null, teachers, false);
});
//点击修改教师按钮
update.setOnAction(actionEvent -> {
inputID(borderPane, null, teachers, false);
});
//点击查询教师按钮
query.setOnAction(actionEvent -> {
queryTeacher(borderPane);
});
//点击刷新
refresh.setOnAction(actionEvent -> {
refresh_teacherTable(borderPane, teachers);
});
//点击返回按钮
back.setOnAction(actionEvent -> {
managerSelect();
});
//单行面板
HBox hBox = Utils.getHBox("-fx-font-size: 18;", 40, Pos.CENTER, true, add, delete, update, query, refresh, back);
//设置borderPane的背景颜色
borderPane.setBackground(new Background(new BackgroundFill(Color.rgb(200, 200, 200),null,null)));//设置hBox的背景颜色
//返回按钮的外边距
hBox.setMargin(back, new Insets(20, 20, 20, 60));
//将hBox 单行面板 添加到边界布局的顶部
borderPane.setTop(hBox);
//从数据库中把老师数据全部填写到teachers
try {
JDBCOperate.showAll_teacherTable(teachers);
} catch (Exception e) {
throw new RuntimeException(e);
}
//展示教师表格
teacherTable(borderPane, teachers);
stage.setScene(new Scene(borderPane,738, 500));
}
/**
* 教师表格
* @param borderPane : 边界布局
* @param teachers : 教师集合
*/
public void teacherTable(BorderPane borderPane, ArrayList<Teacher> teachers) {
//教师的信息表格
TableView<Teacher> tableView = new TableView<>();
//TableColumn类 创建列。然后使用TableView 类的 getColumns()方法将创建的列添加到表中。
TableColumn teacherID = new TableColumn<>("工号");
TableColumn pwd = new TableColumn<>("密码");
TableColumn name = new TableColumn<>("姓名");
TableColumn sex = new TableColumn<>("性别");
TableColumn age = new TableColumn<>("年龄");
TableColumn classSchool = new TableColumn<>("班级");
TableColumn school = new TableColumn<>("学校");
TableColumn salary = new TableColumn<>("工资");
TableColumn telephone = new TableColumn<>("电话号码");
//表格列宽宽度设置
teacherID.setMinWidth(90);
name.setMinWidth(70);
sex.setMinWidth(60);
pwd.setMinWidth(85);
age.setMinWidth(70);
classSchool.setMinWidth(70);
school.setMinWidth(120);
salary.setMinWidth(70);
telephone.setMinWidth(100);
//确定数据导入的列
//引号里面的内容一定要和teacher对象的变量名称对应,Teacher还要有相关变量的set,get方法
teacherID.setCellValueFactory(new PropertyValueFactory<>("teacherID"));
pwd.setCellValueFactory(new PropertyValueFactory<>("pwd"));
name.setCellValueFactory(new PropertyValueFactory<>("name"));
sex.setCellValueFactory(new PropertyValueFactory<>("sex"));
age.setCellValueFactory(new PropertyValueFactory<>("age"));
classSchool.setCellValueFactory(new PropertyValueFactory<>("classSchool"));
school.setCellValueFactory(new PropertyValueFactory<>("school"));
salary.setCellValueFactory(new PropertyValueFactory<>("salary"));
telephone.setCellValueFactory(new PropertyValueFactory<>("telephone"));
tableView.getColumns().addAll(teacherID, pwd, name, sex, age, classSchool, school, salary, telephone);
//将教师数组集合添加到表格中
tableView.getItems().addAll(teachers);
//将表格添加到边界布局的正中心
borderPane.setCenter(tableView);
}
/**
* 管理员增加教师
* @param borderPane :边界布局
* @param teachers :教师集合
*/
public void addTeacher(BorderPane borderPane, ArrayList<Teacher> teachers) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("增加教师", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label teacherID = new Label("工号 : ");
TextField teacherIDTxt = new TextField();
Label pwd = new Label("密码 : " );
TextField pwdTxt = new TextField();
Label name = new Label("姓名 : " );
TextField nameTxt = new TextField();
Label sex = new Label("性别 : " );
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label age = new Label("年龄 : " );
TextField ageTxt = new TextField();
Label classSchool = new Label("年级 : ");
TextField classSchoolTxt = new TextField();
Label school = new Label("学校 : " );
TextField schoolTxt = new TextField();
Label salary = new Label("工资 : " );
TextField salaryTxt = new TextField();
Label telephone = new Label("电话 : " );
TextField telephoneTxt = new TextField();
Button add = new Button("增加");
Button back = new Button("返回");
//单行面板
HBox hBox = new HBox(men, women);
gridPane.add(teacherID, 0, 0);
gridPane.add(teacherIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(sex, 0, 3);
gridPane.add(hBox, 1, 3);
gridPane.add(age, 0, 4);
gridPane.add(ageTxt, 1, 4);
gridPane.add(classSchool, 0, 5);
gridPane.add(classSchoolTxt, 1, 5);
gridPane.add(school, 0, 6);
gridPane.add(schoolTxt, 1, 6);
gridPane.add(salary, 0, 7);
gridPane.add(salaryTxt, 1, 7);
gridPane.add(telephone, 0, 8);
gridPane.add(telephoneTxt, 1, 8);
gridPane.add(add, 0, 10);
gridPane.add(back, 1, 10);
//点击增加按钮
add.setOnAction(actionEvent -> {
//检查是否为空
boolean isData = !teacherIDTxt.getText().equals("") && !pwdTxt.getText().equals("") && !nameTxt.getText().equals("") &&
(men.isSelected() || women.isSelected()) && !ageTxt.getText().equals("") && !classSchoolTxt.getText().equals("") &&
!schoolTxt.getText().equals("") && !salaryTxt.getText().equals("") && !telephoneTxt.getText().equals("");
//部分数据是否可以转换为数字
boolean isNum = Utils.isStrToNum(classSchoolTxt.getText()) && Utils.isStrToNum(ageTxt.getText()) && Utils.isStrToNum(salaryTxt.getText());
//数据是否在合适的范围内
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(ageTxt.getText()), 22, 100);
if (isData && isNum && isSuitable){
String sexDemo = men.isSelected() ? "男" : "女";
//增加
try {
JDBCOperate.add_teacher(teacherIDTxt.getText(), pwdTxt.getText(), nameTxt.getText(), sexDemo, Integer.parseInt(ageTxt.getText()),
Integer.parseInt(classSchoolTxt.getText()), schoolTxt.getText(), Integer.parseInt(salaryTxt.getText()), telephoneTxt.getText());
//刷新教师表格
refresh_teacherTable(borderPane, teachers);
tips("增加成功");
tipStage.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}else {
tips("请检查数据");
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
//设置性别只能选择一个
ToggleGroup toggleGroup = new ToggleGroup();//将单选按钮添加到到ToggleGroup对象,它将管理它们,使得一次只能选择一个单选按钮。
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 340, 640);
tipStage.setScene(scene);
tipStage.show();
}
/**
*
* @param ID
*/
/**
* 输入正确的ID号码后,管理员可以修改老师了
* @param borderPane :边界布局
* @param teachers :教师集合
* @param ID : 需要修改的教师的 ID号码,我们将根据原先的ID来找到该教师
*/
public void updateTeacher(BorderPane borderPane, ArrayList<Teacher> teachers, String ID) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("修改学生", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label teacherID = new Label("ID : ");
TextField teacherIDTxt = new TextField();
Label pwd = new Label("密码 : ");
TextField pwdTxt = new TextField();
Label name = new Label("姓名 : " );
TextField nameTxt = new TextField();
Label sex = new Label("性别 : " );
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label age = new Label("年龄 : " );
TextField ageTxt = new TextField();
Label classSchool = new Label("班级 : ");
TextField classSchoolTxt = new TextField();
Label school = new Label("学校 : " );
TextField schoolTxt = new TextField();
Label salary = new Label("工资 : " );
TextField salaryTxt = new TextField();
Label telephone = new Label("电话 : " );
TextField telephoneTxt = new TextField();
Button update = new Button("修改");
Button back = new Button("返回");
//单行面板
HBox hBox = new HBox(men, women);
gridPane.add(teacherID, 0, 0);
gridPane.add(teacherIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(sex, 0, 3);
gridPane.add(hBox, 1, 3);
gridPane.add(age, 0, 4);
gridPane.add(ageTxt, 1, 4);
gridPane.add(classSchool, 0, 5);
gridPane.add(classSchoolTxt, 1, 5);
gridPane.add(school, 0, 6);
gridPane.add(schoolTxt, 1, 6);
gridPane.add(salary, 0, 7);
gridPane.add(salaryTxt, 1, 7);
gridPane.add(telephone, 0, 8);
gridPane.add(telephoneTxt, 1, 8);
gridPane.add(update, 0, 10);
gridPane.add(back, 1, 10);
//点击修改按钮
update.setOnAction(actionEvent -> {
//检查数据是否为空
boolean isData = !teacherIDTxt.getText().equals("") && !pwdTxt.getText().equals("") && !nameTxt.getText().equals("") &&
(men.isSelected() || women.isSelected())&& !ageTxt.getText().equals("") && !classSchoolTxt.getText().equals("") &&
!schoolTxt.getText().equals("") && !salaryTxt.getText().equals("") && !telephoneTxt.getText().equals("");
//部分数据是否可以转换为数字
boolean isNum = Utils.isStrToNum(classSchoolTxt.getText()) && Utils.isStrToNum(ageTxt.getText()) && Utils.isStrToNum(salaryTxt.getText());
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(ageTxt.getText()), 22, 100);
if (isData && isNum && isSuitable){
String sexDemo = men.isSelected() ? "男" : "女";
//修改教师信息
try {
JDBCOperate.update_teacher(ID, teacherIDTxt.getText(), pwdTxt.getText(), nameTxt.getText(), sexDemo, Integer.parseInt(ageTxt.getText()), Integer.parseInt(classSchoolTxt.getText()), schoolTxt.getText(), Integer.parseInt(salaryTxt.getText()), telephoneTxt.getText());
tips("修改成功");
tipStage.close();
//刷新教师表格
refresh_teacherTable(borderPane, teachers);
} catch (Exception e) {
throw new RuntimeException(e);
}
}else {
tips("请检查数据");
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
//设置性别只能选择一个
ToggleGroup toggleGroup = new ToggleGroup();//将单选按钮添加到到ToggleGroup对象,它将管理它们,使得一次只能选择一个单选按钮。
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 440, 680);
tipStage.setScene(scene);
tipStage.show();
}
/**
* 管理员查询教师
* @param borderPane : 边界布局
*/
public void queryTeacher(BorderPane borderPane) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("可以进行ID查询、班级查询、ID和班级共同查询、姓名模糊查询", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label teacherID = new Label("工号 : ");
TextField teacherIDTxt = new TextField();
Label classSchool = new Label("班级 : ");
TextField classSchoolTxt = new TextField();
Label name = new Label("姓名");
TextField nameTxt = new TextField();
Button query = new Button("查询");
Button back = new Button("返回");
gridPane.add(teacherID, 0, 0);
gridPane.add(teacherIDTxt, 1, 0);
gridPane.add(classSchool, 0, 1);
gridPane.add(classSchoolTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(query, 0, 4);
gridPane.add(back, 1, 4);
//点击查询按钮
query.setOnAction(actionEvent -> {
ArrayList<Teacher> teachers = new ArrayList<>();
//只查询ID
if (!teacherIDTxt.getText().equals("") && classSchoolTxt.getText().equals("") && nameTxt.getText().equals("")){
//将ID符合的教师添加到teachers集合中
try {
JDBCOperate.show_teacherTableByID(teachers, teacherIDTxt.getText());
} catch (Exception e) {
throw new RuntimeException(e);
}
teacherTable(borderPane,teachers);
tipStage.close();
} else if (teacherIDTxt.getText().equals("") && !classSchoolTxt.getText().equals("") && Utils.isStrToNum(classSchoolTxt.getText()) && nameTxt.getText().equals("")) { //只查询班级,而且grade必须可以转换为数字
//将classSchool符合的教师添加到teachers集合中
try {
JDBCOperate.show_teacherTableByClassSchool(teachers, Integer.parseInt(classSchoolTxt.getText()));
} catch (Exception e) {
throw new RuntimeException(e);
}
teacherTable(borderPane, teachers);
tipStage.close();
}else if (!teacherIDTxt.getText().equals("") && !classSchoolTxt.getText().equals("") && Utils.isStrToNum(classSchoolTxt.getText()) && nameTxt.getText().equals("")){ //同时查询ID和班级,而且grade必须可以转换为数字
//将ID和classSchool同时符合的教师添加到teachers集合中
try {
JDBCOperate.show_teacherTableByIDAndClassSchool(teachers, teacherIDTxt.getText(), Integer.parseInt(classSchoolTxt.getText()));
} catch (Exception e) {
throw new RuntimeException(e);
}
teacherTable(borderPane, teachers);
tipStage.close();
} else if (teacherIDTxt.getText().equals("") && classSchoolTxt.getText().equals("") && !nameTxt.getText().equals("")) {
//只通过姓名模糊查询
try {
JDBCOperate.show_teacherTableByName(teachers, nameTxt.getText());
} catch (Exception e) {
throw new RuntimeException(e);
}
teacherTable(borderPane,teachers);
tipStage.close();
} else { //没有输入数据
tips("请检查数据");
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
Scene scene = new Scene(gridPane, 340, 400);
tipStage.setScene(scene);
tipStage.show();
}
5.学生成绩柱状图
/**
* 成绩柱状图
*/
public void dataStatistical() {
stage.setTitle("成绩统计");
Button back = new Button("返回");
//点击返回
back.setOnAction(actionEvent -> {
managerSelect();
});
CategoryAxis xAxis = new CategoryAxis();
xAxis.setLabel("成绩分布");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("人数");
BarChart barChart = new BarChart<>(xAxis, yAxis);
XYChart.Series dataSeries1 = new XYChart.Series();
dataSeries1.setName("年级1");
XYChart.Series dataSeries2 = null;
XYChart.Series dataSeries3 = null;
XYChart.Series dataSeries4 = null;
try {
dataSeries1.getData().add(new XYChart.Data("0<=x<200", JDBCOperate.histogram(1, 0, 200)));
dataSeries1.getData().add(new XYChart.Data("200<=x<350" , JDBCOperate.histogram(1, 200, 350)));
dataSeries1.getData().add(new XYChart.Data("350<=x<500" , JDBCOperate.histogram(1, 350, 500)));
dataSeries1.getData().add(new XYChart.Data("500<=x<=600" , JDBCOperate.histogram(1, 500, 601)));
dataSeries2 = new XYChart.Series();
dataSeries2.setName("年级2");
dataSeries2.getData().add(new XYChart.Data("0<=x<200", JDBCOperate.histogram(2, 0, 200)));
dataSeries2.getData().add(new XYChart.Data("200<=x<350" , JDBCOperate.histogram(2, 200, 350)));
dataSeries2.getData().add(new XYChart.Data("350<=x<500" , JDBCOperate.histogram(2, 350, 500)));
dataSeries2.getData().add(new XYChart.Data("500<=x<=600" , JDBCOperate.histogram(2, 500, 601)));
dataSeries3 = new XYChart.Series();
dataSeries3.setName("年级3");
dataSeries3.getData().add(new XYChart.Data("0<=x<200", JDBCOperate.histogram(3, 0, 200)));
dataSeries3.getData().add(new XYChart.Data("200<=x<350" , JDBCOperate.histogram(3, 200, 350)));
dataSeries3.getData().add(new XYChart.Data("350<=x<500" , JDBCOperate.histogram(3, 350, 500)));
dataSeries3.getData().add(new XYChart.Data("500<=x<=600" , JDBCOperate.histogram(3, 500, 601)));
dataSeries4 = new XYChart.Series();
dataSeries4.setName("年级4");
dataSeries4.getData().add(new XYChart.Data("0<=x<200", JDBCOperate.histogram(4, 0, 200)));
dataSeries4.getData().add(new XYChart.Data("200<=x<350" , JDBCOperate.histogram(4, 200, 350)));
dataSeries4.getData().add(new XYChart.Data("350<=x<500" , JDBCOperate.histogram(4, 350, 500)));
dataSeries4.getData().add(new XYChart.Data("500<=x<=600" , JDBCOperate.histogram(4, 500, 601)));
} catch (SQLException e) {
throw new RuntimeException(e);
}
barChart.getData().add(dataSeries1);
barChart.getData().add(dataSeries2);
barChart.getData().add(dataSeries3);
barChart.getData().add(dataSeries4);
//单列面板
VBox vBox = new VBox(barChart, back);
vBox.setAlignment(Pos.CENTER);
vBox.setMargin(back, new Insets(20, 0, 0, 0));
Scene scene = new Scene(vBox, 500, 540);
stage.setScene(scene);
}
六、ID、密码填写错误
/**
* 设置顶层窗口的一些属性
* @param str : 标题的内容
* @param tipStage : 需要添加属性的窗口
*/
public void setTopTip(String str, Stage tipStage) {
tipStage.setResizable(false);//不能调整大小
tipStage.setTitle(str);//设置标题
//只能操作当前的提示框
tipStage.initOwner(stage);
tipStage.initModality(Modality.WINDOW_MODAL);
//设置该提示框永远在最上层
tipStage.setAlwaysOnTop(true);
}
/**
* 提示框
* @param content : 提示的信息
*/
void tips(String content) {
//创建提示框窗口对象
Stage tipStage = new Stage();
//调用顶层窗口函数设置一些必要的属性
setTopTip(content, tipStage);
Label label = new Label(content);
Button back = new Button("返回");
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
//单列面板
VBox vBox = new VBox(label, back);
vBox.setSpacing(10);
vBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vBox, 150, 130);
tipStage.setScene(scene);
tipStage.show();
}
七、全部代码展示
1.Main函数
package student_manage;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.sql.SQLException;
import java.util.ArrayList;
/**
* @projectName : student_manageSystem
* @className : Main
* @description : 系统与用户交互界面
* @author : lizhuang
* @createDate : 2023/9/27
*
*/
public class Main extends Application {
public Stage stage;
/**
* 主入口
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
this.stage = stage;
stage.setResizable(false);
begin();
stage.show();
}
/**
* 首页面
*/
public void begin() {
GridPane gridPane = new GridPane();
stage.setTitle("学生管理系统首页");
//设置gridPane 的一些属性
Utils.setGridPane(gridPane);
Button manageLog = new Button("管理员登录");
Button teacherLog = new Button("老师登录");
Button studentLog = new Button("学生登录");
Button studentSign = new Button("学生注册");
//点击管理员登录按钮
manageLog.setOnAction(actionEvent -> {
managerLog();
});
//点击老师登录按钮
teacherLog.setOnAction(actionEvent -> {
teacher_log();
});
//点击学生登录按钮
studentLog.setOnAction(actionEvent -> {
student_log();
});
//点击学生注册按钮
studentSign.setOnAction(actionEvent -> {
student_sign();
});
VBox vBox = Utils.getVBox("-fx-font-size: 18;", 20, Pos.CENTER, manageLog, teacherLog, studentLog, studentSign);
Scene scene = new Scene(vBox, 500, 400);
stage.setScene(scene);
}
/**
* 学生的一些操作
* ------------------------------------------------------------------------------------------------------------------------
*/
/**
* 学生注册
*/
public void student_sign() {
stage.setTitle("学生注册");
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);//设置GridPane的一些属性
Label studentID = new Label("学生学号");
TextField studentIDTxt = new TextField();
Label pwd = new Label("学生密码");
TextField pwdTxt = new TextField();
Label name = new Label("学生姓名");
TextField nameTxt = new TextField();
Label sex = new Label("性别");
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label grade = new Label("学生年级");
TextField gradeTxt = new TextField();
Label classSchool = new Label("学生班级");
TextField classSchoolTxt = new TextField();
Label classTeacher = new Label("班主任");
TextField classTeacherTxt = new TextField();
Button sign = new Button("注册");
Button back = new Button("返回");
//点击注册按钮
sign.setOnAction(actionEvent -> {
//数据要符合规范,grade,classSchool成绩必须是数字的形式,并且1<=grade<=4,0<=成绩<=100,ID必须没有重复
//数据是否为空
boolean isEmpty = !studentIDTxt.getText().equals("") && !pwdTxt.getText().equals("") && !nameTxt.getText().equals("")
&& (men.isSelected() || women.isSelected()) && !classTeacherTxt.getText().equals("");
//部分数据是否是数字
boolean isNum = Utils.isStrToNum(gradeTxt.getText()) && Utils.isStrToNum(classSchoolTxt.getText());
//部分数据的范围是否合适
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(gradeTxt.getText()), 1, 4);
try {
//要注册的学生ID不能存在,学生修改的班主任必须存在
if (isEmpty && isNum && isSuitable && !JDBCOperate.judge_IDRepeat(studentIDTxt.getText(), true) && JDBCOperate.teacher_isExit(classTeacherTxt.getText())) {
String sexDemo = men.isSelected() ? "男" : "女";
try {
JDBCOperate.student_signOrAdd(studentIDTxt.getText(), pwdTxt.getText(), nameTxt.getText(), sexDemo,
Integer.parseInt(gradeTxt.getText()), Integer.parseInt(classSchoolTxt.getText()), classTeacherTxt.getText());
tips("注册成功");
begin();//注册成功后返回到主页面
} catch (Exception e) {
throw new RuntimeException(e);
}
} else if (!JDBCOperate.teacher_isExit(classTeacherTxt.getText())) {
tips("请检查该班主任是否存在");
} else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击取消按钮,返回首页面
back.setOnAction(actionEvent -> {
begin();
});
//将男,女单选添加到单行面板中
HBox hBox = new HBox(men, women);
gridPane.add(studentID, 0, 0);
gridPane.add(studentIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(sex, 0, 3);
gridPane.add(hBox, 1, 3);
gridPane.add(grade, 0, 4);
gridPane.add(gradeTxt, 1, 4);
gridPane.add(classSchool, 0, 5);
gridPane.add(classSchoolTxt, 1, 5);
gridPane.add(classTeacher, 0, 6);
gridPane.add(classTeacherTxt, 1, 6);
gridPane.add(sign, 0, 8);
gridPane.add(back, 1, 8);
//设置性别只能选择一个
//将单选按钮添加到到ToggleGroup对象,它将管理它们,使得一次只能选择一个单选按钮。
ToggleGroup toggleGroup = new ToggleGroup();
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 500, 400);
stage.setScene(scene);
}
/**
* 学生登录页面
*/
public void student_log() {
stage.setTitle("学生登录");
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label studentIDLabel = new Label("学生学号");
TextField studentIDTxt = new TextField();
Label pwdLabel = new Label("学生密码");
TextField pwdTxt = new TextField();
Button log = new Button("登录");
Button back = new Button("返回");
//点击登录
log.setOnAction(actionEvent -> {
try {
if (JDBCOperate.studentOrTeacher_log(studentIDTxt.getText(), pwdTxt.getText(), true)) {
//进入学生选项
student_select(studentIDTxt.getText(), pwdTxt.getText());
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击取消
back.setOnAction(actionEvent -> {
begin();
});
//安排每个组件的位置
gridPane.add(studentIDLabel, 0, 0);
gridPane.add(studentIDTxt, 1, 0);
gridPane.add(pwdLabel, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(log, 0, 3);
gridPane.add(back, 1, 3);
Scene scene = new Scene(gridPane, 500, 400);
stage.setScene(scene);
}
/**
* 学生登录后选择选项
* @param ID : 传入ID用来查看学生分数 或 修改学生信息
* @param pwd : 传入ID用来查看学生分数 或 修改学生信息
*/
public void student_select(String ID, String pwd) {
stage.setTitle("学生选择");
Button checkScore = new Button("查看分数");
Button update = new Button("修改信息");
Button back = new Button("返回");
//点击查看分数
checkScore.setOnAction(actionEvent -> {
student_checkScore(ID, pwd);
});
//点击修改信息
update.setOnAction(actionEvent -> {
student_modify(ID, pwd);
});
//点击取消
back.setOnAction(actionEvent -> {
begin();
});
VBox vBox = Utils.getVBox("-fx-font-size: 18;", 20, Pos.CENTER, checkScore, update, back);
Scene scene = new Scene(vBox, 500, 400);
stage.setScene(scene);
}
/**
* 学生查看分数页面
* @param ID : 传入学生ID
* @param pwd : 点击取消时,将ID和p 传入到student_select方法中
*/
public void student_checkScore(String ID, String pwd) {
stage.setTitle("学生查询成绩");
//定义Student对象
Student student = new Student();
//通过SQL语句为student对象赋值,获得学生的信息
try {
JDBCOperate.student_checkScore(student, ID, pwd);
} catch (Exception e) {
throw new RuntimeException(e);
}
Label studentID = new Label("学号 : " + ID);
Label p = new Label("密码 : " + pwd);
Label name = new Label("姓名 : " + student.getName());
Label sex = new Label("性别 : " + student.sex);
Label grade = new Label("年级 : " + student.grade);
Label classSchool = new Label("班级 : " + student.classSchool);
Label chinese = new Label("语文 : " + student.chinese);
Label math = new Label("数学 : " + student.math);
Label english = new Label("英语 : " + student.english);
Label chemistry = new Label("化学 : " + student.chemistry);
Label political = new Label("政治 : " + student.political);
Label history = new Label("历史 : " + student.history);
Label totalScore = new Label("总分 : " + student.totalScore);
Label classTeacher = new Label("班主任 : " + student.classTeacher);
Button back = new Button("返回");
//点击返回按钮,返回首页面
back.setOnAction(actionEvent -> {
student_select(ID, pwd);
});
//单列面板
VBox vBox = Utils.getVBox("-fx-font-size: 18;", 10, Pos.CENTER,studentID, p, name, sex, grade, classSchool, chinese, math,
english, chemistry, political, history, totalScore, classTeacher, back);
Scene scene = new Scene(vBox, 300, 530);
stage.setScene(scene);
}
/**
* 学生修改自己的信息
* @param ID : 通过ID来搜索到我们要修改的学生
* @param p : 点击取消时,将ID和p 传入到student_select方法中
*/
public void student_modify(String ID, String p) {
stage.setTitle("学生修改信息");
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label pwd = new Label("学生密码");
TextField pwdTxt = new TextField();
Label name = new Label("学生姓名");
TextField nameTxt = new TextField();
Label sex = new Label("性别");
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label grade = new Label("学生年级");
TextField gradeTxt = new TextField();
Label classSchool = new Label("学生班级");
TextField classSchoolTxt = new TextField();
Label classTeacher = new Label("班主任");
TextField classTeacherTxt = new TextField();
Button modify = new Button("修改");
Button back = new Button("返回");
//点击修改
modify.setOnAction(actionEvent -> {
//数据要符合规范,grade,classSchool,成绩必须是数字的形式,并且1<=grade<=4,0<=成绩<=100,ID必须没有重复;班主任必须存在
boolean isEmpty = !pwdTxt.getText().equals("") && !nameTxt.getText().equals("") && (men.isSelected() ||
women.isSelected()) && !classTeacherTxt.getText().equals("");
boolean isNum = Utils.isStrToNum(gradeTxt.getText()) && Utils.isStrToNum(classSchoolTxt.getText());
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(gradeTxt.getText()), 1, 4);
try {
if (isEmpty && isNum && isSuitable && JDBCOperate.teacher_isExit(classTeacherTxt.getText())) {//学生修改的班主任必须存在
String sexDemo = men.isSelected() ? "男" : "女";
JDBCOperate.student_modify(ID, pwdTxt.getText(), nameTxt.getText(), sexDemo, Integer.parseInt(gradeTxt.getText()), Integer.parseInt(classSchoolTxt.getText()), classTeacherTxt.getText());
tips("修改成功");
student_select(ID, pwdTxt.getText());
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击取消按钮,返回首页面
back.setOnAction(actionEvent -> {
student_select(ID, p);
});
//将男,女单选添加到单行面板中
HBox hBox = new HBox(men, women);
gridPane.add(pwd, 0, 0);
gridPane.add(pwdTxt, 1, 0);
gridPane.add(name, 0, 1);
gridPane.add(nameTxt, 1, 1);
gridPane.add(sex, 0, 2);
gridPane.add(hBox, 1, 2);
gridPane.add(grade, 0, 3);
gridPane.add(gradeTxt, 1, 3);
gridPane.add(classSchool, 0, 4);
gridPane.add(classSchoolTxt, 1, 4);
gridPane.add(classTeacher, 0, 5);
gridPane.add(classTeacherTxt, 1, 5);
gridPane.add(modify, 0, 7);
gridPane.add(back, 1, 7);
//设置性别只能选择一个
ToggleGroup toggleGroup = new ToggleGroup();//将单选按钮添加到到ToggleGroup对象,它将管理它们,使得一次只能选择一个单选按钮。
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 500, 400);
stage.setScene(scene);
}
/**
* 教师的相关操作
* ------------------------------------------------------------------------------------------------------------------------
*/
/**
* 教师登录
*/
public void teacher_log() {
stage.setTitle("教师登录");
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label teacherID = new Label("教师工号");
TextField teacherIDTxt = new TextField();
Label pwd = new Label("教师密码");
TextField pwdTxt = new TextField();
Button log = new Button("登录");
Button cancel = new Button("取消");
//点击登录按钮
log.setOnAction(actionEvent -> {
try {
if ( JDBCOperate.studentOrTeacher_log(teacherIDTxt.getText(), pwdTxt.getText(), false)) {
manageStudent(true);
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击取消按钮,返回首页面
cancel.setOnAction(actionEvent -> {
begin();
});
gridPane.add(teacherID, 0, 0);
gridPane.add(teacherIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(log, 0, 2);
gridPane.add(cancel, 1, 2);
Scene scene = new Scene(gridPane, 500, 400);
stage.setScene(scene);
}
/**
* 教师,管理员管理学生
* @param select : true.老师管理学生;false.管理员管理学生(因为老师和管理员的返回界面不同,所以要分两种情况)
*/
public void manageStudent(boolean select) {
//学生集合
ArrayList<Student> students = new ArrayList<>();
stage.setTitle("管理学生");
BorderPane borderPane = new BorderPane();
Button add = new Button("增加");
Button delete = new Button("删除");
Button update = new Button("修改");
Button query = new Button("查询");
Button refresh = new Button("刷新");
Button back = new Button("返回");
//点击增加学生
add.setOnAction(actionEvent -> {
addStudent(borderPane, students);
});
//点击删除学生
delete.setOnAction(actionEvent -> {
//删除学生和管理员删除老师的界面相同
//删除学生,true:删除学生,false:删除老师
deletePerson(borderPane, students, null, true);
});
//点击修改学生
update.setOnAction(actionEvent -> {
//先输入ID号码判断是否存在该学生,如果存在就修改学生
//true.老师或管理员修改学生;false.管理员修改老师
inputID(borderPane, students, null, true);
});
//点击查询学生
query.setOnAction(actionEvent -> {
queryStudent(borderPane, students);
});
//点击刷新
refresh.setOnAction(actionEvent -> {
refresh_studentTable(borderPane, students);
});
//点击返回
back.setOnAction(actionEvent -> {
//教师退出该页面
if (select) {
begin();
}else {
//管理员退出该页面
managerSelect();
}
});
//单行面板
HBox hBox = Utils.getHBox("-fx-font-size: 18;", 40, Pos.CENTER, true, add, delete, update, query, refresh, back);
//设置borderPane的背景颜色
borderPane.setBackground(new Background(new BackgroundFill(Color.rgb(200, 200, 200),null,null)));//设置hBox的背景颜色
//设置返回按钮的外边距
hBox.setMargin(back, new Insets(20, 20, 20, 60));
//将hBox 单行面板 添加到边界布局的顶部
borderPane.setTop(hBox);
//从数据库中把学生数据全部填写到students集合中
try {
JDBCOperate.showAll_studentTable(students);
} catch (Exception e) {
throw new RuntimeException(e);
}
//展示学生表格
studentTable(borderPane, students);
stage.setScene(new Scene(borderPane,919, 500));
}
/**
* 学生表格
* @param borderPane : 边界布局
* @param students : 学生集合
*/
public void studentTable(BorderPane borderPane, ArrayList<Student> students) {
//学生的信息表格
TableView<Student> tableView = new TableView<>();
//TableColumn类 创建列。然后使用TableView 类的 getColumns()方法将创建的列添加到表中。
TableColumn studentID = new TableColumn<>("学号");
TableColumn pwd = new TableColumn<>("密码");
TableColumn name = new TableColumn<>("姓名");
TableColumn sex = new TableColumn<>("性别");
TableColumn grade = new TableColumn<>("年级");
TableColumn classSchool = new TableColumn<>("班级");
TableColumn chinese = new TableColumn<>("语文");
TableColumn math = new TableColumn<>("数学");
TableColumn english = new TableColumn<>("英语");
TableColumn chemistry = new TableColumn<>("化学");
TableColumn political = new TableColumn<>("政治");
TableColumn history = new TableColumn<>("历史");
TableColumn totalScore = new TableColumn<>("总分");
TableColumn classTeacher = new TableColumn<>("班主任");
//表格列宽宽度设置
studentID.setMinWidth(70);
pwd.setMinWidth(70);
name.setMinWidth(60);
sex.setMinWidth(60);
grade.setMinWidth(60);
classSchool.setMinWidth(60);
chinese.setMinWidth(60);
math.setMinWidth(60);
english.setMinWidth(60);
chemistry.setMinWidth(60);
political.setMinWidth(60);
history.setMinWidth(60);
totalScore.setMinWidth(60);
classTeacher.setMinWidth(70);
//确定数据导入的列
studentID.setCellValueFactory(new PropertyValueFactory<>("studentID"));
pwd.setCellValueFactory(new PropertyValueFactory<>("pwd"));
name.setCellValueFactory(new PropertyValueFactory<>("name"));
sex.setCellValueFactory(new PropertyValueFactory<>("sex"));
grade.setCellValueFactory(new PropertyValueFactory<>("grade"));
classSchool.setCellValueFactory(new PropertyValueFactory<>("classSchool"));
chinese.setCellValueFactory(new PropertyValueFactory<>("chinese"));
math.setCellValueFactory(new PropertyValueFactory<>("math"));
english.setCellValueFactory(new PropertyValueFactory<>("english"));
chemistry.setCellValueFactory(new PropertyValueFactory<>("chemistry"));
political.setCellValueFactory(new PropertyValueFactory<>("political"));
history.setCellValueFactory(new PropertyValueFactory<>("history"));
totalScore.setCellValueFactory(new PropertyValueFactory<>("totalScore"));
classTeacher.setCellValueFactory(new PropertyValueFactory<>("classTeacher"));
//将列添加到表格中
tableView.getColumns().addAll(studentID, pwd, name, sex, grade, classSchool, chinese, math, english, chemistry, political, history, totalScore, classTeacher);
//将学生数组集合添加到表格中
tableView.getItems().addAll(students);
//将表格添加到边界布局的正中心
borderPane.setCenter(tableView);
}
/**
* 教师,管理员增加学生
* @param borderPane : 边界布局
* @param students : 学生集合
*/
public void addStudent(BorderPane borderPane, ArrayList<Student> students) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("增加学生",tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label studentID = new Label("学生学号");
TextField studentIDTxt = new TextField();
Label pwd = new Label("学生密码");
TextField pwdTxt = new TextField();
Label name = new Label("学生姓名");
TextField nameTxt = new TextField();
Label sex = new Label("性别");
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label grade = new Label("学生年级");
TextField gradeTxt = new TextField();
Label classSchool = new Label("学生班级");
TextField classSchoolTxt = new TextField();
Label classTeacher = new Label("班主任");
TextField classTeacherTxt = new TextField();
Button add = new Button("增加");
Button back = new Button("返回");
//单行面板
HBox hBox = new HBox(men, women);
gridPane.add(studentID, 0, 0);
gridPane.add(studentIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(sex, 0, 3);
gridPane.add(hBox, 1, 3);
gridPane.add(grade, 0, 4);
gridPane.add(gradeTxt, 1, 4);
gridPane.add(classSchool, 0, 5);
gridPane.add(classSchoolTxt, 1, 5);
gridPane.add(classTeacher, 0, 6);
gridPane.add(classTeacherTxt, 1, 6);
gridPane.add(add, 0, 8);
gridPane.add(back, 1, 8);
//点击增加学生
add.setOnAction(actionEvent -> {
//数据要符合规范
boolean isEmpty = !studentIDTxt.getText().equals("") && !pwdTxt.getText().equals("") && !nameTxt.getText().equals("")
&& (men.isSelected() || women.isSelected()) && !classTeacherTxt.getText().equals("");
boolean isNum = Utils.isStrToNum(gradeTxt.getText()) && Utils.isStrToNum(classSchoolTxt.getText());
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(gradeTxt.getText()), 1, 4);
try {
//学生ID不能重复;学生修改的班主任必须存在
if (isEmpty && isNum && isSuitable && !JDBCOperate.judge_IDRepeat(studentIDTxt.getText(), true) && JDBCOperate.teacher_isExit(classTeacherTxt.getText())) {
String sexDemo = men.isSelected() ? "男" : "女";
JDBCOperate.add_student(studentIDTxt.getText(), pwdTxt.getText(), nameTxt.getText(), sexDemo,
Integer.parseInt(gradeTxt.getText()), Integer.parseInt(classSchoolTxt.getText()), classTeacherTxt.getText());
tips("增加成功");
tipStage.close();
//刷新表格中的数据
refresh_studentTable(borderPane, students);
}else if (!JDBCOperate.teacher_isExit(classTeacherTxt.getText())) {
tips("请检查该班主任是否存在");
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
//设置性别只能选择一个
ToggleGroup toggleGroup = new ToggleGroup();//将单选按钮添加到到ToggleGroup对象,它将管理它们,使得一次只能选择一个单选按钮。
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 340, 400);
tipStage.setScene(scene);
tipStage.show();
}
/**
* 删除学生,老师
* @param borderPane : 边界布局
* @param students : 学生集合
* @param teachers : 教师集合
* @param b : true删除学生,false删除老师
*/
public void deletePerson(BorderPane borderPane, ArrayList<Student> students, ArrayList<Teacher> teachers, boolean b) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("删除人员", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label ID = new Label("ID号码 : ");
TextField IDTxt = new TextField();
Button delete = new Button("删除");
Button back = new Button("返回");
gridPane.add(ID, 0, 0);
gridPane.add(IDTxt, 1, 0);
gridPane.add(delete, 0, 2);
gridPane.add(back, 1, 2);
//点击删除按钮
delete.setOnAction(actionEvent -> {
try {
//删除学生,先判断是否存在该学生
if (b && JDBCOperate.judge_IDRepeat(IDTxt.getText(), true)) {
JDBCOperate.delete_studentAndTeacher(IDTxt.getText(), true);
tips("成功删除该学生");
tipStage.close();
//刷新表格中的数据
refresh_studentTable(borderPane, students);
}else if (!b && JDBCOperate.judge_IDRepeat(IDTxt.getText(), true)) { //删除老师,先判断是否存在该老师
JDBCOperate.delete_studentAndTeacher(IDTxt.getText(), false);
tips("成功删除该老师");
tipStage.close();
//刷新表格中的数据
refresh_teacherTable(borderPane, teachers);
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
Scene scene = new Scene(gridPane, 360, 200);
tipStage.setScene(scene);
tipStage.show();
}
/**
* 教师或管理员修改学生,管理员修改教师:必须先输入正确的ID号码后,才能修改
* @param borderPane : 边界布局
* @param students : 学生集合
* @param teachers : 教师集合
* @param select : true.老师或管理员修改学生;false.管理员修改老师
*/
public void inputID(BorderPane borderPane, ArrayList<Student> students, ArrayList<Teacher> teachers, boolean select) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("搜索ID号码", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label ID = new Label("ID号码 : ");
TextField IDTxt = new TextField();
Button search = new Button("搜索");
Button back = new Button("返回");
gridPane.add(ID, 0, 0);
gridPane.add(IDTxt, 1, 0);
gridPane.add(search, 0, 2);
gridPane.add(back, 1, 2);
//点击搜索按钮
search.setOnAction(actionEvent -> {
try {
//修改学生,先查询该学生是否存在
if (select && JDBCOperate.judge_IDRepeat(IDTxt.getText(), true)) {
updateStudent(borderPane, students, IDTxt.getText());
tipStage.close();
}else if(!select && JDBCOperate.judge_IDRepeat(IDTxt.getText(), false)) { //修改老师,先查询该老师是否存在
updateTeacher(borderPane, teachers, IDTxt.getText());
tipStage.close();
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
Scene scene = new Scene(gridPane, 360, 200);
tipStage.setScene(scene);
tipStage.show();
}
/**
* 输入正确的ID号码后,可以修改学生了
* @param borderPane : 边界布局
* @param students : 学生集合
* @param ID : 需要修改的学生的 原来的ID号码,我们要根据原来的ID来查找这个学生
*/
public void updateStudent(BorderPane borderPane, ArrayList<Student> students, String ID) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("修改学生", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label studentID = new Label("学号 : ");
TextField studentIDTxt = new TextField();
Label pwd = new Label("密码 : ");
TextField pwdTxt = new TextField();
Label name = new Label("姓名 : " );
TextField nameTxt = new TextField();
Label sex = new Label("性别 : " );
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label grade = new Label("年级 : ");
TextField gradeTxt = new TextField();
Label classSchool = new Label("班级 : ");
TextField classSchoolTxt = new TextField();
Label chinese = new Label("语文 : " );
TextField chineseTxt = new TextField();
Label math = new Label("数学 : " );
TextField mathTxt = new TextField();
Label english = new Label("英语 : " );
TextField englishTxt = new TextField();
Label chemistry = new Label("化学 : " );
TextField chemistryTxt = new TextField();
Label political = new Label("政治 : " );
TextField politicalTxt = new TextField();
Label history = new Label("历史 : " );
TextField historyTxt = new TextField();
Label classTeacher = new Label("班主任 : " );
TextField classTeacherTxt = new TextField();
Button update = new Button("修改");
Button back = new Button("返回");
//单行面板
HBox hBox = new HBox(men, women);
gridPane.add(studentID, 0, 0);
gridPane.add(studentIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(sex, 0, 3);
gridPane.add(hBox, 1, 3);
gridPane.add(grade, 0, 4);
gridPane.add(gradeTxt, 1, 4);
gridPane.add(classSchool, 0, 5);
gridPane.add(classSchoolTxt, 1, 5);
gridPane.add(chinese, 0, 6);
gridPane.add(chineseTxt, 1, 6);
gridPane.add(math, 0, 7);
gridPane.add(mathTxt, 1, 7);
gridPane.add(english, 0, 8);
gridPane.add(englishTxt, 1, 8);
gridPane.add(chemistry, 0, 9);
gridPane.add(chemistryTxt, 1, 9);
gridPane.add(political, 0, 10);
gridPane.add(politicalTxt, 1, 10);
gridPane.add(history, 0, 11);
gridPane.add(historyTxt, 1, 11);
gridPane.add(classTeacher, 0, 12);
gridPane.add(classTeacherTxt, 1, 12);
gridPane.add(update, 0, 14);
gridPane.add(back, 1, 14);
//点击修改按钮
update.setOnAction(actionEvent -> {
//是否为空
boolean isData = !studentIDTxt.getText().equals("") && !pwdTxt.getText().equals("") && !nameTxt.getText().equals("") &&
(men.isSelected() || women.isSelected()) && !gradeTxt.getText().equals("") && !classSchoolTxt.getText().equals("")
&& !chineseTxt.getText().equals("") && !mathTxt.getText().equals("") && !englishTxt.getText().equals("")
&& !chemistryTxt.getText().equals("") && !politicalTxt.getText().equals("") && !historyTxt.getText().equals("");
//其中的班级和分数是否可以转换为数字
boolean isNum = Utils.isStrToNum(gradeTxt.getText()) && Utils.isStrToNum(classSchoolTxt.getText()) && Utils.isStrToNum(chineseTxt.getText())
&& Utils.isStrToNum(mathTxt.getText()) && Utils.isStrToNum(englishTxt.getText()) && Utils.isStrToNum(chemistryTxt.getText()) &&
Utils.isStrToNum(politicalTxt.getText()) && Utils.isStrToNum(historyTxt.getText());
//数据范围是否合适
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(gradeTxt.getText()), 1, 4) && Utils.isDataSuitable(Integer.parseInt(chineseTxt.getText()), 0, 100)
&& Utils.isDataSuitable(Integer.parseInt(mathTxt.getText()), 0, 100) && Utils.isDataSuitable(Integer.parseInt(englishTxt.getText()), 0, 100)
&& Utils.isDataSuitable(Integer.parseInt(chemistryTxt.getText()), 0, 100) && Utils.isDataSuitable(Integer.parseInt(politicalTxt.getText()), 0, 100)
&& Utils.isDataSuitable(Integer.parseInt(historyTxt.getText()), 0, 100);
try {
//数据正常
if (isData && isNum && isSuitable && JDBCOperate.teacher_isExit(classTeacherTxt.getText())){
String sexDemo = men.isSelected() ? "男" : "女";
int totalScore = Integer.parseInt(chineseTxt.getText()) + Integer.parseInt(mathTxt.getText()) + Integer.parseInt(englishTxt.getText()) +
Integer.parseInt(chemistryTxt.getText()) + Integer.parseInt(politicalTxt.getText()) + Integer.parseInt(historyTxt.getText());
//修改学生信息
JDBCOperate.update_student(ID, studentIDTxt.getText(), pwdTxt.getText(), nameTxt.getText(), sexDemo, Integer.parseInt(gradeTxt.getText()), Integer.parseInt(classSchoolTxt.getText()),
Integer.parseInt(chineseTxt.getText()), Integer.parseInt(mathTxt.getText()), Integer.parseInt(englishTxt.getText()), Integer.parseInt(chemistryTxt.getText()),
Integer.parseInt(politicalTxt.getText()), Integer.parseInt(historyTxt.getText()), totalScore, classTeacherTxt.getText());
tips("修改成功");
tipStage.close();
//刷新表格中的数据
refresh_studentTable(borderPane, students);
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
//设置性别只能选择一个
ToggleGroup toggleGroup = new ToggleGroup();
//将单选按钮添加到到ToggleGroup对象,ToggleGroup对象将管理它们,使得一次只能选择一个单选按钮。
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 440, 680);
tipStage.setScene(scene);
tipStage.show();
}
/**
* 教师,管理员查询学生
* @param borderPane : 边界布局
* @param students : 学生集合
*/
public void queryStudent(BorderPane borderPane, ArrayList<Student> students) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("可以进行ID查询、班级查询、ID和班级共同查询、姓名模糊查询", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label studentID = new Label("学号 : ");
TextField studentIDTxt = new TextField();
Label classSchool = new Label("班级 : ");
TextField gradeTxt = new TextField();
Label name = new Label("姓名 : ");
TextField nameTxt = new TextField();
Button query = new Button("查询");
Button back = new Button("返回");
gridPane.add(studentID, 0, 0);
gridPane.add(studentIDTxt, 1, 0);
gridPane.add(classSchool, 0, 1);
gridPane.add(gradeTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(query, 0, 4);
gridPane.add(back, 1, 4);
//点击查询按钮
query.setOnAction(actionEvent -> {
//只通过ID查询学生
if (!studentIDTxt.getText().equals("") && gradeTxt.getText().equals("") && nameTxt.getText().equals("")){
//将ID符合的学生添加到students集合中
try {
JDBCOperate.show_studentTableByID(students, studentIDTxt.getText());//students 集合里面的数据以及改变了
} catch (Exception e) {
throw new RuntimeException(e);
}
studentTable(borderPane, students);
tipStage.close();
} else if (studentIDTxt.getText().equals("") && !gradeTxt.getText().equals("") && Utils.isStrToNum(gradeTxt.getText()) && nameTxt.getText().equals("")) {//只通过班级查询学生,grade必须可以转换为数字,不然从数据库中查找的时候会报错
//将grade符合的学生添加到students集合中
try {
JDBCOperate.show_studentTableByClassSchool(students, Integer.parseInt(gradeTxt.getText()) );
} catch (Exception e) {
throw new RuntimeException(e);
}
studentTable(borderPane, students);
tipStage.close();
}else if (!studentIDTxt.getText().equals("") && !gradeTxt.getText().equals("") && Utils.isStrToNum(gradeTxt.getText()) && nameTxt.getText().equals("")){//同时通过ID和班级查询,grade必须可以转换为数字
//将ID和grade同时符合的学生添加到students集合中
try {
JDBCOperate.show_studentTableByIDAndClassSchool(students, studentIDTxt.getText(), Integer.parseInt(gradeTxt.getText()));
} catch (Exception e) {
throw new RuntimeException(e);
}
studentTable(borderPane, students);
tipStage.close();
} else if (studentIDTxt.getText().equals("") && gradeTxt.getText().equals("") && !nameTxt.getText().equals("")) {//只通过姓名模糊查询
//只通过姓名模糊查询
try {
JDBCOperate.show_studentTableByName(students, nameTxt.getText());
} catch (Exception e) {
throw new RuntimeException(e);
}
studentTable(borderPane, students);
tipStage.close();
} else { //没有输入数据
tips("请检查数据");
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
Scene scene = new Scene(gridPane, 480, 400);
tipStage.setScene(scene);
tipStage.show();
}
/**
* 刷新学生表格(增加,删除,修改后调用)
* @param borderPane : 边界布局
* @param students : 学生集合
*/
public void refresh_studentTable(BorderPane borderPane, ArrayList<Student> students) {
//填写学生数据
try {
JDBCOperate.showAll_studentTable(students);
} catch (Exception e) {
throw new RuntimeException(e);
}
//展示学生表格
studentTable(borderPane, students);
}
/**
* 管理员的操作
* ------------------------------------------------------------------------------------------------------------------------
*/
/**
* 管理员登录
*/
public void managerLog() {
stage.setTitle("管理员登录");
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label managerID = new Label("管理员ID");
TextField managerIDTxt = new TextField();
Label pwd = new Label("管理员密码");
TextField pwdTxt = new TextField();
Button log = new Button("登录");
Button cancel = new Button("取消");
//点击登录按钮
log.setOnAction(actionEvent -> {
try {
if (JDBCOperate.manager_log(managerIDTxt.getText(), pwdTxt.getText())) {
managerSelect();
}else {
tips("请检查数据");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
//点击取消按钮,返回首页面
cancel.setOnAction(actionEvent -> {
begin();
});
gridPane.add(managerID, 0, 0);
gridPane.add(managerIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(log, 0, 2);
gridPane.add(cancel, 1, 2);
Scene scene = new Scene(gridPane, 500, 400);
stage.setScene(scene);
}
/**
* 管理员登录后的 选择
*/
public void managerSelect() {
stage.setTitle("管理员选项");
Button manageStudent = new Button("管理学生");
Button manageTeacher = new Button("管理教师");
Button dataStatistical = new Button("成绩统计");
Button back = new Button("退出系统");
//点击管理学生按钮
manageStudent.setOnAction(actionEvent -> {
manageStudent(false);
});
//点击管理教师按钮
manageTeacher.setOnAction(actionEvent -> {
manageTeacher();
});
//点击成绩统计
dataStatistical.setOnAction(actionEvent -> {
dataStatistical();
});
//点击退出按钮
back.setOnAction(actionEvent -> {
begin();
});
//单行面板
VBox vBox = Utils.getVBox("-fx-font-size: 18;", 40, Pos.CENTER, manageStudent, manageTeacher, dataStatistical, back);
stage.setScene(new Scene(vBox, 500, 400));
}
/**
* 管理员管理老师
*/
public void manageTeacher() {
//教师集合
ArrayList<Teacher> teachers = new ArrayList<>();
stage.setTitle("管理员管理老师");
BorderPane borderPane = new BorderPane();
Button add = new Button("增加");
Button delete = new Button("删除");
Button update = new Button("修改");
Button query = new Button("查询");
Button refresh = new Button("刷新");
Button back = new Button("返回");
//点击增加教师按钮
add.setOnAction(actionEvent -> {
addTeacher(borderPane, teachers);
});
//点击删除教师按钮
delete.setOnAction(actionEvent -> {
//删除学生和老师的界面相同
//删除老师
deletePerson(borderPane, null, teachers, false);
});
//点击修改教师按钮
update.setOnAction(actionEvent -> {
inputID(borderPane, null, teachers, false);
});
//点击查询教师按钮
query.setOnAction(actionEvent -> {
queryTeacher(borderPane);
});
//点击刷新
refresh.setOnAction(actionEvent -> {
refresh_teacherTable(borderPane, teachers);
});
//点击返回按钮
back.setOnAction(actionEvent -> {
managerSelect();
});
//单行面板
HBox hBox = Utils.getHBox("-fx-font-size: 18;", 40, Pos.CENTER, true, add, delete, update, query, refresh, back);
//设置borderPane的背景颜色
borderPane.setBackground(new Background(new BackgroundFill(Color.rgb(200, 200, 200),null,null)));//设置hBox的背景颜色
//返回按钮的外边距
hBox.setMargin(back, new Insets(20, 20, 20, 60));
//将hBox 单行面板 添加到边界布局的顶部
borderPane.setTop(hBox);
//从数据库中把老师数据全部填写到teachers
try {
JDBCOperate.showAll_teacherTable(teachers);
} catch (Exception e) {
throw new RuntimeException(e);
}
//展示教师表格
teacherTable(borderPane, teachers);
stage.setScene(new Scene(borderPane,738, 500));
}
/**
* 教师表格
* @param borderPane : 边界布局
* @param teachers : 教师集合
*/
public void teacherTable(BorderPane borderPane, ArrayList<Teacher> teachers) {
//教师的信息表格
TableView<Teacher> tableView = new TableView<>();
//TableColumn类 创建列。然后使用TableView 类的 getColumns()方法将创建的列添加到表中。
TableColumn teacherID = new TableColumn<>("工号");
TableColumn pwd = new TableColumn<>("密码");
TableColumn name = new TableColumn<>("姓名");
TableColumn sex = new TableColumn<>("性别");
TableColumn age = new TableColumn<>("年龄");
TableColumn classSchool = new TableColumn<>("班级");
TableColumn school = new TableColumn<>("学校");
TableColumn salary = new TableColumn<>("工资");
TableColumn telephone = new TableColumn<>("电话号码");
//表格列宽宽度设置
teacherID.setMinWidth(90);
name.setMinWidth(70);
sex.setMinWidth(60);
pwd.setMinWidth(85);
age.setMinWidth(70);
classSchool.setMinWidth(70);
school.setMinWidth(120);
salary.setMinWidth(70);
telephone.setMinWidth(100);
//确定数据导入的列
//引号里面的内容一定要和teacher对象的变量名称对应,Teacher还要有相关变量的set,get方法
teacherID.setCellValueFactory(new PropertyValueFactory<>("teacherID"));
pwd.setCellValueFactory(new PropertyValueFactory<>("pwd"));
name.setCellValueFactory(new PropertyValueFactory<>("name"));
sex.setCellValueFactory(new PropertyValueFactory<>("sex"));
age.setCellValueFactory(new PropertyValueFactory<>("age"));
classSchool.setCellValueFactory(new PropertyValueFactory<>("classSchool"));
school.setCellValueFactory(new PropertyValueFactory<>("school"));
salary.setCellValueFactory(new PropertyValueFactory<>("salary"));
telephone.setCellValueFactory(new PropertyValueFactory<>("telephone"));
tableView.getColumns().addAll(teacherID, pwd, name, sex, age, classSchool, school, salary, telephone);
//将教师数组集合添加到表格中
tableView.getItems().addAll(teachers);
//将表格添加到边界布局的正中心
borderPane.setCenter(tableView);
}
/**
* 管理员增加教师
* @param borderPane :边界布局
* @param teachers :教师集合
*/
public void addTeacher(BorderPane borderPane, ArrayList<Teacher> teachers) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("增加教师", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label teacherID = new Label("工号 : ");
TextField teacherIDTxt = new TextField();
Label pwd = new Label("密码 : " );
TextField pwdTxt = new TextField();
Label name = new Label("姓名 : " );
TextField nameTxt = new TextField();
Label sex = new Label("性别 : " );
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label age = new Label("年龄 : " );
TextField ageTxt = new TextField();
Label classSchool = new Label("年级 : ");
TextField classSchoolTxt = new TextField();
Label school = new Label("学校 : " );
TextField schoolTxt = new TextField();
Label salary = new Label("工资 : " );
TextField salaryTxt = new TextField();
Label telephone = new Label("电话 : " );
TextField telephoneTxt = new TextField();
Button add = new Button("增加");
Button back = new Button("返回");
//单行面板
HBox hBox = new HBox(men, women);
gridPane.add(teacherID, 0, 0);
gridPane.add(teacherIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(sex, 0, 3);
gridPane.add(hBox, 1, 3);
gridPane.add(age, 0, 4);
gridPane.add(ageTxt, 1, 4);
gridPane.add(classSchool, 0, 5);
gridPane.add(classSchoolTxt, 1, 5);
gridPane.add(school, 0, 6);
gridPane.add(schoolTxt, 1, 6);
gridPane.add(salary, 0, 7);
gridPane.add(salaryTxt, 1, 7);
gridPane.add(telephone, 0, 8);
gridPane.add(telephoneTxt, 1, 8);
gridPane.add(add, 0, 10);
gridPane.add(back, 1, 10);
//点击增加按钮
add.setOnAction(actionEvent -> {
//检查是否为空
boolean isData = !teacherIDTxt.getText().equals("") && !pwdTxt.getText().equals("") && !nameTxt.getText().equals("") &&
(men.isSelected() || women.isSelected()) && !ageTxt.getText().equals("") && !classSchoolTxt.getText().equals("") &&
!schoolTxt.getText().equals("") && !salaryTxt.getText().equals("") && !telephoneTxt.getText().equals("");
//部分数据是否可以转换为数字
boolean isNum = Utils.isStrToNum(classSchoolTxt.getText()) && Utils.isStrToNum(ageTxt.getText()) && Utils.isStrToNum(salaryTxt.getText());
//数据是否在合适的范围内
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(ageTxt.getText()), 22, 100);
if (isData && isNum && isSuitable){
String sexDemo = men.isSelected() ? "男" : "女";
//增加
try {
JDBCOperate.add_teacher(teacherIDTxt.getText(), pwdTxt.getText(), nameTxt.getText(), sexDemo, Integer.parseInt(ageTxt.getText()),
Integer.parseInt(classSchoolTxt.getText()), schoolTxt.getText(), Integer.parseInt(salaryTxt.getText()), telephoneTxt.getText());
//刷新教师表格
refresh_teacherTable(borderPane, teachers);
tips("增加成功");
tipStage.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}else {
tips("请检查数据");
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
//设置性别只能选择一个
ToggleGroup toggleGroup = new ToggleGroup();//将单选按钮添加到到ToggleGroup对象,它将管理它们,使得一次只能选择一个单选按钮。
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 340, 640);
tipStage.setScene(scene);
tipStage.show();
}
/**
*
* @param ID
*/
/**
* 输入正确的ID号码后,管理员可以修改老师了
* @param borderPane :边界布局
* @param teachers :教师集合
* @param ID : 需要修改的教师的 ID号码,我们将根据原先的ID来找到该教师
*/
public void updateTeacher(BorderPane borderPane, ArrayList<Teacher> teachers, String ID) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("修改学生", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label teacherID = new Label("ID : ");
TextField teacherIDTxt = new TextField();
Label pwd = new Label("密码 : ");
TextField pwdTxt = new TextField();
Label name = new Label("姓名 : " );
TextField nameTxt = new TextField();
Label sex = new Label("性别 : " );
RadioButton men = new RadioButton("男");
RadioButton women = new RadioButton("女");
Label age = new Label("年龄 : " );
TextField ageTxt = new TextField();
Label classSchool = new Label("班级 : ");
TextField classSchoolTxt = new TextField();
Label school = new Label("学校 : " );
TextField schoolTxt = new TextField();
Label salary = new Label("工资 : " );
TextField salaryTxt = new TextField();
Label telephone = new Label("电话 : " );
TextField telephoneTxt = new TextField();
Button update = new Button("修改");
Button back = new Button("返回");
//单行面板
HBox hBox = new HBox(men, women);
gridPane.add(teacherID, 0, 0);
gridPane.add(teacherIDTxt, 1, 0);
gridPane.add(pwd, 0, 1);
gridPane.add(pwdTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(sex, 0, 3);
gridPane.add(hBox, 1, 3);
gridPane.add(age, 0, 4);
gridPane.add(ageTxt, 1, 4);
gridPane.add(classSchool, 0, 5);
gridPane.add(classSchoolTxt, 1, 5);
gridPane.add(school, 0, 6);
gridPane.add(schoolTxt, 1, 6);
gridPane.add(salary, 0, 7);
gridPane.add(salaryTxt, 1, 7);
gridPane.add(telephone, 0, 8);
gridPane.add(telephoneTxt, 1, 8);
gridPane.add(update, 0, 10);
gridPane.add(back, 1, 10);
//点击修改按钮
update.setOnAction(actionEvent -> {
//检查数据是否为空
boolean isData = !teacherIDTxt.getText().equals("") && !pwdTxt.getText().equals("") && !nameTxt.getText().equals("") &&
(men.isSelected() || women.isSelected())&& !ageTxt.getText().equals("") && !classSchoolTxt.getText().equals("") &&
!schoolTxt.getText().equals("") && !salaryTxt.getText().equals("") && !telephoneTxt.getText().equals("");
//部分数据是否可以转换为数字
boolean isNum = Utils.isStrToNum(classSchoolTxt.getText()) && Utils.isStrToNum(ageTxt.getText()) && Utils.isStrToNum(salaryTxt.getText());
boolean isSuitable = Utils.isDataSuitable(Integer.parseInt(ageTxt.getText()), 22, 100);
if (isData && isNum && isSuitable){
String sexDemo = men.isSelected() ? "男" : "女";
//修改教师信息
try {
JDBCOperate.update_teacher(ID, teacherIDTxt.getText(), pwdTxt.getText(), nameTxt.getText(), sexDemo, Integer.parseInt(ageTxt.getText()), Integer.parseInt(classSchoolTxt.getText()), schoolTxt.getText(), Integer.parseInt(salaryTxt.getText()), telephoneTxt.getText());
tips("修改成功");
tipStage.close();
//刷新教师表格
refresh_teacherTable(borderPane, teachers);
} catch (Exception e) {
throw new RuntimeException(e);
}
}else {
tips("请检查数据");
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
//设置性别只能选择一个
ToggleGroup toggleGroup = new ToggleGroup();//将单选按钮添加到到ToggleGroup对象,它将管理它们,使得一次只能选择一个单选按钮。
men.setToggleGroup(toggleGroup);
women.setToggleGroup(toggleGroup);
Scene scene = new Scene(gridPane, 440, 680);
tipStage.setScene(scene);
tipStage.show();
}
/**
* 管理员查询教师
* @param borderPane : 边界布局
*/
public void queryTeacher(BorderPane borderPane) {
//创建提示框窗口对象
Stage tipStage = new Stage();
setTopTip("可以进行ID查询、班级查询、ID和班级共同查询、姓名模糊查询", tipStage);
GridPane gridPane = new GridPane();
Utils.setGridPane(gridPane);
Label teacherID = new Label("工号 : ");
TextField teacherIDTxt = new TextField();
Label classSchool = new Label("班级 : ");
TextField classSchoolTxt = new TextField();
Label name = new Label("姓名");
TextField nameTxt = new TextField();
Button query = new Button("查询");
Button back = new Button("返回");
gridPane.add(teacherID, 0, 0);
gridPane.add(teacherIDTxt, 1, 0);
gridPane.add(classSchool, 0, 1);
gridPane.add(classSchoolTxt, 1, 1);
gridPane.add(name, 0, 2);
gridPane.add(nameTxt, 1, 2);
gridPane.add(query, 0, 4);
gridPane.add(back, 1, 4);
//点击查询按钮
query.setOnAction(actionEvent -> {
ArrayList<Teacher> teachers = new ArrayList<>();
//只查询ID
if (!teacherIDTxt.getText().equals("") && classSchoolTxt.getText().equals("") && nameTxt.getText().equals("")){
//将ID符合的教师添加到teachers集合中
try {
JDBCOperate.show_teacherTableByID(teachers, teacherIDTxt.getText());
} catch (Exception e) {
throw new RuntimeException(e);
}
teacherTable(borderPane,teachers);
tipStage.close();
} else if (teacherIDTxt.getText().equals("") && !classSchoolTxt.getText().equals("") && Utils.isStrToNum(classSchoolTxt.getText()) && nameTxt.getText().equals("")) { //只查询班级,而且grade必须可以转换为数字
//将classSchool符合的教师添加到teachers集合中
try {
JDBCOperate.show_teacherTableByClassSchool(teachers, Integer.parseInt(classSchoolTxt.getText()));
} catch (Exception e) {
throw new RuntimeException(e);
}
teacherTable(borderPane, teachers);
tipStage.close();
}else if (!teacherIDTxt.getText().equals("") && !classSchoolTxt.getText().equals("") && Utils.isStrToNum(classSchoolTxt.getText()) && nameTxt.getText().equals("")){ //同时查询ID和班级,而且grade必须可以转换为数字
//将ID和classSchool同时符合的教师添加到teachers集合中
try {
JDBCOperate.show_teacherTableByIDAndClassSchool(teachers, teacherIDTxt.getText(), Integer.parseInt(classSchoolTxt.getText()));
} catch (Exception e) {
throw new RuntimeException(e);
}
teacherTable(borderPane, teachers);
tipStage.close();
} else if (teacherIDTxt.getText().equals("") && classSchoolTxt.getText().equals("") && !nameTxt.getText().equals("")) {
//只通过姓名模糊查询
try {
JDBCOperate.show_teacherTableByName(teachers, nameTxt.getText());
} catch (Exception e) {
throw new RuntimeException(e);
}
teacherTable(borderPane,teachers);
tipStage.close();
} else { //没有输入数据
tips("请检查数据");
}
});
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
Scene scene = new Scene(gridPane, 340, 400);
tipStage.setScene(scene);
tipStage.show();
}
/**
* 刷新教师表格
* @param borderPane : 边界布局
* @param teachers : 教师集合
*/
public void refresh_teacherTable(BorderPane borderPane, ArrayList<Teacher> teachers) {
//填写全部的教师数据
try {
JDBCOperate.showAll_teacherTable(teachers);
} catch (Exception e) {
throw new RuntimeException(e);
}
//展示教师表格
teacherTable(borderPane, teachers);
}
/**
* 成绩柱状图
*/
public void dataStatistical() {
stage.setTitle("成绩统计");
Button back = new Button("返回");
//点击返回
back.setOnAction(actionEvent -> {
managerSelect();
});
CategoryAxis xAxis = new CategoryAxis();
xAxis.setLabel("成绩分布");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("人数");
BarChart barChart = new BarChart<>(xAxis, yAxis);
XYChart.Series dataSeries1 = new XYChart.Series();
dataSeries1.setName("年级1");
XYChart.Series dataSeries2 = null;
XYChart.Series dataSeries3 = null;
XYChart.Series dataSeries4 = null;
try {
dataSeries1.getData().add(new XYChart.Data("0<=x<200", JDBCOperate.histogram(1, 0, 200)));
dataSeries1.getData().add(new XYChart.Data("200<=x<350" , JDBCOperate.histogram(1, 200, 350)));
dataSeries1.getData().add(new XYChart.Data("350<=x<500" , JDBCOperate.histogram(1, 350, 500)));
dataSeries1.getData().add(new XYChart.Data("500<=x<=600" , JDBCOperate.histogram(1, 500, 601)));
dataSeries2 = new XYChart.Series();
dataSeries2.setName("年级2");
dataSeries2.getData().add(new XYChart.Data("0<=x<200", JDBCOperate.histogram(2, 0, 200)));
dataSeries2.getData().add(new XYChart.Data("200<=x<350" , JDBCOperate.histogram(2, 200, 350)));
dataSeries2.getData().add(new XYChart.Data("350<=x<500" , JDBCOperate.histogram(2, 350, 500)));
dataSeries2.getData().add(new XYChart.Data("500<=x<=600" , JDBCOperate.histogram(2, 500, 601)));
dataSeries3 = new XYChart.Series();
dataSeries3.setName("年级3");
dataSeries3.getData().add(new XYChart.Data("0<=x<200", JDBCOperate.histogram(3, 0, 200)));
dataSeries3.getData().add(new XYChart.Data("200<=x<350" , JDBCOperate.histogram(3, 200, 350)));
dataSeries3.getData().add(new XYChart.Data("350<=x<500" , JDBCOperate.histogram(3, 350, 500)));
dataSeries3.getData().add(new XYChart.Data("500<=x<=600" , JDBCOperate.histogram(3, 500, 601)));
dataSeries4 = new XYChart.Series();
dataSeries4.setName("年级4");
dataSeries4.getData().add(new XYChart.Data("0<=x<200", JDBCOperate.histogram(4, 0, 200)));
dataSeries4.getData().add(new XYChart.Data("200<=x<350" , JDBCOperate.histogram(4, 200, 350)));
dataSeries4.getData().add(new XYChart.Data("350<=x<500" , JDBCOperate.histogram(4, 350, 500)));
dataSeries4.getData().add(new XYChart.Data("500<=x<=600" , JDBCOperate.histogram(4, 500, 601)));
} catch (SQLException e) {
throw new RuntimeException(e);
}
barChart.getData().add(dataSeries1);
barChart.getData().add(dataSeries2);
barChart.getData().add(dataSeries3);
barChart.getData().add(dataSeries4);
//单列面板
VBox vBox = new VBox(barChart, back);
vBox.setAlignment(Pos.CENTER);
vBox.setMargin(back, new Insets(20, 0, 0, 0));
Scene scene = new Scene(vBox, 500, 540);
stage.setScene(scene);
}
/**
* 一些通用的代码
* ------------------------------------------------------------------------------------------------------------------------
*/
/**
* 设置顶层窗口的一些属性
* @param str : 标题的内容
* @param tipStage : 需要添加属性的窗口
*/
public void setTopTip(String str, Stage tipStage) {
tipStage.setResizable(false);//不能调整大小
tipStage.setTitle(str);//设置标题
//只能操作当前的提示框
tipStage.initOwner(stage);
tipStage.initModality(Modality.WINDOW_MODAL);
//设置该提示框永远在最上层
tipStage.setAlwaysOnTop(true);
}
/**
* 提示框
* @param content : 提示的信息
*/
void tips(String content) {
//创建提示框窗口对象
Stage tipStage = new Stage();
//调用顶层窗口函数设置一些必要的属性
setTopTip(content, tipStage);
Label label = new Label(content);
Button back = new Button("返回");
//点击返回按钮,关闭提示框
back.setOnAction(actionEvent -> {
tipStage.close();
});
//单列面板
VBox vBox = new VBox(label, back);
vBox.setSpacing(10);
vBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vBox, 150, 130);
tipStage.setScene(scene);
tipStage.show();
}
}
2.Student类
package student_manage;
public class Student {
String studentID;
String pwd;
String name;
String sex;
int grade;
int classSchool;
int chinese;
int math;
int english;
int chemistry;
int political;
int history;
int totalScore;
String classTeacher;
public Student() {
}
public Student(String studentID, String pwd, String name, String sex, int grade, int classSchool, int chinese, int math, int english, int chemistry, int political, int history, int totalScore, String classTeacher) {
this.studentID = studentID;
this.pwd = pwd;
this.name = name;
this.sex = sex;
this.grade = grade;
this.classSchool = classSchool;
this.chinese = chinese;
this.math = math;
this.english = english;
this.chemistry = chemistry;
this.political = political;
this.history = history;
this.totalScore = totalScore;
this.classTeacher = classTeacher;
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public int getClassSchool() {
return classSchool;
}
public void setClassSchool(int classSchool) {
this.classSchool = classSchool;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getChemistry() {
return chemistry;
}
public void setChemistry(int chemistry) {
this.chemistry = chemistry;
}
public int getPolitical() {
return political;
}
public void setPolitical(int political) {
this.political = political;
}
public int getHistory() {
return history;
}
public void setHistory(int history) {
this.history = history;
}
public int getTotalScore() {
return totalScore;
}
public void setTotalScore(int totalScore) {
this.totalScore = totalScore;
}
public String getClassTeacher() {
return classTeacher;
}
public void setClassTeacher(String classTeacher) {
this.classTeacher = classTeacher;
}
}
3.Teacher类
package student_manage;
public class Teacher {
String teacherID;
String pwd;
String name;
String sex;
int age;
int classSchool;
String school;
int salary;
String telephone;
public Teacher() {
}
public Teacher(String teacherID, String pwd, String name, String sex, int age, int classSchool, String school, int salary, String telephone) {
this.teacherID = teacherID;
this.pwd = pwd;
this.name = name;
this.sex = sex;
this.age = age;
this.classSchool = classSchool;
this.school = school;
this.salary = salary;
this.telephone = telephone;
}
public String getTeacherID() {
return teacherID;
}
public void setTeacherID(String teacherID) {
this.teacherID = teacherID;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getClassSchool() {
return classSchool;
}
public void setClassSchool(int classSchool) {
this.classSchool = classSchool;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
4.Utils工具类
package student_manage;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
//工具类
public class Utils {
/**
* 设置GridPane布局的一些属性
* @param gridPane : 网格布局
*/
public static void setGridPane(GridPane gridPane) {
//设置每个组件之间的间距
gridPane.setHgap(10);//设置节点间的水平间距,也就是列与列的间距(节点就是组件)
gridPane.setVgap(10);//节点间的垂直间距,也就是行与行的间距
//设置组件在中间分布
gridPane.setAlignment(Pos.CENTER);
gridPane.setStyle("-fx-font-size: 18;");
}
/**
* 设置VBox单列面板布局的一些属性
* @param style : 字体类型
* @param spacing : 间距
* @param value : 设置组件的位置
* @param children : 组件
* @return : VBox
*/
public static VBox getVBox(String style, int spacing, Pos value, Node... children) {
VBox vBox = new VBox(children);
//字体类型
vBox.setStyle(style);
//间距
vBox.setSpacing(spacing);
//设置组件的位置
vBox.setAlignment(value);
return vBox;
}
/**
* 设置HBox单行面板布局的一些属性
* @param style : 字体类型
* @param spacing : 间距
* @param value : 设置组件的位置
* @param isSetBackground : true:说明需要设置背景颜色,是表格;false:不需要设置背景颜色
* @param children : 组件
* @return : HBox
*/
public static HBox getHBox(String style, int spacing, Pos value, boolean isSetBackground, Node... children) {
HBox hBox = new HBox(children);
//字体类型
hBox.setStyle(style);
//间距
hBox.setSpacing(spacing);
//设置组件的位置
hBox.setAlignment(value);
//设置hBox的背景颜色
if (isSetBackground) {
hBox.setBackground(new Background(new BackgroundFill(Color.rgb(119, 168, 157), null, null)));
}
return hBox;
}
/**
* 判断字符串是否可以转换为数字(用来查询使用)
* @param str : 输入字符串;str为空时,返回false
* @return : true.可以转换为数字;false.不可以
*/
public static boolean isStrToNum(String str) {
try {
Integer.parseInt(str);//如果可以执行这行代码,就代表可以转换为数字,否则return false
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* 判断数据是否在正确的区间(0<=每科成绩<=100;1<=年级<=4;班级>=0)
* @param data : 数据的大小
* @param min : 允许的最小值(包含最小值)
* @param max: 允许的最大值(包含最大值)
*/
public static boolean isDataSuitable(int data, int min, int max) {
return data >= min && data <= max;
}
}
5.SQL语句
package student_manage;
import java.sql.*;
import java.util.ArrayList;
public class JDBCOperate {
/**
* 判断学生,老师的ID是否存在
* @param ID : ID
* @param b : true.学生,false.老师
* @return : true:存在该数据,false:不存在
* @throws Exception
*/
public static boolean judge_IDRepeat(String ID, boolean b) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = null;
if (b) {
sql = "select * from student_table where studentID = ?";
}else {
sql = "select * from teacher_table where teacherID = ?";
}
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, ID);
ResultSet rs = pstmt.executeQuery();
try {
return rs.next();
} finally {
closeResource(rs, pstmt, conn);
}
}
/**
* 学生登录,教师登录,查询ID和密码是否正确
* @param ID : ID
* @param pwd : 密码
* @param b : true学生登录,false老师登录
* @return : true.正确;false.错误
* @throws Exception
*/
public static boolean studentOrTeacher_log(String ID, String pwd, boolean b) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = null;
if (b) {
sql = "select * from student_table where studentID = ? && pwd = ?";
}else {
sql = "select * from teacher_table where teacherID = ? && pwd = ?";
}
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, ID);
pstmt.setString(2, pwd);
ResultSet rs = pstmt.executeQuery();
try {
return rs.next();
} finally {
closeResource(rs, pstmt, conn);
}
}
/**
* 学生查看分数,从数据库中查找相应的分数
* @param student : 为该学生对象的属性赋值
* @param studentID : 通过该ID找到对应的学生
* @param pwd : 密码
* @throws Exception
*/
public static void student_checkScore(Student student, String studentID, String pwd) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = "select * from student_table where studentID = ? && pwd = ?";
PreparedStatement pstmt =conn.prepareStatement(sql);
pstmt.setString(1, studentID);
pstmt.setString(2, pwd);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
student.name = rs.getString("name");
student.sex = rs.getString("sex");
student.grade = rs.getInt("grade");
student.classSchool = rs.getInt("classSchool");
student.chinese = rs.getInt("chinese");
student.math = rs.getInt("math");
student.english = rs.getInt("english");
student.chemistry = rs.getInt("chemistry");
student.political = rs.getInt("political");
student.history = rs.getInt("history");
student.totalScore = rs.getInt("totalScore");
student.classTeacher = rs.getString("classTeacher");
}
closeResource(rs, pstmt, conn);
}
/**
* 学生注册;老师或管理员 增加学生
* @param studentID
* @param grade
* @param name
* @param sex
* @param pwd
* @param classTeacher
*/
public static void student_signOrAdd(String studentID, String pwd, String name, String sex, int grade, int classSchool, String classTeacher) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = "insert into student_table values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, studentID);
pstmt.setString(2, pwd);
pstmt.setString(3, name);
pstmt.setString(4, sex);
pstmt.setInt(5, grade);
pstmt.setInt(6, classSchool);
pstmt.setInt(7, 0);
pstmt.setInt(8, 0);
pstmt.setInt(9, 0);
pstmt.setInt(10, 0);
pstmt.setInt(11, 0);
pstmt.setInt(12, 0);
pstmt.setInt(13, 0);
pstmt.setString(14, classTeacher);
int count = pstmt.executeUpdate();
if (count > 0) System.out.println("成功注册或增加了一个学生");
closeResource(null, pstmt, conn);
}
/**
* 学生注册;学生修改信息;老师、管理员增加学生;老师、管理员修改学生的时候,学生的班主任必须存在(通过名字查询该老师,名字必须完全一样)
* @param name
*/
public static boolean teacher_isExit(String name) throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = "select * from teacher_table where name = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
return rs.next();
}
/**
* 学生自己修改自己的信息(无法修改学号和成绩)
* @param oldID
* @param pwd
* @param name
* @param sex
* @param grade
* @param classSchool
* @param classTeacher
* @throws Exception
*/
public static void student_modify(String oldID, String pwd, String name, String sex, int grade, int classSchool, String classTeacher) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = "update student_table set pwd=?, name=?, sex=?, grade=?, classSchool=?, classTeacher=? where studentID = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, pwd);
pstmt.setString(2, name);
pstmt.setString(3, sex);
pstmt.setInt(4, grade);
pstmt.setInt(5, classSchool);
pstmt.setString(6, classTeacher);
pstmt.setString(7, oldID);
int count = pstmt.executeUpdate();
if (count > 0) System.out.println("学生更改了自己的数据");
closeResource(null, pstmt, conn);
}
/**
* 操作学生
* ------------------------------------------------------------------------------------------------------------------------
*/
/**
* 老师或管理员 增加学生
* @param studentID
* @param grade
* @param name
* @param sex
* @param pwd
* @param classTeacher
*/
public static void add_student(String studentID, String pwd, String name, String sex, int grade, int classSchool, String classTeacher) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = "insert into student_table values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, studentID);
pstmt.setString(2, pwd);
pstmt.setString(3, name);
pstmt.setString(4, sex);
pstmt.setInt(5, grade);
pstmt.setInt(6, classSchool);
pstmt.setInt(7, 0);
pstmt.setInt(8, 0);
pstmt.setInt(9, 0);
pstmt.setInt(10, 0);
pstmt.setInt(11, 0);
pstmt.setInt(12, 0);
pstmt.setInt(13, 0);
pstmt.setString(14, classTeacher);
int count = pstmt.executeUpdate();
if (count > 0) System.out.println("成功增加了一个学生");
closeResource(null, pstmt, conn);
}
/**
* 删除学生和删除教师
* @param ID
* @param b : true删除学生,false删除老师
* @throws Exception
*/
public static void delete_studentAndTeacher(String ID, boolean b) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = null;
//学生
if (b) {
sql = "delete from student_table where studentID = ?";
}else { //老师
sql = "delete from teacher_table where teacherID = ?";
}
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,ID);
int count = pstmt.executeUpdate();
closeResource(null, pstmt, conn);
}
/**
* 老师,管理员修改学生信息(可以修改学号,成绩)
* @param oldID : 之前的学号,根据这个学号来查找到学生
* @param studentID
* @param grade
* @param name
* @param sex
* @param pwd
* @param chinese
* @param math
* @param english
* @param chemistry
* @param political
* @param history
* @param totalScore
* @param classTeacher
* @throws Exception
*/
public static void update_student(String oldID, String studentID, String pwd, String name, String sex, int grade, int classSchool, int chinese, int math, int english, int chemistry, int political, int history, int totalScore, String classTeacher) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = "update student_table set studentID=?, pwd=?, name=?, sex=?, grade=?, classSchool=?, chinese=?, math=?, english=?, chemistry=?, political=?, history=?, totalScore=?, classTeacher=? where studentID = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, studentID);
pstmt.setString(2, pwd);
pstmt.setString(3, name);
pstmt.setString(4, sex);
pstmt.setInt(5, grade);
pstmt.setInt(6, classSchool);
pstmt.setInt(7, chinese);
pstmt.setInt(8, math);
pstmt.setInt(9, english);
pstmt.setInt(10, chemistry);
pstmt.setInt(11, political);
pstmt.setInt(12, history);
pstmt.setInt(13, totalScore);
pstmt.setString(14, classTeacher);
pstmt.setString(15, oldID);
int count = pstmt.executeUpdate();
if (count > 0) System.out.println("成功更新了学生数据");
closeResource(null, pstmt, conn);
}
/**
* 把学生数据库中的 所有 数据传递给学生表格(和老师,管理员登录后首先看到的学生表格有关,也和刷新按钮有关)
* @param students
* @throws Exception
*/
public static void showAll_studentTable(ArrayList<Student> students) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
//先清空所有的数据
students.clear();
String sql = "select * from student_table";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String studentID = rs.getString("studentID");
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String sex = rs.getString("sex");
int grade = rs.getInt("grade");
int classSchool = rs.getInt("classSchool");
int chinese = rs.getInt("chinese");
int math = rs.getInt("math");
int english = rs.getInt("english");
int chemistry = rs.getInt("chemistry");
int political = rs.getInt("political");
int history = rs.getInt("history");
int totalScore = rs.getInt("totalScore");
String classTeacher = rs.getString("classTeacher");
//创建student对象,并添加到ArrayList
Student student = new Student(studentID, pwd, name, sex, grade, classSchool, chinese, math, english, chemistry, political, history, totalScore, classTeacher);
students.add(student);
}
//关闭资源
closeResource(rs, pstmt, conn);
}
/**
* 通过 ID查询 把学生数据库中的数据传递给学生表格
* @param students
* @param studentID
* @throws Exception
*/
public static void show_studentTableByID(ArrayList<Student> students, String studentID) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
//先清空所有的数据
students.clear();
String sql = "select * from student_table where studentID = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, studentID);
ResultSet rs = pstmt.executeQuery();
//因为ID是唯一的,只有一个,所以不用使用while()
if (rs.next()) {
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String sex = rs.getString("sex");
int grade = rs.getInt("grade");
int classSchool = rs.getInt("classSchool");
int chinese = rs.getInt("chinese");
int math = rs.getInt("math");
int english = rs.getInt("english");
int chemistry = rs.getInt("chemistry");
int political = rs.getInt("political");
int history = rs.getInt("history");
int totalScore = rs.getInt("totalScore");
String classTeacher = rs.getString("classTeacher");
//创建student对象,并添加到ArrayList
Student student = new Student(studentID, pwd, name, sex, grade, classSchool, chinese, math, english, chemistry, political, history, totalScore, classTeacher);
students.add(student);
}
//关闭资源
closeResource(rs, pstmt, null);
}
/**
* 通过 班级查询 把学生数据库中的数据传递给学生表格
* @param students
* @param classSchool
* @throws Exception
*/
public static void show_studentTableByClassSchool(ArrayList<Student> students, int classSchool) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
//先清空所有的数据
students.clear();
String sql = "select * from student_table where classSchool = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, classSchool);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String studentID = rs.getString("studentID");
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String sex = rs.getString("sex");
int grade = rs.getInt("grade");
int chinese = rs.getInt("chinese");
int math = rs.getInt("math");
int english = rs.getInt("english");
int chemistry = rs.getInt("chemistry");
int political = rs.getInt("political");
int history = rs.getInt("history");
int totalScore = rs.getInt("totalScore");
String classTeacher = rs.getString("classTeacher");
//创建student对象,并添加到ArrayList
Student student = new Student(studentID, pwd, name, sex, grade, classSchool, chinese, math, english, chemistry, political, history, totalScore, classTeacher);
students.add(student);
}
//关闭资源
closeResource(rs, pstmt, null);
}
/**
* 同时通过 ID和班级查询 把学生数据库中的数据传递给学生表格
* @param students
* @param studentID
* @param classSchool
* @throws Exception
*/
public static void show_studentTableByIDAndClassSchool(ArrayList<Student> students, String studentID, int classSchool) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
//先清空所有的数据
students.clear();
String sql = "select * from student_table where studentID = ? and classSchool = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, studentID);
pstmt.setInt(2, classSchool);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String sex = rs.getString("sex");
int grade = rs.getInt("grade");
int chinese = rs.getInt("chinese");
int math = rs.getInt("math");
int english = rs.getInt("english");
int chemistry = rs.getInt("chemistry");
int political = rs.getInt("political");
int history = rs.getInt("history");
int totalScore = rs.getInt("totalScore");
String classTeacher = rs.getString("classTeacher");
//创建student对象,并添加到ArrayList
Student student = new Student(studentID, pwd, name, sex, grade, classSchool, chinese, math, english, chemistry, political, history, totalScore, classTeacher);
students.add(student);
}
//关闭资源
closeResource(rs, pstmt, null);
}
/**
* 通过姓名模糊查询 把学生数据库中的数据传递给学生表格
* @param students
* @param name
* @throws Exception
*/
public static void show_studentTableByName(ArrayList<Student> students, String name) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
//先清空所有的数据
students.clear();
//模糊查询
String sql = "select * from student_table where name like ?" + " '%'; ";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String studentID = rs.getString("studentID");
String n = rs.getString("name");
String pwd = rs.getString("pwd");
String sex = rs.getString("sex");
int grade = rs.getInt("grade");
int classSchool = rs.getInt("classSchool");
int chinese = rs.getInt("chinese");
int math = rs.getInt("math");
int english = rs.getInt("english");
int chemistry = rs.getInt("chemistry");
int political = rs.getInt("political");
int history = rs.getInt("history");
int totalScore = rs.getInt("totalScore");
String classTeacher = rs.getString("classTeacher");
//创建student对象,并添加到ArrayList
Student student = new Student(studentID, pwd, n, sex, grade, classSchool, chinese, math, english, chemistry, political, history, totalScore, classTeacher);
students.add(student);
}
//关闭资源
closeResource(rs, pstmt, null);
}
/**
* 操作老师
* ------------------------------------------------------------------------------------------------------------------------
*/
/**
* 增加教师
* @param teacherID
* @param name
* @param sex
* @param pwd
* @param age
* @param classSchool
* @param school
* @param salary
* @param telephone
* @throws Exception
*/
public static void add_teacher(String teacherID, String pwd, String name, String sex, int age, int classSchool, String school, int salary, String telephone) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = "insert into teacher_table values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, teacherID);
pstmt.setString(2, pwd);
pstmt.setString(3, name);
pstmt.setString(4, sex);
pstmt.setInt(5, age);
pstmt.setInt(6, classSchool);
pstmt.setString(7, school);
pstmt.setInt(8, salary);
pstmt.setString(9, telephone);
int count = pstmt.executeUpdate();
if (count > 0) System.out.println("成功注册或增加了一个教师");
closeResource(null, pstmt, conn);
}
/**
* 更新教师
* @param ID
* @param teacherID
* @param name
* @param sex
* @param pwd
* @param age
* @param classSchool
* @param school
* @param salary
* @param telephone
* @throws Exception
*/
public static void update_teacher(String ID, String teacherID, String pwd, String name, String sex, int age, int classSchool, String school, int salary, String telephone) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = "update teacher_table set teacherID=?, pwd=?, name=?, sex=?, age=?, classSchool=?, school=?, salary=?, telephone=? where teacherID = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, teacherID);
pstmt.setString(2, pwd);
pstmt.setString(3, name);
pstmt.setString(4, sex);
pstmt.setInt(5, age);
pstmt.setInt(6, classSchool);
pstmt.setString(7, school);
pstmt.setInt(8, salary);
pstmt.setString(9, telephone);
pstmt.setString(10,ID);
int count = pstmt.executeUpdate();
if (count > 0) System.out.println("成功更新了教师数据");
closeResource(null, pstmt, null);
}
/**
* 把教师数据库中的 所有 数据传递给教师表格(和管理员登录后首先看到的教师表格有关,也和刷新按钮有关)
* @param teachers
* @throws Exception
*/
public static void showAll_teacherTable(ArrayList<Teacher> teachers) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
//先清空所有的数据
teachers.clear();
String sql = "select * from teacher_table";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String teacherID = rs.getString("teacherID");
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt("age");
int classSchool = rs.getInt("classSchool");
String school = rs.getString("school");
int salary = rs.getInt("salary");
String telephone = rs.getString("telephone");
//创建teacher对象,并添加到ArrayList
Teacher teacher = new Teacher(teacherID, pwd, name, sex, age, classSchool, school, salary, telephone);
teachers.add(teacher);
}
//关闭资源
closeResource(rs, pstmt, conn);
}
/**
* 通过 ID查询 把老师数据库中的数据传递给老师表格
* @param teachers
* @param teacherID
* @throws Exception
*/
public static void show_teacherTableByID(ArrayList<Teacher> teachers, String teacherID) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
//先清空所有的数据
teachers.clear();
String sql = "select * from teacher_table where teacherID = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, teacherID);
ResultSet rs = pstmt.executeQuery();
//因为ID是唯一的,只有一个,所以不用使用while()
if (rs.next()) {
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt("age");
int classSchool = rs.getInt("classSchool");
String school = rs.getString("school");
int salary = rs.getInt("salary");
String telephone = rs.getString("telephone");
//创建teacher对象,并添加到ArrayList
Teacher teacher = new Teacher(teacherID, pwd, name, sex, age, classSchool, school, salary, telephone);
teachers.add(teacher);
}
//关闭资源
closeResource(rs, pstmt, conn);
}
/**
* 通过 班级查询 把学生数据库中的数据传递给学生表格
* @param teachers
* @param classSchool
* @throws Exception
*/
public static void show_teacherTableByClassSchool(ArrayList<Teacher> teachers, int classSchool) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
//先清空所有的数据
teachers.clear();
String sql = "select * from teacher_table where classSchool = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, classSchool);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String teacherID = rs.getString("teacherID");
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt("age");
String school = rs.getString("school");
int salary = rs.getInt("salary");
String telephone = rs.getString("telephone");
//创建teacher对象,并添加到ArrayList
Teacher teacher = new Teacher(teacherID, pwd, name, sex, age, classSchool, school, salary, telephone);
teachers.add(teacher);
}
//关闭资源
closeResource(rs, pstmt, conn);
}
/**
* 同时通过 ID和班级查询 把学生数据库中的数据传递给学生表格
* @param teachers
* @param teacherID
* @param classSchool
* @throws Exception
*/
public static void show_teacherTableByIDAndClassSchool(ArrayList<Teacher> teachers, String teacherID, int classSchool) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
//先清空所有的数据
teachers.clear();
String sql = "select * from teacher_table where teacherID = ? and classSchool = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, teacherID);
pstmt.setInt(2, classSchool);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt("age");
String school = rs.getString("school");
int salary = rs.getInt("salary");
String telephone = rs.getString("telephone");
//创建teacher对象,并添加到ArrayList
Teacher teacher = new Teacher(teacherID, pwd, name, sex, age, classSchool, school, salary, telephone);
teachers.add(teacher);
}
//关闭资源
closeResource(rs, pstmt, null);
}
/**
* 通过姓名模糊查询 把教师数据库中的数据传递给教师表格
* @param teachers
* @param name
* @throws Exception
*/
public static void show_teacherTableByName(ArrayList<Teacher> teachers, String name) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
//先清空所有的数据
teachers.clear();
//模糊查询
String sql = "select * from teacher_table where name like ?" + " '%'; ";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String teacherID = rs.getString("teacherID");
String pwd = rs.getString("pwd");
String n = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt("age");
int classSchool = rs.getInt("classSchool");
String school = rs.getString("school");
int salary = rs.getInt("salary");
String telephone = rs.getString("telephone");
//创建student对象,并添加到ArrayList
Teacher teacher = new Teacher(teacherID, pwd, n, sex, age, classSchool, school, salary, telephone);
teachers.add(teacher);
}
//关闭资源
closeResource(rs, pstmt, null);
}
/**
* 管理员的登录和柱状图
* ------------------------------------------------------------------------------------------------------------------------
*/
/**
* 管理员登录
* @param ID
* @param pwd
* @return
* @throws Exception
*/
public static boolean manager_log(String ID, String pwd) throws Exception {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = "select * from manager_table where ID = ? && pwd = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, ID);
pstmt.setString(2, pwd);
ResultSet rs = pstmt.executeQuery();
return rs.next();
}
/**
* 计算各个年级,各个分数段的人数
* @param grade : 年级
* @param from : 起始位置(包含)
* @param to : 终止位置(不包含)
* @return
*/
public static int histogram(int grade, int from, int to) throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student_managesystem?useSSL=false", "root", "root");
String sql = "select studentID from student_table where grade = ? && totalScore >= ? && totalScore < ?;";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, grade);
pstmt.setInt(2, from);
pstmt.setInt(3, to);
ResultSet rs = pstmt.executeQuery();
int total = 0;
while (rs.next()) {
total++;
}
return total;
}
/**
* 其他通用的
* ------------------------------------------------------------------------------------------------------------------------
*/
/**
* 关闭资源
* @param rs
* @param stmt
* @param conn
*/
public static void closeResource(ResultSet rs, Statement stmt, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}