介绍:
本篇文章是关于javafx和mysql实现的一个简易的学生管理系统。
需要源码私信作者即可免费获取
辅助软件:Scene Builder
一、基本功能
1、学生注册
2、学生登陆
3、学生成绩查询
4、教师登陆
5、教师管理学生(增删查改)
6、管理员登陆
7、管理员管理学生和老师(增删查改)
以上操作都需要jdbc连接数据库
二、er图
三、系统结构图
代码
每一个fxml文件对应一个controller类
且fxml文件由sceneBuilder软件自动生成
页面及控制器
登陆页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="421.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.LoginController">
<children>
<Label layoutX="230.0" layoutY="14.0" prefHeight="119.0" prefWidth="157.0" style="-fx-font-weight: 700;" text="登陆">
<font>
<Font size="68.0" />
</font>
</Label>
<Label layoutX="148.0" layoutY="140.0" prefHeight="45.0" prefWidth="79.0" text="账号">
<font>
<Font size="23.0" />
</font></Label>
<Label layoutX="148.0" layoutY="208.0" prefHeight="45.0" prefWidth="79.0" text="密码">
<font>
<Font size="23.0" />
</font></Label>
<TextField fx:id="userField" layoutX="227.0" layoutY="140.0" prefHeight="45.0" prefWidth="225.0" />
<PasswordField fx:id="psField" layoutX="227.0" layoutY="208.0" prefHeight="45.0" prefWidth="225.0" />
<Button fx:id="loginButton" layoutX="359.0" layoutY="295.0" mnemonicParsing="false" onAction="#Login" text="学生登陆">
<font>
<Font size="28.0" />
</font></Button>
<Button fx:id="registerButton" layoutX="148.0" layoutY="295.0" mnemonicParsing="false" onAction="#Registe" text="注册">
<font>
<Font size="28.0" />
</font>
</Button>
<Button fx:id="loginTeacherButton" layoutX="359.0" layoutY="355.0" mnemonicParsing="false" onAction="#LoginTeacher" text="教师登陆">
<font>
<Font size="28.0" />
</font>
</Button>
<Button fx:id="loginManageButton" layoutX="148.0" layoutY="355.0" mnemonicParsing="false" onAction="#LoginManage" text="管理员登陆">
<font>
<Font size="28.0" />
</font>
</Button>
</children>
</Pane>
controller
package controller;
import Dao.jdbc;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class LoginController {
@FXML
PasswordField psField;
@FXML
TextField userField;
@FXML
private Button loginButton;
@FXML
private Button registerButton;
@FXML
private Button loginTeacherButton;
@FXML
private Button loginManageButton;
//初始化方法
public void initialize() throws Exception {
}
@FXML
// 点击登陆按钮 进行页面跳转
void Login(ActionEvent event) throws Exception{
// 点击登陆按钮,判断账号密码是否为数据库里的且是否正确,如果不是,显示弹窗,如果是,则进入学生信息页面
// 即使文本框和密码框为空,getText返回的是一个String 的:"" 而不是null 故可以用isEmpty检查是否为""
if ( psField.getText().isEmpty()|| userField.getText().isEmpty() ){
showErrorMessage("账号或密码不能为空!");//比用label 再新创建一个stage和scene好用 方便
} else if (jdbc.isLoginStudent(userField.getText(),psField.getText())){
// 将id文本框的内容传递给StudentController
StudentController.setStudentId(userField.getText());
loginPane();
} else {
showErrorMessage("账号或密码错误!");
}
}
@FXML
// 管理员登陆
void LoginManage(ActionEvent event) throws Exception {
if ( psField.getText().isEmpty()|| userField.getText().isEmpty() ){
showErrorMessage("账号或密码不能为空!");//比用label 再新创建一个stage和scene好用 方便
}else if (jdbc.isloginManager(userField.getText(),psField.getText()) ){
TeacherController.setTeacherId(userField.getText());
loginMannager();
}else {
showErrorMessage("账号或密码错误!");
}
}
@FXML
// 教师登陆按钮
void LoginTeacher(ActionEvent event) throws Exception{
if ( psField.getText().isEmpty()|| userField.getText().isEmpty() ){
showErrorMessage("账号或密码不能为空!");//比用label 再新创建一个stage和scene好用 方便
}else if (jdbc.isLoginTeacher(userField.getText(),psField.getText())){
TeacherController.setTeacherId(userField.getText());
loginTeacher();
}else {
showErrorMessage("账号或密码错误!");
}
}
@FXML
// 点击注册按钮 ,跳转到注册的页面
void Registe(ActionEvent event) throws Exception{
registePane();
}
// 登陆时提示错误信息的弹窗
static void showErrorMessage(String message) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
// 切换到学生信息面板
public void loginPane() throws Exception{
Stage stage=(Stage) loginButton.getScene().getWindow();
Parent root=FXMLLoader.load(getClass().getResource("Student.fxml"));
stage.setTitle("Student information");
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
// 切换到学生注册面板
public void registePane() throws Exception{
Stage stage=(Stage) loginButton.getScene().getWindow();
Parent root=FXMLLoader.load(getClass().getResource("Register.fxml"));
stage.setTitle("Student registe");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
// 教师登陆
public void loginTeacher() throws Exception{
Stage stage=(Stage) loginButton.getScene().getWindow();
Parent root=FXMLLoader.load(getClass().getResource("Teacher.fxml"));
stage.setTitle("TeacherController infomation");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
// 管理员登陆 财务部
public void loginMannager() throws Exception{
Stage stage=(Stage) loginButton.getScene().getWindow();
Parent root=FXMLLoader.load(getClass().getResource("Manager.fxml"));
stage.setTitle("TeacherController infomation");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
}
学生注册页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.RegisteController">
<children>
<Pane layoutX="174.0" layoutY="39.0" prefHeight="348.0" prefWidth="252.0">
<children>
<Text layoutX="27.0" layoutY="53.0" strokeType="OUTSIDE" strokeWidth="0.0" text="学号" wrappingWidth="31.0" />
<Text layoutX="29.0" layoutY="99.0" strokeType="OUTSIDE" strokeWidth="0.0" text="班级" />
<Text layoutX="29.0" layoutY="142.0" strokeType="OUTSIDE" strokeWidth="0.0" text="姓名" />
<Text layoutX="29.0" layoutY="186.0" strokeType="OUTSIDE" strokeWidth="0.0" text="学院" />
<Text layoutX="29.0" layoutY="226.0" strokeType="OUTSIDE" strokeWidth="0.0" text="密码" />
<TextField fx:id="idField" layoutX="68.0" layoutY="36.0" promptText="请输入学号" />
<TextField fx:id="classField" layoutX="68.0" layoutY="74.0" promptText="请输入班级" />
<TextField fx:id="nameField" layoutX="68.0" layoutY="125.0" promptText="请输入姓名" />
<TextField fx:id="collegeField" layoutX="68.0" layoutY="169.0" promptText="请输入学院" />
<PasswordField fx:id="passwordField" layoutX="68.0" layoutY="209.0" promptText="请输入密码" />
<ButtonBar layoutX="29.0" layoutY="263.0" prefHeight="40.0" prefWidth="200.0">
<buttons>
<Button fx:id="backButton" mnemonicParsing="false" onAction="#back" text="返回" />
<Button fx:id="registeButton" mnemonicParsing="false" onAction="#registeSubmit" text="注册" />
</buttons>
</ButtonBar>
</children>
</Pane>
<Text layoutX="283.0" layoutY="37.0" strokeType="OUTSIDE" strokeWidth="0.0" text="注册" wrappingWidth="48.21630859375">
<font>
<Font size="24.0" />
</font>
</Text>
</children>
</AnchorPane>
controller
//学生注册页面
package controller;
import Dao.jdbc;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import pojo.Student;
//学生注册页面
public class RegisteController {
@FXML
private TextField classField;
@FXML
private TextField collegeField;
@FXML
private TextField idField;
@FXML
private TextField nameField;
@FXML
private PasswordField passwordField;
@FXML
private Button backButton;
// 想要实现的功能是:点击提交,将文本框里面的数据传递给数据库,进行添加学生的操作
@FXML
void registeSubmit(ActionEvent event)throws Exception {
// 不能注册重复的id
if (jdbc.Student_id(idField.getText())){
showErrorMessage("注册失败! id重复!");
}else if (idField.getText().isEmpty() && passwordField.getText().isEmpty() && collegeField.getText().isEmpty() && classField.getText().isEmpty() && nameField.getText().isEmpty() ){
showErrorMessage("注册失败!内容不能为空!"); //不能注册空学生
}else{
Student student=new Student();
jdbc.addStuent(student,idField.getText(),passwordField.getText(), collegeField.getText(),classField.getText(),nameField.getText());
showRightMessage("账号注册成功!");
}
}
@FXML
void back(ActionEvent event) throws Exception{
back();
}
public void back() throws Exception{
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("Login.fxml"));
stage.setTitle("登陆");
stage.setX(447);
stage.setY(141);
backButton.getScene().setRoot(root);
}
// 注册时提示 注册失败 的弹窗
static void showErrorMessage(String message) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
// 注册时提示 注册成功 的弹窗
static void showRightMessage(String message) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("成功");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
}
学生信息页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.StudentController">
<children>
<Label layoutX="214.0" layoutY="86.0" prefHeight="30.0" prefWidth="58.0" text="学院:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="214.0" layoutY="140.0" prefHeight="30.0" prefWidth="58.0" text="班级:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="214.0" layoutY="190.0" prefHeight="30.0" prefWidth="58.0" text="姓名:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="214.0" layoutY="247.0" prefHeight="30.0" prefWidth="58.0" text="性别:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="studentLabel1" layoutX="296.0" layoutY="82.0" prefHeight="39.0" prefWidth="225.0" text="学院">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="223.0" layoutY="20.0" prefHeight="45.0" prefWidth="151.0" text="学生个人信息">
<font>
<Font size="24.0" />
</font>
</Label>
<Label fx:id="studentLabel2" layoutX="297.0" layoutY="136.0" prefHeight="39.0" prefWidth="124.0" text="班级">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="studentLabel3" layoutX="297.0" layoutY="186.0" prefHeight="39.0" prefWidth="132.0" text="姓名">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="studentLabel4" layoutX="296.0" layoutY="243.0" prefHeight="39.0" prefWidth="104.0" text="性别">
<font>
<Font size="14.0" />
</font>
</Label>
<ButtonBar layoutX="165.0" layoutY="298.0" prefHeight="40.0" prefWidth="202.0">
<buttons>
<Button fx:id="backButton" mnemonicParsing="false" onAction="#back" text="返回" />
<Button fx:id="queryButton" mnemonicParsing="false" onAction="#queryScore" text="成绩查询" />
</buttons>
</ButtonBar>
</children>
</AnchorPane>
controller
package controller;
import Dao.jdbc;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import pojo.Student;
public class StudentController {
@FXML
private Label studentLabel1;
@FXML
private Label studentLabel2;
@FXML
private Label studentLabel3;
@FXML
private Label studentLabel4;
@FXML
private Button backButton;
@FXML
private Button queryButton;
private static Student student=new Student();
//初始化方法
// 显示学生的信息
public void initialize() throws Exception {
jdbc.Student(student,student.getId());
studentLabel1.setText(student.getCollege());
studentLabel2.setText(student.getClas());
studentLabel3.setText(student.getName());
studentLabel4.setText(student.getSex());
// 将该学生传递给成绩页面
ScoreController.setStudent(student);
}
// 想要初始化页面,让该学生的信息显示 出来,也就是将学生对应的信息 设置 label的Text
// 将LoginController中的输入的文本框的id传递过来
public static void setStudentId(String id){
student.setId(id);
}
@FXML
void back(ActionEvent event) throws Exception{
loginPane();
}
@FXML
void queryScore(ActionEvent event) throws Exception {
scorePane();
}
// 切换到 学生 登陆 面板
public void loginPane() throws Exception{
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root=FXMLLoader.load(getClass().getResource("Login.fxml"));
stage.setTitle("Login");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
// 切换到学生 成绩查询 面板
public void scorePane() throws Exception{
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root=FXMLLoader.load(getClass().getResource("Score.fxml"));
stage.setTitle("Student Score");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
}
学生成绩页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.ScoreController">
<children>
<Label layoutX="185.0" layoutY="20.0" prefHeight="51.0" prefWidth="188.0" text="大一第二学期期末成绩">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="50.0" layoutY="88.0" prefHeight="42.0" prefWidth="129.0" text="大学语文:">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="50.0" layoutY="145.0" prefHeight="42.0" prefWidth="129.0" text="高等数学:">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="50.0" layoutY="200.0" prefHeight="42.0" prefWidth="129.0" text="大学英语:">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="50.0" layoutY="257.0" prefHeight="42.0" prefWidth="129.0" text="大学物理:">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="279.0" layoutY="88.0" prefHeight="42.0" prefWidth="129.0" text="电子技术:">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="279.0" layoutY="145.0" prefHeight="42.0" prefWidth="129.0" text="java程序设计:">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="279.0" layoutY="200.0" prefHeight="42.0" prefWidth="129.0" text="大学体育:">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="279.0" layoutY="257.0" prefHeight="42.0" prefWidth="129.0" text="思想政治:">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="s1" layoutX="162.0" layoutY="93.0" prefHeight="32.0" prefWidth="65.0" text="60">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="s2" layoutX="162.0" layoutY="150.0" prefHeight="32.0" prefWidth="65.0" text="60">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="s3" layoutX="162.0" layoutY="205.0" prefHeight="32.0" prefWidth="65.0" text="60">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="s4" layoutX="162.0" layoutY="262.0" prefHeight="32.0" prefWidth="65.0" text="60">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="s5" layoutX="408.0" layoutY="93.0" prefHeight="32.0" prefWidth="65.0" text="60">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="s6" layoutX="408.0" layoutY="150.0" prefHeight="32.0" prefWidth="65.0" text="60">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="s7" layoutX="408.0" layoutY="205.0" prefHeight="32.0" prefWidth="65.0" text="60">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="s8" layoutX="408.0" layoutY="262.0" prefHeight="32.0" prefWidth="65.0" text="60">
<font>
<Font size="14.0" />
</font>
</Label>
<Button fx:id="backButton" layoutX="212.0" layoutY="335.0" mnemonicParsing="false" onAction="#back" text="返回" />
</children>
</AnchorPane>
controller
package controller;
import Dao.jdbc;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import pojo.Student;
public class ScoreController {
@FXML
private Label s1;
@FXML
private Label s2;
@FXML
private Label s3;
@FXML
private Label s4;
@FXML
private Label s5;
@FXML
private Label s6;
@FXML
private Label s7;
@FXML
private Label s8;
@FXML
private Button backButton;
static Student student=new Student();
//初始化方法
// 展示该学生的成绩
public void initialize() throws Exception {
jdbc.getStudentScore(student,student.getId());
s1.setText(String.valueOf(student.getChinese()));
s2.setText(String.valueOf(student.getMath()));
s3.setText(String.valueOf(student.getEnglish()));
s4.setText(String.valueOf(student.getPhysics()));
s5.setText(String.valueOf(student.getElectronic()));
s6.setText(String.valueOf(student.getJava()));
s7.setText(String.valueOf(student.getPe()));
s8.setText(String.valueOf(student.getPolitics()));
}
// 获取该学生的对象信息
public static void setStudent(Student s){
student=s;
}
@FXML
void back(ActionEvent event) throws Exception{
studentInfoPane();
}
// 切换到学生信息面板
public void studentInfoPane() throws Exception{
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("Student.fxml"));
stage.setTitle("Student information");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
}
教师信息页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.TeacherController">
<children>
<Label layoutX="210.0" layoutY="22.0" prefHeight="50.0" prefWidth="156.0" text="教师个人信息">
<font>
<Font size="24.0" />
</font>
</Label>
<Label layoutX="89.0" layoutY="82.0" prefHeight="40.0" prefWidth="81.0" text="姓名:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="89.0" layoutY="130.0" prefHeight="40.0" prefWidth="81.0" text="性别:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="89.0" layoutY="178.0" prefHeight="40.0" prefWidth="106.0" text="出生日期:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="89.0" layoutY="226.0" prefHeight="40.0" prefWidth="98.0" text="所属学院:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="336.0" layoutY="84.0" prefHeight="40.0" prefWidth="81.0" text="职称:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="336.0" layoutY="134.0" prefHeight="40.0" prefWidth="98.0" text="联系方式:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="336.0" layoutY="182.0" prefHeight="40.0" prefWidth="106.0" text="状态:">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="336.0" layoutY="230.0" prefHeight="40.0" prefWidth="98.0" text="薪资:">
<font>
<Font size="18.0" />
</font>
</Label>
<Button fx:id="backButton" layoutX="111.0" layoutY="294.0" mnemonicParsing="false" onAction="#back" text="返回">
<font>
<Font size="18.0" />
</font>
</Button>
<Button fx:id="manageStudentButton" layoutX="318.0" layoutY="294.0" mnemonicParsing="false" onAction="#manageStudent" text="管理学生">
<font>
<Font size="18.0" />
</font>
</Button>
<Label fx:id="t1" layoutX="195.0" layoutY="90.0" prefHeight="27.0" prefWidth="134.0" text="曹嘉毕">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="t2" layoutX="195.0" layoutY="137.0" prefHeight="27.0" prefWidth="134.0" text="男">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="t3" layoutX="195.0" layoutY="185.0" prefHeight="27.0" prefWidth="134.0" text="2000/01/01">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="t4" layoutX="195.0" layoutY="232.0" prefHeight="27.0" prefWidth="134.0" text="数学学院">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="t5" layoutX="442.0" layoutY="91.0" prefHeight="27.0" prefWidth="134.0" text="讲师">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="t6" layoutX="442.0" layoutY="141.0" prefHeight="27.0" prefWidth="134.0" text="13487329812">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="t7" layoutX="442.0" layoutY="189.0" prefHeight="27.0" prefWidth="134.0" text="在职">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="t8" layoutX="442.0" layoutY="236.0" prefHeight="27.0" prefWidth="134.0" text="8000">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</AnchorPane>
controller
package controller;
import Dao.jdbc;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import pojo.Teacher;
import static controller.ScoreController.student;
public class TeacherController {
@FXML
private Label t1;
@FXML
private Label t2;
@FXML
private Label t3;
@FXML
private Label t4;
@FXML
private Label t5;
@FXML
private Label t6;
@FXML
private Label t7;
@FXML
private Label t8;
@FXML
private Button backButton;
@FXML
private Button manageStudentButton;
static Teacher teacher=new Teacher();
//初始化方法
public void initialize() throws Exception {
// 初始化老师信息
jdbc.Teacher(teacher,teacher.getId());
// 将数据库里老师的 个人信息 传递给 teacher对象
t1.setText(teacher.getName());
t2.setText(teacher.getSex());
t3.setText(teacher.getBorn());
t4.setText(teacher.getCollege());
t5.setText(teacher.getTitle());
t6.setText(teacher.getNumber());
t7.setText(teacher.getState());
t8.setText(String.valueOf(teacher.getSalary()));
// 将该老师信息 传递给成绩页面
// ScoreController.setStudent(student);
}
@FXML
void back(ActionEvent event) throws Exception{
backPane();
}
@FXML
// 老师管理学生页面
void manageStudent(ActionEvent event) throws Exception{
manageStudentPane();
}
// 将LoginController中的输入的文本框的id传递过来
public static void setTeacherId(String id){
teacher.setId(id);
}
public void backPane() throws Exception{
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("Login.fxml"));
stage.setTitle("Login");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
public void manageStudentPane() throws Exception {
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root=FXMLLoader.load(getClass().getResource("Tch_Manage_Stu.fxml"));
stage.setTitle("Teacher Manage Student");
stage.setX(200);
stage.setScene(new Scene(root, 1000, 600));
stage.show();
}
}
教师管理学生页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Tch_Manage_Stu_C">
<children>
<Label layoutX="21.0" layoutY="49.0" prefHeight="29.0" prefWidth="62.0" text="查找学生">
<font>
<Font size="14.0" />
</font>
</Label>
<TableView fx:id="studentInfomation" layoutY="109.0" prefHeight="491.0" prefWidth="1000.0">
<columns>
<TableColumn fx:id="idColumn" prefWidth="61.0" text="学号" />
<TableColumn fx:id="collegeColumn" prefWidth="153.0" text="学院" />
<TableColumn fx:id="nameColumn" prefWidth="62.0" text="姓名" />
<TableColumn fx:id="classColumn" prefWidth="75.0" text="班级" />
<TableColumn fx:id="chineseColumn" prefWidth="75.0" text="大学语文" />
<TableColumn fx:id="mathColumn" prefWidth="75.0" text="高等数学" />
<TableColumn fx:id="englishColumn" prefWidth="75.0" text="大学英语" />
<TableColumn fx:id="physicsColumn" prefWidth="75.0" text="大学物理" />
<TableColumn fx:id="electronicColumn" prefWidth="75.0" text="电子技术" />
<TableColumn fx:id="javaColumn" prefWidth="97.0" text="Java程序设计" />
<TableColumn fx:id="peColumn" prefWidth="74.0" text="大学体育" />
<TableColumn fx:id="politicsColumn" prefWidth="74.0" text="思想政治" />
</columns>
</TableView>
<TextField fx:id="searchField" layoutX="91.0" layoutY="51.0" prefHeight="25.0" prefWidth="169.0" promptText="学号、姓名、班级、学院..." />
<ButtonBar layoutX="344.0" layoutY="44.0" prefHeight="40.0" prefWidth="268.0">
<buttons>
<Button fx:id="deleteStudentButton" mnemonicParsing="false" onAction="#deleteStudent" text="删除学生" />
<Button fx:id="addStudentButton" mnemonicParsing="false" onAction="#addStudent" text="添加学生" />
<Button fx:id="updateStudentButton" mnemonicParsing="false" onAction="#updateStudent" text="修改学生" />
</buttons>
</ButtonBar>
<Button fx:id="figureButton" layoutX="266.0" layoutY="51.0" mnemonicParsing="false" onAction="#figureStudent" text="查询" />
<Button fx:id="backButton" layoutX="822.0" layoutY="51.0" mnemonicParsing="false" onAction="#back" text="返回" />
</children>
</AnchorPane>
controller
package controller;
import Dao.jdbc;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;
import pojo.Student;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
public class Tch_Manage_Stu_C {
@FXML
private TableColumn<Student, String> idColumn;
@FXML
private TableColumn<Student, String> classColumn;
@FXML
private TableColumn<Student, String>nameColumn;
@FXML
private TableColumn<Student, String> collegeColumn;
@FXML
private TableColumn<Student, Double> chineseColumn;
@FXML
private TableColumn<Student,Double> mathColumn;
@FXML
private TableColumn<Student, Double> englishColumn;
@FXML
private TableColumn<Student,Double> physicsColumn;
@FXML
private TableColumn<Student, Double> electronicColumn;
@FXML
private TableColumn<Student, Double> javaColumn;
@FXML
private TableColumn<Student,Double> peColumn;
@FXML
private TableColumn<Student,Double> politicsColumn;
@FXML
private Button addStudentButton;
@FXML
private Button deleteStudentButton;
@FXML
private Button figureButton;
@FXML
private TextField searchField;
@FXML
private Button updateStudentButton;
@FXML
private Button backButton;
@FXML
private TableView<Student> studentInfomation;
List<Student> studentList=new ArrayList<>();
ObservableList<Student> observableList=FXCollections.observableList(studentList);
List<Student> studentList2=new ArrayList<>();
//初始化方法
public void initialize() throws Exception {
// 展示数据库里所有学生的信息
// 1 初始化所有学生
jdbc.StudentAllInfo(studentList);
// 2 获取所有学生的信息
idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
classColumn.setCellValueFactory(new PropertyValueFactory<>("clas"));
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
collegeColumn.setCellValueFactory(new PropertyValueFactory<>("college"));
chineseColumn.setCellValueFactory(new PropertyValueFactory<>("chinese"));
mathColumn.setCellValueFactory(new PropertyValueFactory<>("math"));
englishColumn.setCellValueFactory(new PropertyValueFactory<>("english"));
physicsColumn.setCellValueFactory(new PropertyValueFactory<>("physics"));
electronicColumn.setCellValueFactory(new PropertyValueFactory<>("electronic"));
javaColumn.setCellValueFactory(new PropertyValueFactory<>("java"));
peColumn.setCellValueFactory(new PropertyValueFactory<>("pe"));
politicsColumn.setCellValueFactory(new PropertyValueFactory<>("politics"));
// 设置 TableView 的数据
studentInfomation.setItems(observableList);
// studentInfomation.setItems((ObservableList<Student>) studentList);
}
@FXML
// 点击 添加学生 的按钮,跳转到添加学生信息的页面
void addStudent(ActionEvent event) throws Exception{
addStudentPane();
}
@FXML
// 选中一个学生的行,点击删除按钮,进行删除
void deleteStudent(ActionEvent event) throws Exception {
// 获取这一行的数据
Student selectedStudent=studentInfomation.getSelectionModel().getSelectedItem();
if (selectedStudent !=null){
// 你的 studentList 是一个 ArrayList,而不是 ObservableList。这意味着即使你从列表中删除了元素,
// TableView 也不会自动更新来反映这些更改,因为 TableView 绑定到的是一个 ObservableList(如果你已经这样做了的话)。
// 但是,这不是 remove 方法不工作的直接原因;它只是意味着 UI 不会更新。
// equals 和 hashCode 方法的实现:
// 如之前所述,ArrayList 的 remove 方法依赖于对象的 equals 方法来查找要删除的元素。如果 Student 类的 equals
// 方法没有正确实现,那么 remove 方法可能找不到并删除 selectedStudent。
// 数据源同步问题:
// 如果 TableView 的 items 属性被设置为一个与 studentList 不同的列表(比如一个基于 studentList 创建的
// ObservableList),那么从 studentList 中删除元素将不会影响 TableView 的显示。
// studentList.remove(selectedStudent);
observableList.remove(selectedStudent);
// studentList.remove(selectedStudent);
// 同步删除数据库里的数据
jdbc.deleteStudent(selectedStudent);
}
}
@FXML
// 按照 学号、姓名 查找学生
// 点击查询按钮就跳转到 查询后的页面
void figureStudent(ActionEvent event) throws Exception {
if (studentList2!=null){
studentList2.clear();
}
// 将文本框的内容传递给数据库,进行模糊查询
// id查询
jdbc.findStudent(studentList2,searchField.getText());
// 将查询后的结果传给 查询后的 页面
FindedStudentControlelr.setStudentList(studentList2);
// 最后进行页面的跳转
findedStudentPane();
}
@FXML
// 修改学生信息
void updateStudent(ActionEvent event) throws Exception{
// 单元格可编辑模式开启失败
// studentInfomation.setEditable(true);
// idColumn.setCellFactory(TextFieldTableCell.forTableColumn());
// 获取要修改的学生的信息
Student selectedStudent=studentInfomation.getSelectionModel().getSelectedItem();
// 将要被修改的学生信息传递给修改页面
UpdateStudent.setSelectStudent(selectedStudent);
updateStudentPane();
}
@FXML
void back(ActionEvent event) throws Exception{
backPane();
}
public void addStudentPane() throws Exception {
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("AddStudent.fxml"));
stage.setTitle("Teacher Manage Student");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
public void updateStudentPane() throws Exception {
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("UpdateStudent.fxml"));
stage.setTitle("Teacher Update Student");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
public void backPane() throws Exception {
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root=FXMLLoader.load(getClass().getResource("Teacher.fxml"));
stage.setTitle("TeacherController infomation");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
public void findedStudentPane() throws Exception {
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("FindedStudent.fxml"));
stage.setTitle("Teacher Find Student");
stage.setX(200);
stage.setScene(new Scene(root, 1000, 600));
stage.show();
}
}
添加学生页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.AddStudent">
<children>
<Label layoutX="172.0" layoutY="87.0" prefHeight="23.0" prefWidth="34.0" text="学号">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="172.0" layoutY="127.0" prefHeight="23.0" prefWidth="34.0" text="姓名">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="172.0" layoutY="169.0" prefHeight="23.0" prefWidth="34.0" text="学院">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="172.0" layoutY="208.0" prefHeight="23.0" prefWidth="34.0" text="班级">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="221.0" layoutY="14.0" text="添加学生">
<font>
<Font size="24.0" />
</font>
</Label>
<TextField fx:id="add1" layoutX="206.0" layoutY="86.0" />
<TextField fx:id="add2" layoutX="206.0" layoutY="126.0" />
<TextField fx:id="add3" layoutX="206.0" layoutY="168.0" />
<TextField fx:id="add4" layoutX="206.0" layoutY="207.0" />
<Button fx:id="backButton" layoutX="196.0" layoutY="295.0" mnemonicParsing="false" onAction="#back" text="返回" />
<Button fx:id="submitButton" layoutX="299.0" layoutY="295.0" mnemonicParsing="false" onAction="#submit" text="提交" />
</children>
</AnchorPane>
controller
package controller;
import Dao.jdbc;
import Dao.juge;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import pojo.Student;
//老师添加学生页面
public class AddStudent {
@FXML
private TextField add1;
@FXML
private TextField add2;
@FXML
private TextField add3;
@FXML
private TextField add4;
@FXML
private Button backButton;
@FXML
private Button submitButton;
@FXML
void back(ActionEvent event) throws Exception{
backPane();
}
// 进行添加学生的操作
// 将文本框里面的内容传递给数据库,进行学生的添加
@FXML
void submit(ActionEvent event) throws Exception {
if (jdbc.Student_id(add1.getText())){
juge.showErrorMessage("注册失败! id重复!");
}else if(add1.getText().isEmpty() && add2.getText().isEmpty() && add3.getText().isEmpty() && add4.getText().isEmpty()){
juge.showErrorMessage("添加失败!内容不能为空!");
}
else {
Student student=new Student();
jdbc.TaddStuent(student,add1.getText(),add2.getText(), add3.getText(),add4.getText());
juge.showRightMessage("学生添加成功!");
}
}
public void backPane() throws Exception {
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("Tch_Manage_Stu.fxml"));
stage.setTitle("Teacher Manage Student");
stage.setX(200);
stage.setScene(new Scene(root, 1000, 600));
stage.show();
}
}
修改学生页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.UpdateStudent">
<children>
<Label layoutX="34.0" layoutY="96.0" prefHeight="23.0" prefWidth="34.0" text="学号">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="34.0" layoutY="136.0" prefHeight="23.0" prefWidth="34.0" text="姓名">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="34.0" layoutY="178.0" prefHeight="23.0" prefWidth="34.0" text="学院">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="34.0" layoutY="217.0" prefHeight="23.0" prefWidth="34.0" text="班级">
<font>
<Font size="14.0" />
</font>
</Label>
<TextField fx:id="u1" layoutX="68.0" layoutY="95.0" />
<TextField fx:id="u2" layoutX="68.0" layoutY="135.0" />
<TextField fx:id="u3" layoutX="68.0" layoutY="177.0" />
<TextField fx:id="u4" layoutX="68.0" layoutY="216.0" />
<Label layoutX="243.0" layoutY="25.0" text="修改学生信息">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="243.0" layoutY="96.0" prefHeight="23.0" prefWidth="58.0" text="大学语文">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="243.0" layoutY="136.0" prefHeight="23.0" prefWidth="58.0" text="高等数学">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="243.0" layoutY="178.0" prefHeight="23.0" prefWidth="58.0" text="大学英语">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="243.0" layoutY="217.0" prefHeight="23.0" prefWidth="58.0" text="大学物理">
<font>
<Font size="14.0" />
</font>
</Label>
<TextField fx:id="u5" layoutX="301.0" layoutY="95.0" prefHeight="25.0" prefWidth="58.0" />
<TextField fx:id="u6" layoutX="301.0" layoutY="135.0" prefHeight="25.0" prefWidth="58.0" />
<TextField fx:id="u7" layoutX="301.0" layoutY="177.0" prefHeight="25.0" prefWidth="58.0" />
<TextField fx:id="u8" layoutX="301.0" layoutY="216.0" prefHeight="25.0" prefWidth="58.0" />
<TextField fx:id="u9" layoutX="460.0" layoutY="95.0" prefHeight="25.0" prefWidth="58.0" />
<TextField fx:id="u10" layoutX="460.0" layoutY="135.0" prefHeight="25.0" prefWidth="58.0" />
<TextField fx:id="u11" layoutX="460.0" layoutY="177.0" prefHeight="25.0" prefWidth="58.0" />
<TextField fx:id="u12" layoutX="460.0" layoutY="216.0" prefHeight="25.0" prefWidth="58.0" />
<Label layoutX="392.0" layoutY="96.0" prefHeight="23.0" prefWidth="58.0" text="电子技术">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="392.0" layoutY="136.0" prefHeight="23.0" prefWidth="58.0" text="java程序">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="392.0" layoutY="178.0" prefHeight="23.0" prefWidth="58.0" text="大学体育">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="392.0" layoutY="217.0" prefHeight="23.0" prefWidth="58.0" text="思想政治">
<font>
<Font size="14.0" />
</font>
</Label>
<Button fx:id="backButton" layoutX="221.0" layoutY="285.0" mnemonicParsing="false" onAction="#back" text="返回" />
<Button fx:id="submitButton" layoutX="351.0" layoutY="285.0" mnemonicParsing="false" onAction="#submit" text="提交" />
<Label fx:id="tip1" layoutX="76.0" layoutY="78.0" prefHeight="17.0" prefWidth="122.0" text="不可修改学号!!!" textFill="#da0808">
<font>
<Font size="10.0" />
</font>
</Label>
<Label fx:id="tip2" layoutX="76.0" layoutY="119.0" prefHeight="17.0" prefWidth="108.0" text="不可修改姓名!!!" textFill="#da0808">
<font>
<Font size="10.0" />
</font>
</Label>
</children>
</AnchorPane>
controller
package controller;
import Dao.jdbc;
import Dao.juge;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;
import pojo.Student;
import java.awt.event.MouseEvent;
//老师修改学生信息的页面
public class UpdateStudent {
@FXML
private Label tip1;
@FXML
private Label tip2;
@FXML
private Button backButton;
@FXML
private Button submitButton;
@FXML
private TextField u1;
@FXML
private TextField u10;
@FXML
private TextField u11;
@FXML
private TextField u12;
@FXML
private TextField u2;
@FXML
private TextField u3;
@FXML
private TextField u4;
@FXML
private TextField u5;
@FXML
private TextField u6;
@FXML
private TextField u7;
@FXML
private TextField u8;
@FXML
private TextField u9;
static Student student=new Student();
//初始化方法
// 让本来的信息传递过来
public void initialize() throws Exception {
// 鼠标放在id和姓名的文本框上显示的信息
Tooltip tooltip=new Tooltip("不可修改");
u1.setTooltip(tooltip);
u2.setTooltip(tooltip);
u1.setText(student.getId());
u2.setText(student.getName());
// 学生id和姓名不可修改
u1.setEditable(false);
u2.setEditable(false);
u3.setText(student.getCollege());
u4.setText(student.getClas());
u5.setText(String.valueOf(student.getChinese()));
u6.setText(String.valueOf(student.getMath()));
u7.setText(String.valueOf(student.getEnglish()));
u8.setText(String.valueOf(student.getPhysics()));
u9.setText(String.valueOf(student.getElectronic()));
u10.setText(String.valueOf(student.getJava()));
u11.setText(String.valueOf(student.getPe()));
u12.setText(String.valueOf(student.getPolitics()));
}
// 将被选中的学生信息传递过来
public static void setSelectStudent(Student selectStudent){
student=selectStudent;
}
@FXML
// 返回老师管理学生页面
void back(ActionEvent event) throws Exception{
backPane();
}
@FXML
// 修改学生信息提交
void submit(ActionEvent event) throws Exception{
// 成绩只能为数字
if (juge.isDouble(u5.getText()) && juge.isDouble(u6.getText()) && juge.isDouble(u7.getText()) && juge.isDouble(u8.getText())
&& juge.isDouble(u9.getText()) && juge.isDouble(u10.getText()) && juge.isDouble(u11.getText()) && juge.isDouble(u12.getText()) ){
// 点击提交按钮后,将文本框中的学生信息获取,最后将修改过后的学生对象传递给jdbc进行数据库的修改
student.setId(u1.getText());
student.setName(u2.getText());
student.setCollege(u3.getText());
student.setClas(u4.getText());
student.setChinese(Double.valueOf(u5.getText()));
student.setMath(Double.valueOf(u6.getText()));
student.setEnglish(Double.valueOf(u7.getText())
);
student.setPhysics(Double.valueOf(u8.getText()));
student.setElectronic(Double.valueOf(u9.getText()));
student.setJava(Double.valueOf(u10.getText()));
student.setPe(Double.valueOf(u11.getText()));
student.setPolitics(Double.valueOf(u12.getText()));
jdbc.updateStudent(student);
juge.showRightMessage("修改成功!!");
}else {
juge.showErrorMessage("输入成绩格式错误!! 请重新输入");
}
}
public void backPane() throws Exception {
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("Tch_Manage_Stu.fxml"));
stage.setTitle("TeacherController Manage Student");
stage.setX(200);
stage.setScene(new Scene(root, 1000, 600));
stage.show();
}
// 添加提交成功的弹窗
public void isSubmit(){
}
}
查询学生页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.FindedStudentControlelr">
<children>
<TableView fx:id="studentInfomation" layoutY="119.0" prefHeight="491.0" prefWidth="1000.0">
<columns>
<TableColumn fx:id="idColumn" prefWidth="61.0" text="学号" />
<TableColumn fx:id="collegeColumn" prefWidth="153.0" text="学院" />
<TableColumn fx:id="nameColumn" prefWidth="62.0" text="姓名" />
<TableColumn fx:id="classColumn" prefWidth="75.0" text="班级" />
<TableColumn fx:id="chineseColumn" prefWidth="75.0" text="大学语文" />
<TableColumn fx:id="mathColumn" prefWidth="75.0" text="高等数学" />
<TableColumn fx:id="englishColumn" prefWidth="75.0" text="大学英语" />
<TableColumn fx:id="physicsColumn" prefWidth="75.0" text="大学物理" />
<TableColumn fx:id="electronicColumn" prefWidth="75.0" text="电子技术" />
<TableColumn fx:id="javaColumn" prefWidth="97.0" text="Java程序设计" />
<TableColumn fx:id="peColumn" prefWidth="74.0" text="大学体育" />
<TableColumn fx:id="politicsColumn" prefWidth="74.0" text="思想政治" />
</columns>
</TableView>
<Label layoutX="410.0" layoutY="41.0" prefHeight="33.0" prefWidth="121.0" text="查询结果">
<font>
<Font size="24.0" />
</font>
</Label>
<Button fx:id="backButton" layoutX="55.0" layoutY="58.0" mnemonicParsing="false" onAction="#back" text="返回" />
</children>
</AnchorPane>
controller
package controller;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import pojo.Student;
import java.util.ArrayList;
import java.util.List;
public class FindedStudentControlelr {
@FXML
private Button backButton;
@FXML
private TableColumn<Student, String> idColumn;
@FXML
private TableColumn<Student, String> classColumn;
@FXML
private TableColumn<Student, String>nameColumn;
@FXML
private TableColumn<Student, String> collegeColumn;
@FXML
private TableColumn<Student, Double> chineseColumn;
@FXML
private TableColumn<Student,Double> mathColumn;
@FXML
private TableColumn<Student, Double> englishColumn;
@FXML
private TableColumn<Student,Double> physicsColumn;
@FXML
private TableColumn<Student, Double> electronicColumn;
@FXML
private TableColumn<Student, Double> javaColumn;
@FXML
private TableColumn<Student,Double> peColumn;
@FXML
private TableColumn<Student,Double> politicsColumn;
@FXML
private TableView<Student> studentInfomation;
static List<Student> studentList2=new ArrayList<>();
ObservableList<Student> observableList2= FXCollections.observableList(studentList2);
//初始化方法
public void initialize() throws Exception {
// 加载 查询到的 学生的数据
// 2 获取所有学生的信息
idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
classColumn.setCellValueFactory(new PropertyValueFactory<>("clas"));
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
collegeColumn.setCellValueFactory(new PropertyValueFactory<>("college"));
chineseColumn.setCellValueFactory(new PropertyValueFactory<>("chinese"));
mathColumn.setCellValueFactory(new PropertyValueFactory<>("math"));
englishColumn.setCellValueFactory(new PropertyValueFactory<>("english"));
physicsColumn.setCellValueFactory(new PropertyValueFactory<>("physics"));
electronicColumn.setCellValueFactory(new PropertyValueFactory<>("electronic"));
javaColumn.setCellValueFactory(new PropertyValueFactory<>("java"));
peColumn.setCellValueFactory(new PropertyValueFactory<>("pe"));
politicsColumn.setCellValueFactory(new PropertyValueFactory<>("politics"));
// 设置 TableView 的数据
studentInfomation.setItems(observableList2);
}
// 将查询到的结果传递过来 传递的是学生list
static void setStudentList(List<Student> studentList){
studentList2=studentList;
}
@FXML
void back(ActionEvent event) throws Exception {
backPane();
}
public void backPane() throws Exception {
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("Tch_Manage_Stu.fxml"));
stage.setTitle("Teacher Manage Student");
stage.setX(200);
stage.setScene(new Scene(root, 1000, 600));
stage.show();
}
}
管理员操作页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.ManagerController">
<children>
<Button fx:id="manageStudentButton" layoutX="207.0" layoutY="108.0" mnemonicParsing="false" onAction="#manageStudent" prefHeight="42.0" prefWidth="162.0" text="管理学生">
<font>
<Font size="24.0" />
</font>
</Button>
<Button fx:id="manageTeacherButton" layoutX="207.0" layoutY="190.0" mnemonicParsing="false" onAction="#manageTeacher" prefHeight="42.0" prefWidth="162.0" text="管理老师">
<font>
<Font size="24.0" />
</font>
</Button>
<Button fx:id="backButton" layoutX="207.0" layoutY="270.0" mnemonicParsing="false" onAction="#back" prefHeight="42.0" prefWidth="162.0" text="返回">
<font>
<Font size="24.0" />
</font>
</Button>
<Label layoutX="197.0" layoutY="14.0" prefHeight="45.0" prefWidth="182.0" text="管理员操作" textAlignment="CENTER">
<font>
<Font size="36.0" />
</font>
</Label>
</children>
</AnchorPane>
controller
package controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class ManagerController {
@FXML
private Button backButton;
@FXML
private Button manageStudentButton;
@FXML
private Button manageTeacherButton;
@FXML
void back(ActionEvent event) throws Exception{
loginPane();
}
@FXML
// 跳转管理学生界面
void manageStudent(ActionEvent event) throws Exception {
manageStudentPane();
}
@FXML
// 跳转管理老师界面
void manageTeacher(ActionEvent event) throws Exception {
manageTeacherPane();
}
// 返回登陆页面
public void loginPane() throws Exception{
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("Login.fxml"));
stage.setTitle("Login");
stage.setX(447);
stage.setY(141);
stage.setScene(new Scene(root, 600, 500));
stage.show();
}
public void manageStudentPane() throws Exception{
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("ManageStudent.fxml"));
stage.setTitle("Manage student");
stage.setX(200);
stage.setScene(new Scene(root, 1000, 600));
stage.show();
}
public void manageTeacherPane() throws Exception{
Stage stage=(Stage) backButton.getScene().getWindow();
Parent root= FXMLLoader.load(getClass().getResource("ManageTeacher.fxml"));
stage.setTitle("Manage Teacher");
stage.setX(200);
stage.setScene(new Scene(root, 1000, 600));
stage.show();
}
}
管理员添加教师页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.ManageAddTea">
<children>
<Label layoutX="172.0" layoutY="87.0" prefHeight="23.0" prefWidth="34.0" text="编号">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="172.0" layoutY="127.0" prefHeight="23.0" prefWidth="34.0" text="姓名">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="172.0" layoutY="169.0" prefHeight="23.0" prefWidth="34.0" text="学院">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="172.0" layoutY="208.0" prefHeight="23.0" prefWidth="34.0" text="性别">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="221.0" layoutY="14.0" text="添加教师">
<font>
<Font size="24.0" />
</font>
</Label>
<TextField fx:id="add1" layoutX="206.0" layoutY="86.0" />
<TextField fx:id="add2" layoutX="206.0" layoutY="126.0" />
<TextField fx:id="add3" layoutX="206.0" layoutY="168.0" />
<TextField fx:id="add4" layoutX="206.0" layoutY="207.0" />
<Button fx:id="backButton" layoutX="196.0" layoutY="295.0" mnemonicParsing="false" onAction="#back" text="返回" />
<Button fx:id="submitButton" layoutX="299.0" layoutY="295.0" mnemonicParsing="false" onAction="#submit" text="提交" />
</children>
</AnchorPane>
controller
package controller;
import Dao.jdbc;
import Dao.juge;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import pojo.Student;
import pojo.Teacher;
public class ManageAddTea {
@FXML
private TextField add1;
@FXML
private TextField add2;
@FXML
private TextField add3;
@FXML
private TextField add4;
@FXML
private Button backButton;
@FXML
private Button submitButton;
@FXML
void back(ActionEvent event) throws Exception{
backPane();
}
@FXML
void submit(ActionEvent event) throws Exception{
if (jdbc.Student_id(add1.getText())){
juge.showErrorMessage("注册失败! id重复!");
}else if(add1.getText().isEmpty() && add2.getText().isEmpty() && add3.getText().isEmpty() && add4.getText().isEmpty()){
juge.showErrorMessage("添加失败!内容不能为空!");
}
else {
Teacher teacher=new Teacher();
jdbc.MaddTeacher(teacher,add1.getText(),add2.getText(), add3.getText(),add4.getText());
juge.showRightMessage("教师添加成功!");
}
}
public void backPane() throws Exception {
Stage stage = (Stage) backButton.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource("ManageTeacher.fxml"));
stage.setTitle("Manage Teacher");
stage.setX(200);
stage.setScene(new Scene(root, 1000, 600));
stage.show();
}
}
管理员修改教师页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.ManageUpdateTea">
<children>
<Label layoutX="44.0" layoutY="106.0" prefHeight="23.0" prefWidth="34.0" text="编号">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="44.0" layoutY="146.0" prefHeight="23.0" prefWidth="34.0" text="姓名">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="44.0" layoutY="188.0" prefHeight="23.0" prefWidth="34.0" text="学院">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="44.0" layoutY="227.0" prefHeight="23.0" prefWidth="34.0" text="性别">
<font>
<Font size="14.0" />
</font>
</Label>
<TextField fx:id="u1" layoutX="78.0" layoutY="105.0" prefHeight="25.0" prefWidth="117.0" />
<TextField fx:id="u2" layoutX="78.0" layoutY="145.0" prefHeight="25.0" prefWidth="117.0" />
<TextField fx:id="u3" layoutX="78.0" layoutY="187.0" prefHeight="25.0" prefWidth="117.0" />
<TextField fx:id="u4" layoutX="78.0" layoutY="226.0" prefHeight="25.0" prefWidth="117.0" />
<Label layoutX="253.0" layoutY="35.0" text="修改教师信息">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="253.0" layoutY="106.0" prefHeight="23.0" prefWidth="58.0" text="头衔">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="253.0" layoutY="146.0" prefHeight="23.0" prefWidth="58.0" text="出生日期">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="253.0" layoutY="188.0" prefHeight="23.0" prefWidth="58.0" text="电话号码">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="253.0" layoutY="227.0" prefHeight="23.0" prefWidth="58.0" text="薪资">
<font>
<Font size="14.0" />
</font>
</Label>
<TextField fx:id="u5" layoutX="311.0" layoutY="105.0" prefHeight="25.0" prefWidth="90.0" />
<TextField fx:id="u6" layoutX="311.0" layoutY="145.0" prefHeight="25.0" prefWidth="90.0" />
<TextField fx:id="u7" layoutX="311.0" layoutY="187.0" prefHeight="25.0" prefWidth="90.0" />
<TextField fx:id="u8" layoutX="311.0" layoutY="226.0" prefHeight="25.0" prefWidth="90.0" />
<Button fx:id="backButton" layoutX="231.0" layoutY="295.0" mnemonicParsing="false" onAction="#back" text="返回" />
<Button fx:id="submitButton" layoutX="361.0" layoutY="295.0" mnemonicParsing="false" onAction="#submit" text="提交" />
<Label layoutX="438.0" layoutY="170.0" prefHeight="23.0" prefWidth="58.0" text="状态">
<font>
<Font size="14.0" />
</font>
</Label>
<TextField fx:id="u9" layoutX="496.0" layoutY="169.0" prefHeight="25.0" prefWidth="90.0" />
</children>
</AnchorPane>
controller
package controller;
import Dao.jdbc;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import pojo.Teacher;
public class ManageUpdateTea {
@FXML
private Button backButton;
@FXML
private Button submitButton;
@FXML
private TextField u1;
@FXML
private TextField u2;
@FXML
private TextField u3;
@FXML
private TextField u4;
@FXML
private TextField u5;
@FXML
private TextField u6;
@FXML
private TextField u7;
@FXML
private TextField u8;
@FXML
private TextField u9;
static Teacher teacher=new Teacher();
// 让本来的信息传递过来
public void initialize() throws Exception {
u1.setText(teacher.getId());
u2.setText(teacher.getName());
u3.setText(teacher.getCollege());
u4.setText(teacher.getSex());
u5.setText(teacher.getTitle());
u6.setText(String.valueOf(teacher.getBorn()));
u7.setText(teacher.getNumber());
u8.setText(String.valueOf(teacher.getSalary()));
u9.setText(teacher.getState());
}
public static void setSelectTeacher(Teacher selectTeacher){
teacher=selectTeacher;
}
@FXML
void back(ActionEvent event) throws Exception {
backPane();
}
@FXML
void submit(ActionEvent event) throws Exception{
teacher.setId(u1.getText());
teacher.setName(u2.getText());
teacher.setCollege(u3.getText());
teacher.setSex(u4.getText());
teacher.setTitle(u5.getText());
teacher.setBorn(u6.getText());
teacher.setNumber(u7.getText());
teacher.setSalary(Double.valueOf(u8.getText()));
teacher.setState(u9.getText());
jdbc.updateTeacher(teacher);
}
public void backPane() throws Exception {
Stage stage = (Stage) backButton.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource("ManageTeacher.fxml"));
stage.setTitle(" Manage Teacher");
stage.setX(200);
stage.setScene(new Scene(root, 1000, 600));
stage.show();
}
}
管理员查询教师页面
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/22" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.ManageFindTea">
<children>
<TableView fx:id="tableviewTeacher" layoutX="3.0" layoutY="121.0" prefHeight="488.0" prefWidth="1000.0">
<columns>
<TableColumn fx:id="id" prefWidth="91.0" text="编号" />
<TableColumn fx:id="college" prefWidth="130.0" text="学院" />
<TableColumn fx:id="name" prefWidth="85.0" text="姓名" />
<TableColumn fx:id="sex" minWidth="5.0" prefWidth="42.0" text="性别" />
<TableColumn fx:id="title" prefWidth="74.0" text="头衔" />
<TableColumn fx:id="born" prefWidth="134.0" text="出生日期" />
<TableColumn fx:id="number" prefWidth="132.0" text="电话号码" />
<TableColumn fx:id="salary" prefWidth="82.0" text="薪资" />
<TableColumn fx:id="state" prefWidth="65.0" text="状态" />
</columns>
</TableView>
<Button fx:id="backButton" layoutX="77.0" layoutY="56.0" mnemonicParsing="false" onAction="#back" text="返回" />
</children>
</AnchorPane>
controller
package controller;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import pojo.Teacher;
import java.util.ArrayList;
import java.util.List;
public class ManageFindTea {
@FXML
private Button backButton;
@FXML
private TableColumn<Teacher, String> born;
@FXML
private TableColumn<Teacher, String> college;
@FXML
private TableColumn<Teacher, String> id;
@FXML
private TableColumn<Teacher, String> name;
@FXML
private TableColumn<Teacher, String> number;
@FXML
private TableColumn<Teacher, Double> salary;
@FXML
private TableColumn<Teacher, String> sex;
@FXML
private TableColumn<Teacher, String> state;
@FXML
private TableView<Teacher> tableviewTeacher;
@FXML
private TableColumn<Teacher, String> title;
static List<Teacher> teacherList2=new ArrayList<>();
ObservableList<Teacher> observableList2= FXCollections.observableList(teacherList2);
public void initialize() throws Exception {
id.setCellValueFactory(new PropertyValueFactory<>("id"));
name.setCellValueFactory(new PropertyValueFactory<>("name"));
college.setCellValueFactory(new PropertyValueFactory<>("college"));
sex.setCellValueFactory(new PropertyValueFactory<>("sex"));
title.setCellValueFactory(new PropertyValueFactory<>("title"));
number.setCellValueFactory(new PropertyValueFactory<>("number"));
salary.setCellValueFactory(new PropertyValueFactory<>("salary"));
state.setCellValueFactory(new PropertyValueFactory<>("state"));
born.setCellValueFactory(new PropertyValueFactory<>("born"));
tableviewTeacher.setItems(observableList2);
}
@FXML
void back(ActionEvent event) throws Exception {
backPane();
}
static void setTeacherList(List<Teacher> teacherList){
teacherList2=teacherList;
}
public void backPane() throws Exception {
Stage stage = (Stage) backButton.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getResource("ManageTeacher.fxml"));
stage.setTitle(" Manage Teacher");
stage.setX(200);
stage.setScene(new Scene(root, 1000, 600));
stage.show();
}
}
Dao
jdbc
package Dao;
import javafx.scene.control.Alert;
import org.junit.Test;
import pojo.Manager;
import pojo.Student;
import pojo.Teacher;
import java.sql.*;
import java.util.List;
//ctrl+shift+上下键 可以移动一整行
//品牌数据的增删改查
public class jdbc {
static Connection conn;
// public List<Student> studentList=new ArrayList<>();
public static void Connection() throws Exception{
// 注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取连接
String url="jdbc:mysql://127.0.0.1:3306/Manage";
String urnm="root";
String pswd="000111ZHY";
conn= DriverManager.getConnection(url,urnm,pswd);
}
public static boolean isLoginStudent(String id,String password) throws Exception{
Connection();
// 检查账号密码是否为数据库里的,且判断是否正确
String sql="select *from Student where id=? && password=? ;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,id);
ps.setString(2,password);
ResultSet rs=ps.executeQuery();
// 查询到了该学生账号
if(rs.next()){
close(rs,ps,conn);
return true;
}
close(rs,ps,conn);
return false;
}
public static boolean isLoginTeacher(String id,String password) throws Exception{
Connection();
// 检查账号密码是否为数据库里的,且判断是否正确
String sql="select *from Teacher where id=? && password=? ;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,id);
ps.setString(2,password);
ResultSet rs=ps.executeQuery();
// 查询到了该老师账号
if(rs.next()){
close(rs,ps,conn);
return true;
}
close(rs,ps,conn);
return false;
}
// 关闭资源
public static void close(ResultSet rs,PreparedStatement ps,Connection conn) throws SQLException {
if (rs!=null){
rs.close();
}
if (ps!=null){
ps.close();
}
if (conn!=null){
conn.close();
}
}
// 初始化学生信息
// 在管理学生数据时用到,对学生进行增删查改
public static void StudentAllInfo(List<Student> studentList) throws Exception {
Connection();
String sql="select *from Student;";
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()){
Student student=new Student();
student.setId(rs.getString("id"));
student.setPassword(rs.getString("password"));
student.setCollege(rs.getString("college"));
student.setClas(rs.getString("clas"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setChinese(rs.getDouble("chinese"));
student.setMath(rs.getDouble("math"));
student.setEnglish(rs.getDouble("english"));
student.setPhysics(rs.getDouble("physics"));
student.setElectronic(rs.getDouble("electronic"));
student.setJava(rs.getDouble("java"));
student.setPe(rs.getDouble("pe"));
student.setPolitics(rs.getDouble("politics"));
studentList.add(student);
}
close(rs,ps,conn);
// 学生账号密码初始化成功
}
// 初始化老师信息
public static void teacherAllInfo(List<Teacher> teacherList) throws Exception {
Connection();
String sql="select *from Teacher;";
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()){
Teacher teacher=new Teacher();
teacher.setId(rs.getString("id"));
teacher.setPassword(rs.getString("password"));
teacher.setCollege(rs.getString("college"));
teacher.setName(rs.getString("name"));
teacher.setSex(rs.getString("sex"));
teacher.setTitle(rs.getString("title"));
teacher.setBorn(rs.getString("born"));
teacher.setNumber(rs.getString("number"));
teacher.setState(rs.getString("state"));
teacher.setSalary(rs.getDouble("salary"));
teacherList.add(teacher);
}
close(rs,ps,conn);
}
// 学生注册
// String id,String college,String clas,String name,String sex
public static void addStuent(Student student,String id, String password, String college,String clas,String name ) throws Exception{
Connection();
String sql="insert into Student(id,password,college,clas,name) values (?,?,?,?,?);";
PreparedStatement ps=conn.prepareStatement(sql);
// 设置参数
ps.setString(1,id);
ps.setString(2,password);
ps.setString(3,college);
ps.setString(4, clas);
ps.setString(5,name);
// ps.setString(5,sex);
// 执行sql
int count=ps.executeUpdate();//影响的行数
System.out.println(count > 0);
ps.close();
conn.close();
}
//老师添加学生信息
public static void TaddStuent(Student student,String id, String name,String college,String clas ) throws Exception{
Connection();
String sql="insert into Student(id,name,college,clas) values (?,?,?,?);";
PreparedStatement ps=conn.prepareStatement(sql);
// 设置参数
ps.setString(1,id);
ps.setString(2,name);
ps.setString(3,college);
ps.setString(4, clas);
// 执行sql
int count=ps.executeUpdate();//影响的行数
System.out.println(count > 0);
ps.close();
conn.close();
}
public static void MaddTeacher(Teacher teacher,String id, String name,String college,String sex ) throws Exception{
Connection();
String sql="insert into Teacher(id,name,college,sex) values (?,?,?,?);";
PreparedStatement ps=conn.prepareStatement(sql);
// 设置参数
ps.setString(1,id);
ps.setString(2,name);
ps.setString(3,college);
ps.setString(4, sex);
// 执行sql
int count=ps.executeUpdate();//影响的行数
System.out.println(count > 0);
ps.close();
conn.close();
}
// 查询要添加的学生id是否重复
public static boolean Student_id(String id) throws Exception{
Connection();
String sql="select *from Student where id=?;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,id);
ResultSet rs=ps.executeQuery();
if (rs.next()){
return true;
}else return false;
}
public static boolean Teacher_id(String id) throws Exception{
Connection();
String sql="select *from Teacher where id=?;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,id);
ResultSet rs=ps.executeQuery();
if (rs.next()){
return true;
}else return false;
}
// 初始化某个学生的信息
public static void Student(Student student,String id) throws Exception{
Connection();
String sql="select *from Student where id=? ;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,id);
ResultSet rs=ps.executeQuery();
if (rs.next()){
student.setId(rs.getString("id"));
student.setCollege(rs.getString("college"));
student.setClas(rs.getString("clas"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
System.out.println("查询成功");
}else {
System.out.println("查询失败");
}
}
// 初始化 老师 的信息
public static void Teacher(Teacher teacher, String id) throws Exception{
Connection();
String sql="select *from Teacher where id=? ;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,id);
ResultSet rs=ps.executeQuery();
if (rs.next()){
teacher.setId(rs.getString("id"));
teacher.setName(rs.getString("name"));
teacher.setSex(rs.getString("sex"));
teacher.setCollege(rs.getString("college"));
teacher.setBorn(String.valueOf(rs.getDate("born")));
teacher.setTitle(rs.getString("title"));
teacher.setNumber(rs.getString("number"));
teacher.setState(rs.getString("state"));
teacher.setSalary(rs.getDouble("salary"));
System.out.println("该老师 查询成功");
}else {
System.out.println("该老师 查询失败");
}
}
// 老师修改学生信息
// 不能修改学生的id和姓名
public static void updateStudent(Student student) throws Exception {
Connection();
String sql=" update Student set name=? ,college=?,clas=?, chinese=?,math=?,english=?,physics=?,electronic=?,java=?,pe=? , politics=? where id=? ;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,student.getName());
ps.setString(2,student.getCollege());
ps.setString(3,student.getClas());
ps.setString(4,String.valueOf(student.getChinese()));
ps.setString(5,String.valueOf(student.getMath()));
ps.setString(6,String.valueOf(student.getEnglish()));
ps.setString(7,String.valueOf(student.getPhysics()));
ps.setString(8,String.valueOf(student.getElectronic()));
ps.setString(9,String.valueOf(student.getJava()));
ps.setString(10,String.valueOf(student.getPe()));
ps.setString(11,String.valueOf(student.getPolitics()));
ps.setString(12,String.valueOf(student.getId()));
int count=ps.executeUpdate();
if (count>0){
System.out.println("修改成功");
}else System.out.println("修改失败");
close(null,ps,conn);
}
public static void updateTeacher(Teacher teacher) throws Exception {
Connection();
String sql=" update Teacher set name=? ,college=?,sex=?, title=?,number=?,born=?,salary=?,state=? where id=? ;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,teacher.getName());
ps.setString(2,teacher.getCollege());
ps.setString(3,teacher.getSex());
ps.setString(4,teacher.getTitle());
ps.setString(5,teacher.getNumber());
ps.setString(6, teacher.getBorn());
ps.setDouble(7,Double.valueOf(teacher.getSalary()));
ps.setString(8,teacher.getState());
ps.setString(9,teacher.getId());
int count=ps.executeUpdate();
if (count>0){
System.out.println("修改成功");
}else System.out.println("修改失败");
close(null,ps,conn);
}
// 删除学生
public static void deleteStudent(Student student) throws Exception{
Connection();
String sql="delete from Student where id=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,student.getId());
int count=ps.executeUpdate();
if (count>0){
System.out.println("删除成功");
}else System.out.println("删除失败");
close(null,ps,conn);
}
// 删除老师
public static void deleteTeacher(Teacher teacher) throws Exception{
Connection();
String sql="delete from Teacher where id=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,teacher.getId());
int count=ps.executeUpdate();
if (count>0){
System.out.println("删除成功");
}else System.out.println("删除失败");
close(null,ps,conn);
}
// 注册学生操作
@Test
public void registeStuent() throws Exception{
Connection();
String id ="8";
String college = "计算机科学与技术学院";
String clas = "计科234";
String name = "渣狗超";
String sex = "男";
}
// 获取 学生成绩 的操作
public static void getStudentScore(Student student,String id) throws Exception{
Connection();
String sql="select *from Student where id=? ;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,id);
ResultSet rs=ps.executeQuery();
while (rs.next()){
student.setChinese(rs.getDouble("chinese"));
student.setMath(rs.getDouble("math"));
student.setEnglish(rs.getDouble("english"));
student.setPhysics(rs.getDouble("physics"));
student.setElectronic(rs.getDouble("electronic"));
student.setJava(rs.getDouble("java"));
student.setPe(rs.getDouble("pe"));
student.setPolitics(rs.getDouble("politics"));
}
}
// 获取 查询 到的学生的信息
// 模糊查询
public static void findStudent(List<Student> studentList ,String s) throws Exception {
Connection();
System.out.println(s);
String sql="select *from Student where id=? || name like ? || clas like ? || college like ?;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,s);
ps.setString(2,"%"+s+"%");
ps.setString(3,"%"+s+"%");
ps.setString(4,"%"+s+"%");
ResultSet rs=ps.executeQuery();
while (rs.next()){
Student student=new Student();
student.setId(rs.getString("id"));
student.setPassword(rs.getString("password"));
student.setCollege(rs.getString("college"));
student.setClas(rs.getString("clas"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setChinese(rs.getDouble("chinese"));
student.setMath(rs.getDouble("math"));
student.setEnglish(rs.getDouble("english"));
student.setPhysics(rs.getDouble("physics"));
student.setElectronic(rs.getDouble("electronic"));
student.setJava(rs.getDouble("java"));
student.setPe(rs.getDouble("pe"));
student.setPolitics(rs.getDouble("politics"));
studentList.add(student);
}
close(rs,ps,conn);
// 学生账号密码初始化成功
}
public static void findTeacher(List<Teacher> teacherList ,String s) throws Exception {
Connection();
System.out.println(s);
String sql="select *from Teacher where id=? || name like ? || college like ?;";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,s);
ps.setString(2,"%"+s+"%");
ps.setString(3,"%"+s+"%");
ResultSet rs=ps.executeQuery();
while (rs.next()){
Teacher teacher=new Teacher();
teacher.setId(rs.getString("id"));
// teacher.setPassword(rs.getString("password"));
teacher.setCollege(rs.getString("college"));
teacher.setName(rs.getString("name"));
teacher.setSex(rs.getString("sex"));
teacher.setTitle(rs.getString("title"));
teacher.setNumber(rs.getString("number"));
teacher.setBorn(rs.getString("born"));
teacher.setSalary(rs.getDouble("salary"));
teacher.setState(rs.getString("state"));
teacherList.add(teacher);
}
close(rs,ps,conn);
// 学生账号密码初始化成功
}
//管理员登陆
//先判断账号密码是否正确
public static boolean isloginManager(String id , String password) throws Exception{
Connection();
String sql="select *from Manager where id=? and password=?";
PreparedStatement ps =conn.prepareStatement(sql);
ps.setString(1,id);
ps.setString(2,password);
ResultSet rs=ps.executeQuery();
if (rs.next()){
return true;
}else{
return false;
}
}
}
juge
package Dao;
import javafx.scene.control.Alert;
public class juge {
// 成绩只能为数字
public static boolean isDouble(Object d){
// 如果能转化为Double类型
try {
Double.parseDouble(String.valueOf(d));
return true;
} catch (NumberFormatException e) {
return false;
}
}
// 登陆时提示错误信息的弹窗
public static void showErrorMessage(String message) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
public static void showRightMessage(String message) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("正确");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
}
pojo
Manager
package pojo;
public class Manager {
private String id;
private String password;
private String name;
private String part;
public Manager() {
}
@Override
public String toString() {
return "Manager{" +
"id='" + id + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", part='" + part + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPart() {
return part;
}
public void setPart(String part) {
this.part = part;
}
}
Student
package pojo;
import java.util.Objects;
public class Student {
private String id;
private String password;
private String college;
private String clas;
private String name;
private String sex;
private double chinese;
private double math;
private double english;
private double physics;
private double electronic;
private double java;
private double pe;
private double politics;
public Student() {
super();
}
public Student(String id){
this.id=id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if (obj==null || getClass() != obj.getClass() ){
return false;
}
Student student=(Student) obj;
return Objects.equals(id,student.id);
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", password='" + password + '\'' +
", college='" + college + '\'' +
", clas='" + clas + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", chinese=" + chinese +
", math=" + math +
", english=" + english +
", physecs=" + physics +
", electronic=" + electronic +
", java=" + java +
", pe=" + pe +
", politics=" + politics +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getClas() {
return clas;
}
public void setClas(String clas) {
this.clas = clas;
}
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 Double getChinese() {
return chinese;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
public double getPhysics() {
return physics;
}
public void setPhysics(double physics) {
this.physics = physics;
}
public double getElectronic() {
return electronic;
}
public void setElectronic(double electronic) {
this.electronic = electronic;
}
public double getJava() {
return java;
}
public void setJava(double java) {
this.java = java;
}
public double getPe() {
return pe;
}
public void setPe(double pe) {
this.pe = pe;
}
public double getPolitics() {
return politics;
}
public void setPolitics(double politics) {
this.politics = politics;
}
}
Teacher
package pojo;
public class Teacher {
private String id;
private String password;
private String college;
private String name;
private String sex;
private String title;
private String born;
private String number;
private String state;
private Double salary;
public Teacher() {
}
public Teacher(String id) {
this.id = id;
}
@Override
public String toString() {
return "Teacher{" +
"id='" + id + '\'' +
", password='" + password + '\'' +
", college='" + college + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", title='" + title + '\'' +
", born='" + born + '\'' +
", number='" + number + '\'' +
", state='" + state + '\'' +
", salary='" + salary + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
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 String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBorn() {
return born;
}
public void setBorn(String born) {
this.born = born;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
// FXMLLoader loader = new FXMLLoader(getClass().getResource("login.fxml"));
// Parent root = loader.load(); // 加载FXML文件并初始化控制器
// LoginController controller = loader.getController(); // 获取控制器实例
primaryStage.setTitle("test");
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 600, 500));
primaryStage.show();
System.out.println(primaryStage.getX());
System.out.println(primaryStage.getY());
}
public static void main(String[] args) {
launch(args);
}
}
启动类MainLaunch
import javafx.application.Application;
import org.junit.Test;
public class MainLaunch {
public static void main(String[] args) {
// 注意运行的是已经编译过的class文件
// Application.launch(Main.class);
Main.main(args);
}
}
以上就是学生管理系统的所有内容,其中管理员管理学生的页面与老师管理学生的页面基本一样,只是重复的代码逻辑,根据需求添加即可。