javafx+mysql实现学生信息管理系统
时间: 2023-06-29 12:02:25 浏览: 203
JavaFX + MySql 实现学生信息管理系统
学生信息管理系统是一个常见的实践项目,它可以帮助你学习JavaFX和MySQL的基础知识。下面是一个简单的学生信息管理系统的实现过程:
1. 创建MySQL数据库
首先,需要创建一个名为“student_info”的MySQL数据库。该数据库包含一个名为“students”的表,用于存储学生的基本信息。表结构如下:
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
age INT NOT NULL,
department VARCHAR(50) NOT NULL
);
2. 创建JavaFX窗口
使用JavaFX创建一个窗口,包含输入框和按钮,用于添加、修改和删除学生信息。代码示例:
public class StudentInfoManagement extends Application {
private Stage stage;
private TextField nameTextField;
private TextField genderTextField;
private TextField ageTextField;
private TextField departmentTextField;
@Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
stage.setTitle("学生信息管理系统");
Label nameLabel = new Label("姓名:");
nameTextField = new TextField();
HBox nameHBox = new HBox(nameLabel, nameTextField);
nameHBox.setAlignment(Pos.CENTER);
Label genderLabel = new Label("性别:");
genderTextField = new TextField();
HBox genderHBox = new HBox(genderLabel, genderTextField);
genderHBox.setAlignment(Pos.CENTER);
Label ageLabel = new Label("年龄:");
ageTextField = new TextField();
HBox ageHBox = new HBox(ageLabel, ageTextField);
ageHBox.setAlignment(Pos.CENTER);
Label departmentLabel = new Label("专业:");
departmentTextField = new TextField();
HBox departmentHBox = new HBox(departmentLabel, departmentTextField);
departmentHBox.setAlignment(Pos.CENTER);
Button addButton = new Button("添加");
addButton.setOnAction(event -> addStudent());
Button updateButton = new Button("修改");
updateButton.setOnAction(event -> updateStudent());
Button deleteButton = new Button("删除");
deleteButton.setOnAction(event -> deleteStudent());
HBox buttonHBox = new HBox(addButton, updateButton, deleteButton);
buttonHBox.setAlignment(Pos.CENTER);
buttonHBox.setSpacing(20);
VBox vBox = new VBox(nameHBox, genderHBox, ageHBox, departmentHBox, buttonHBox);
vBox.setAlignment(Pos.CENTER);
vBox.setSpacing(20);
Scene scene = new Scene(vBox, 400, 300);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
private void addStudent() {
// TODO: 添加学生信息到数据库
}
private void updateStudent() {
// TODO: 修改学生信息到数据库
}
private void deleteStudent() {
// TODO: 删除学生信息到数据库
}
}
3. 连接MySQL数据库
使用JDBC连接MySQL数据库,并实现添加、修改和删除学生信息的方法。代码示例:
private void addStudent() {
try {
String name = nameTextField.getText();
String gender = genderTextField.getText();
int age = Integer.parseInt(ageTextField.getText());
String department = departmentTextField.getText();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_info", "root", "password");
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO students (name, gender, age, department) VALUES (?, ?, ?, ?)");
preparedStatement.setString(1, name);
preparedStatement.setString(2, gender);
preparedStatement.setInt(3, age);
preparedStatement.setString(4, department);
preparedStatement.executeUpdate();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("学生信息添加成功");
alert.showAndWait();
} catch (Exception e) {
e.printStackTrace();
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText("学生信息添加失败");
alert.showAndWait();
}
}
private void updateStudent() {
try {
String name = nameTextField.getText();
String gender = genderTextField.getText();
int age = Integer.parseInt(ageTextField.getText());
String department = departmentTextField.getText();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_info", "root", "password");
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE students SET gender = ?, age = ?, department = ? WHERE name = ?");
preparedStatement.setString(1, gender);
preparedStatement.setInt(2, age);
preparedStatement.setString(3, department);
preparedStatement.setString(4, name);
int rows = preparedStatement.executeUpdate();
if (rows == 0) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText("学生信息修改失败");
alert.showAndWait();
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("学生信息修改成功");
alert.showAndWait();
}
} catch (Exception e) {
e.printStackTrace();
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText("学生信息修改失败");
alert.showAndWait();
}
}
private void deleteStudent() {
try {
String name = nameTextField.getText();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_info", "root", "password");
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM students WHERE name = ?");
preparedStatement.setString(1, name);
int rows = preparedStatement.executeUpdate();
if (rows == 0) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText("学生信息删除失败");
alert.showAndWait();
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("学生信息删除成功");
alert.showAndWait();
}
} catch (Exception e) {
e.printStackTrace();
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText("学生信息删除失败");
alert.showAndWait();
}
}
4. 查询MySQL数据库
使用JDBC从MySQL数据库中查询学生信息,并在JavaFX窗口中显示。代码示例:
public void displayStudents() {
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_info", "root", "password");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM students");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String gender = resultSet.getString("gender");
int age = resultSet.getInt("age");
String department = resultSet.getString("department");
System.out.println(id + " " + name + " " + gender + " " + age + " " + department);
}
} catch (Exception e) {
e.printStackTrace();
}
}
以上就是一个简单的JavaFX和MySQL实现学生信息管理系统的过程。
阅读全文