|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
@FishC
下面使我完整的代码,在调用addTeacher方法是会出现这个错误
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "javafx.beans.property.StringProperty.set(Object)" because "this.name" is null
package com.example.javafxmw;
import TeacherPostsSystem.TeacherInformation;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.*;
import java.util.Optional;
public class HelloApplication extends Application{
public TextField accountField = new TextField();
public PasswordField passwordField = new PasswordField();
ToggleGroup toggleGroup = new ToggleGroup();
RadioButton adminRadioButton = new RadioButton("管理员");
BorderPane borderPane = new BorderPane();
TableView<TeacherInformation> teacherTableView = new TableView<>();
ObservableList<TeacherInformation> teacherList = FXCollections.observableArrayList();
TextField infoTextField = new TextField();
@Override
public void start(Stage stage) {
VBox vBox = new VBox();
vBox.setSpacing(10);
// 创建账号输入框
HBox accountBox = new HBox();
Label accountLabel = new Label("账号:");
accountBox.getChildren().addAll(accountLabel, accountField);
// 创建密码输入框
HBox passwordBox = new HBox();
Label passwordLabel = new Label("密码:");
passwordBox.getChildren().addAll(passwordLabel, passwordField);
// 创建登录按钮
Button loginButton = new Button("登录");
loginButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
// 登录功能
login();
}
});
// 创建注册按钮
Button registerButton = new Button("注册");
registerButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
// 注册功能,管理员账号写死,不可注册和修改,教师账号可注册
register();
}
});
// 创建单选按钮
adminRadioButton.setToggleGroup(toggleGroup);
adminRadioButton.setSelected(true); // 默认选中管理员登录
RadioButton teacherRadioButton = new RadioButton("教师");
teacherRadioButton.setToggleGroup(toggleGroup);
// 将登录按钮和注册按钮添加到水平布局中
HBox buttonsBox = new HBox();
buttonsBox.getChildren().addAll(loginButton, registerButton);
buttonsBox.setSpacing(130);
// 将单选按钮添加到水平布局中
HBox radioButtonsBox = new HBox();
radioButtonsBox.getChildren().addAll(adminRadioButton, teacherRadioButton);
radioButtonsBox.setSpacing(100);
// 将控件添加到布局中
vBox.getChildren().addAll(accountBox, passwordBox, buttonsBox, radioButtonsBox);
BorderPane.setMargin(vBox, new Insets(180, 250, 180, 250));
// 设置vBox居中显示
borderPane.setAlignment(vBox, Pos.CENTER);
borderPane.setCenter(vBox);
Scene scene = new Scene(borderPane, 800, 640);
stage.setTitle("教师岗位管理系统");
stage.setScene(scene);
stage.show();
}
// 数据库连接
private Connection getConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/teacherportsystem";
String username = "root";
String password = "1542";
return DriverManager.getConnection(url , username , password);
}
//登录方法
public void login() {
String account = accountField.getText();
String password = passwordField.getText();
// 判断选择的身份
boolean isAdmin = toggleGroup.getSelectedToggle().equals(adminRadioButton);
String tableName = isAdmin ? "admin" : "teacher";
// 从对应的表中查询账号和密码
String sql = "SELECT * FROM " + tableName + " WHERE account = ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, account);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
String dbPassword = rs.getString("password");
// 对用户输入的密码进行加密,比对数据库中的密码是否一致
if (password.equals(dbPassword)) {
// 登录成功,弹出提示框
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("登录成功!");
alert.showAndWait();
// 展示新的界面
showMainScene();
} else {
// 密码错误,弹出提示框
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText("用户名或密码错误!");
alert.showAndWait();
}
} else {
// 账号不存在,弹出提示框
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText("用户名或密码错误!");
alert.showAndWait();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void showMainScene() {
// 创建表格
TableColumn<TeacherInformation, String> nameColumn = new TableColumn<>("姓名");
TableColumn<TeacherInformation, String> ageColumn = new TableColumn<>("年龄");
TableColumn<TeacherInformation, String> sexColumn = new TableColumn<>("性别");
TableColumn<TeacherInformation, String> wordIdColumn = new TableColumn<>("工号");
TableColumn<TeacherInformation, String> portColumn = new TableColumn<>("职称");
// 设置列宽
nameColumn.setPrefWidth(80);
ageColumn.setPrefWidth(60);
sexColumn.setPrefWidth(60);
wordIdColumn.setPrefWidth(120);
portColumn.setPrefWidth(80);
// 绑定列和属性
nameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
ageColumn.setCellValueFactory(cellData -> cellData.getValue().ageProperty());
sexColumn.setCellValueFactory(cellData -> cellData.getValue().sexProperty());
wordIdColumn.setCellValueFactory(cellData -> cellData.getValue().wordIdProperty());
portColumn.setCellValueFactory(cellData -> cellData.getValue().portProperty());
// 将列添加到表格中
teacherTableView.getColumns().addAll(nameColumn, ageColumn, sexColumn, wordIdColumn, portColumn);
// 从数据库中读取教师信息
try (Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM teacherinformation")) {
while (rs.next()) {
String name = rs.getString("name");
String age = rs.getString("age");
String sex = rs.getString("sex");
String workId = rs.getString("workid");
String port = rs.getString("port");
TeacherInformation teacher = new TeacherInformation(name, age, sex, workId, port);
teacherList.add(teacher);
}
} catch (SQLException e) {
e.printStackTrace();
}
// 将教师信息添加到表格中
teacherTableView.setItems(teacherList);
// 创建文本框显示从数据库中读取到的教师信息
infoTextField.setEditable(false);
infoTextField.setPromptText("从数据库中读取到的教师信息:");
infoTextField.setText("共读取到" + teacherList.size() + "条教师信息。");
// 创建增加的功能按钮
Button addButton = new Button("添加");
addButton.setOnAction(e -> addTeacher());
//查询
Button queryButton = new Button("查询");
queryButton.setOnAction(e -> queryTeacher());
//修改
Button modifyButton = new Button("修改");
modifyButton.setOnAction(e -> modifyTeacher());
//删除
Button deleteButton = new Button("删除");
deleteButton.setOnAction(e -> deleteTeacher());
// 将功能按钮添加到HBox中
HBox buttonBox = new HBox();
buttonBox.getChildren().addAll(addButton, queryButton, modifyButton, deleteButton);
buttonBox.setAlignment(Pos.CENTER);
buttonBox.setSpacing(10);
// 将文本框、表格和底部的功能按钮添加到VBox中
VBox vBox = new VBox();
vBox.getChildren().addAll(infoTextField, teacherTableView, buttonBox);
vBox.setAlignment(Pos.CENTER);
vBox.setSpacing(10);
Scene mainScene = new Scene(vBox, 800, 640);
Stage mainStage = new Stage();
mainStage.setTitle("教师岗位管理系统");
mainStage.setScene(mainScene);
mainStage.show();
}
private void deleteTeacher() {
}
private void modifyTeacher() {
}
private void queryTeacher() {
}
private void addTeacher() {
// 创建新增教师的Dialog
Dialog<ButtonType> dialog = new Dialog<>();
dialog.setTitle("新增教师");
dialog.setHeaderText(null);
// 设置Dialog的按钮
ButtonType confirmButtonType = new ButtonType("确认", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(confirmButtonType, ButtonType.CANCEL);
// 创建文本框和标签,用于输入教师的各项信息
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setVgap(10);
Label nameLabel = new Label("姓名:");
TextField nameField = new TextField();
nameField.setPromptText("请输入教师姓名");
gridPane.add(nameLabel, 0, 0);
gridPane.add(nameField, 1, 0);
Label ageLabel = new Label("年龄:");
TextField ageField = new TextField();
ageField.setPromptText("请输入教师年龄");
gridPane.add(ageLabel, 0, 1);
gridPane.add(ageField, 1, 1);
Label sexLabel = new Label("性别:");
TextField sexField = new TextField();
sexField.setPromptText("请输入教师性别");
gridPane.add(sexLabel, 0, 2);
gridPane.add(sexField, 1, 2);
Label wordIdLabel = new Label("工号:");
TextField wordIdField = new TextField();
wordIdField.setPromptText("请输入教师工号");
gridPane.add(wordIdLabel, 0, 3);
gridPane.add(wordIdField, 1, 3);
Label portLabel = new Label("职称:");
TextField portField = new TextField();
portField.setPromptText("请输入教师职称");
gridPane.add(portLabel, 0, 4);
gridPane.add(portField, 1, 4);
// 将GridPane添加到Dialog的主体部分
dialog.getDialogPane().setContent(gridPane);
// 显示Dialog,并等待用户输入
Optional<ButtonType> result = dialog.showAndWait();
if (result.isPresent() && result.get().equals(confirmButtonType)) {
// 获取用户输入的教师信息
TeacherInformation teacher = new TeacherInformation();
teacher.setName(nameField.getText());
teacher.setAge(String.valueOf(Integer.parseInt(ageField.getText())));
teacher.setSex(sexField.getText());
teacher.setWordId(wordIdField.getText());
teacher.setPort(portField.getText());
// 检查工号是否已存在
for (TeacherInformation item : teacherTableView.getItems()) {
if (item.getWordId().equals(teacher.getWordId())) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("警告");
alert.setHeaderText(null);
alert.setContentText("工号已存在,请重新输入!");
alert.showAndWait();
return;
}
}
// 将教师信息添加到表格中
teacherTableView.getItems().add(teacher);
// 更新文本框的内容
infoTextField.setText("共读取到" + teacherTableView.getItems().size() + "条教师信息。");
}
}
public void register() {
String account = accountField.getText();
String password = passwordField.getText();
// 检查用户名是否已存在
if (checkNameExist(account)) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText("用户名已存在!");
alert.showAndWait();
return;
}
// 注册新用户
if (createTeacherAccount(account, password)) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("提示");
alert.setHeaderText(null);
alert.setContentText("注册成功!");
alert.showAndWait();
} else {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText(null);
alert.setContentText("注册失败!");
alert.showAndWait();
}
}
private boolean createTeacherAccount(String account, String password) {
String sql = "INSERT INTO teacher (account, password) VALUES (?, ?)";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, account);
stmt.setString(2, password);
int result = stmt.executeUpdate();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
private boolean checkNameExist(String name) {
boolean result = false;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
// 执行查询语句
String sql = "SELECT account FROM teacher";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
// 判断是否有和name相同的值
while (rs.next()) {
String account = rs.getString("account");
if (account.equals(name)) {
result = true;
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
launch();
}
}
|
|