|

楼主 |
发表于 2023-12-31 15:44:29
|
显示全部楼层
@FishC
package com.example.javafxmw;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.jdbc.MysqlDataSource;
public class HelloApplication extends Application{
public TextField accountField = new TextField();
public PasswordField passwordField = new PasswordField();
ToggleGroup toggleGroup = new ToggleGroup();
RadioButton adminRadioButton = new RadioButton("管理员");
@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));
BorderPane borderPane = new BorderPane();
borderPane.setAlignment(vBox, Pos.CENTER); // 设置vBox居中显示
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() {
}
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) {
String sql = "SELECT COUNT(*) FROM teacher WHERE account = ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
int count = rs.getInt(1);
return count > 0;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
launch();
}
}
帮我重新设计注册方法,注册需要让用户在新的对话框中填写用户名和密码,还要保证用户名唯一
|
|