我正在使用 javaFX 创建登录系统。当用户输入正确的用户名和密码时,系统应打开一个名为“Dashbord”的新场景。 这是我的登录功能
public void validatelogin() throws SQLException, ClassNotFoundException, IOException {
String jdbcURL = "jdbc:mysql://localhost/medibase";
String username = "root";
String password = "0852";
Connection connection = DriverManager.getConnection(jdbcURL,username,password);
Class.forName("com.mysql.jdbc.Driver");
String uname = username1.getText();
String psd = password1.getText();
pst=connection.prepareStatement("SELECT * FROM user_account WHERE username=? and password=?");
pst.setString(1, uname);
pst.setString(2, psd);
rs = pst.executeQuery();
if(rs.next()){
//loginmessagelabel.setText("Congratulations");
switchtoSC1(null);
} else{
loginmessagelabel.setText("Login failed");
}
}
当用户输入正确的用户名和密码时,我调用了 switchtoSC1 方法,但这不起作用。这是方法,,
public void switchtoSC1(ActionEvent event)throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Dashboard.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene=new Scene(root);
stage.setScene(scene);
stage.show();
}
正如@James_D已经说过的,你必须传递一个非空事件来获取阶段变量,并且你还应该在同一个阶段中更改场景而不是再次创建一个新阶段,因为这不会照顾资源和会减慢应用程序的速度。
另外,查看您的登录代码,我建议您仅通过用户名获取数据库行,然后将密码(如果是密码哈希值更好,或者使用加盐哈希值更好)作为变量进行比较。这是因为结果集可能很棘手,并且您不想将身份验证基于它。
public void validateLogin(Connection conn) {
String username = username1.getText(); // you should obviously prevent username
// duplication in the signin page code
String password = password1.getText();
String queryStr = "select * from user_account where username=?";
try (PreparedStatement prepStmt = conn.prepareStatement(queryStr)) {
prepStmt.setString(1, username);
ResultSet rset = prepStmt.executeQuery();
if (!rset.next())
throw new LoginFailedException(); // the username doesn't match any database
// row
byte[] passwordHash = rset.getBytes("passwordColumn"); // get the password hash from
// the resultset
byte[] inputHash = someHashingFunction(password); // get the input hash with the same
// function used to store passwords,
// so that you get the same result
// inputting the same password
checkHashes(passwordHash, inputHash))
switchScene(); // some method or block to switch scenes
} catch (LoginFailedException e) {
// show some error scene here
}
}
public void checkHashes(byte[] passwordHash, byte[] inputHash) throw LoginFailedException {
for (int x = 0; x < inputHash.length; x++) {
if (passwordHash[x] != inputHash[x]) {
throw new LoginFailedException();
}
}
这就是我在场景之间切换的方法。 当用户输入正确的用户凭证时,场景将更改为 SalaryCalcForm.fxml
HelloApplication.java
package org.xxxxx;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
primaryStage.setTitle("Login");
primaryStage.setScene(new Scene(root, 520, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}
HelloController.java
package org.jayathilakariceproducts.jayathilakarice;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import javafx.stage.Window;
import org.jayathilakariceproducts.jayathilakarice.login.LoginDAO;
import java.io.IOException;
public class HelloController {
private Stage stage;
private Scene scene;
private Parent root;
@FXML
private Button loginButton;
@FXML
private Label loginMessageLabel;
@FXML
private TextArea userNameTextArea;
@FXML
private PasswordField passwordPasswordField;
public void switchToSalaryCalcForm(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("salaryCalcForm.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
stage.show();
}
// use for future redirections
public void switchToXXXXXXXXX(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("yourfxmlFile.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
stage.setScene(new Scene(root));
stage.show();
}
//When the login button is clicked, this method executes.
// LoginDAO does the database operations.
public void loginButtonOnAction(ActionEvent e) throws IOException {
LoginDAO loginDAO = new LoginDAO();
//validate inputs
if (userNameTextArea.getText() == null || userNameTextArea.getText().isBlank()){
loginMessageLabel.setText("User Name is Empty. Please enter the user name");
}
else if (passwordPasswordField.getText() == null || passwordPasswordField.getText().isBlank()){
loginMessageLabel.setText("Password is Empty. Please enter the password");
}
else {
boolean isValidate = loginDAO.isUserAuthorized(userNameTextArea.getText() , passwordPasswordField.getText());
if (isValidate) {
loginMessageLabel.setText("user authorized to login");
// swith the scene. pass argument 'e'
switchToSalaryCalcForm(e);
} else {
loginMessageLabel.setText("failed to login");
}
}
}
}