Javafx学生管理系统
时间: 2023-12-08 19:04:47 浏览: 146
以下是一个简单的JavaFX学生管理系统的实现,包括增删改查和登录注册功能:
```java
import java.sql.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class StudentManagementSystem extends Application {
private Connection conn;
private Statement stmt;
private ResultSet rs;
private TextField tfId = new TextField();
private TextField tfName = new TextField();
private TextField tfGender = new TextField();
private TextField tfScore = new TextField();
private Button btnAdd = new Button("添加");
private Button btnDelete = new Button("删除");
private Button btnUpdate = new Button("修改");
private Button btnQuery = new Button("查询");
private TextField tfUsername = new TextField();
private PasswordField pfPassword = new PasswordField();
private Button btnLogin = new Button("登录");
private Button btnRegister = new Button("注册");
@Override
public void start(Stage primaryStage) {
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "123456");
stmt = conn.createStatement();
} catch (Exception ex) {
ex.printStackTrace();
}
// 添加学生界面
GridPane paneAdd = new GridPane();
paneAdd.setPadding(new Insets(10, 10, 10, 10));
paneAdd.setHgap(5);
paneAdd.setVgap(5);
paneAdd.add(new Label("学号:"), 0, 0);
paneAdd.add(tfId, 1, 0);
paneAdd.add(new Label("姓名:"), 0, 1);
paneAdd.add(tfName, 1, 1);
paneAdd.add(new Label("性别:"), 0, 2);
paneAdd.add(tfGender, 1, 2);
paneAdd.add(new Label("成绩:"), 0, 3);
paneAdd.add(tfScore, 1, 3);
HBox hboxAdd = new HBox(10);
hboxAdd.setAlignment(Pos.CENTER);
hboxAdd.getChildren().addAll(btnAdd, btnDelete, btnUpdate, btnQuery);
VBox vboxAdd = new VBox(10);
vboxAdd.getChildren().addAll(paneAdd, hboxAdd);
// 登录界面
GridPane paneLogin = new GridPane();
paneLogin.setPadding(new Insets(10, 10, 10, 10));
paneLogin.setHgap(5);
paneLogin.setVgap(5);
paneLogin.add(new Label("用户名:"), 0, 0);
paneLogin.add(tfUsername, 1, 0);
paneLogin.add(new Label("密码:"), 0, 1);
paneLogin.add(pfPassword, 1, 1);
HBox hboxLogin = new HBox(10);
hboxLogin.setAlignment(Pos.CENTER);
hboxLogin.getChildren().addAll(btnLogin, btnRegister);
VBox vboxLogin = new VBox(10);
vboxLogin.getChildren().addAll(paneLogin, hboxLogin);
// 主界面
TabPane tabPane = new TabPane();
Tab tabAdd = new Tab("添加学生", vboxAdd);
Tab tabLogin = new Tab("登录", vboxLogin);
tabPane.getTabs().addAll(tabAdd, tabLogin);
// 添加学生按钮事件
btnAdd.setOnAction(e -> {
try {
String sql = "INSERT INTO student VALUES (" +
tfId.getText() + ", '" +
tfName.getText() + "', '" +
tfGender.getText() + "', " +
tfScore.getText() + ")";
stmt.executeUpdate(sql);
tfId.setText("");
tfName.setText("");
tfGender.setText("");
tfScore.setText("");
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("添加成功!");
alert.showAndWait();
} catch (Exception ex) {
ex.printStackTrace();
}
});
// 删除学生按钮事件
btnDelete.setOnAction(e -> {
try {
String sql = "DELETE FROM student WHERE id=" + tfId.getText();
stmt.executeUpdate(sql);
tfId.setText("");
tfName.setText("");
tfGender.setText("");
tfScore.setText("");
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("删除成功!");
alert.showAndWait();
} catch (Exception ex) {
ex.printStackTrace();
}
});
// 修改学生按钮事件
btnUpdate.setOnAction(e -> {
try {
String sql = "UPDATE student SET name='" + tfName.getText() +
"', gender='" + tfGender.getText() +
"', score=" + tfScore.getText() +
" WHERE id=" + tfId.getText();
stmt.executeUpdate(sql);
tfId.setText("");
tfName.setText("");
tfGender.setText("");
tfScore.setText("");
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("修改成功!");
alert.showAndWait();
} catch (Exception ex) {
ex.printStackTrace();
}
});
// 查询学生按钮事件
btnQuery.setOnAction(e -> {
try {
String sql = "SELECT * FROM student WHERE id=" + tfId.getText();
rs = stmt.executeQuery(sql);
if (rs.next()) {
tfName.setText(rs.getString("name"));
tfGender.setText(rs.getString("gender"));
tfScore.setText(rs.getString("score"));
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("未找到该学生!");
alert.showAndWait();
}
} catch (Exception ex) {
ex.printStackTrace();
}
});
// 登录按钮事件
btnLogin.setOnAction(e -> {
try {
String sql = "SELECT * FROM teacher WHERE name='" + tfUsername.getText() +
"' AND password='" + pfPassword.getText() + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
primaryStage.setScene(new Scene(new Label("登录成功!")));
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("用户名或密码错误!");
alert.showAndWait();
}
} catch (Exception ex) {
ex.printStackTrace();
}
});
// 注册按钮事件
btnRegister.setOnAction(e -> {
try {
String sql = "INSERT INTO teacher VALUES (NULL, '" +
tfUsername.getText() + "', '" +
pfPassword.getText() + "', '男')";
stmt.executeUpdate(sql);
tfUsername.setText("");
pfPassword.setText("");
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("注册成功!");
alert.showAndWait();
} catch (Exception ex) {
ex.printStackTrace();
}
});
// 显示主界面
Scene scene = new Scene(tabPane, 400, 300);
primaryStage.setTitle("学生管理系统");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
阅读全文