此代碼用於在單擊按鈕後顯示錯誤。 buttonclick
所做的是檢查用戶名和密碼是否與數據庫中的用戶名和密碼一致。如果在JavaFX中沒有找到任何結果集,則顯示警報
如果屬實,它會顯示下一個場景。如果錯誤,我想表明我犯的錯誤。顯示下一個場景工作正常,顯示錯誤沒有。
這是我的代碼:
public Pane createLogin() throws SQLException {
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.setPadding(new Insets(20, 150, 10, 10));
Button login = new Button("Login!");
TextField usernameField = new TextField();
usernameField.setPromptText("Username");
PasswordField passwordField = new PasswordField();
passwordField.setPromptText("Password");
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("ERROR");
alert.setHeaderText("OH SNAP");
alert.setContentText("I have a great message for you!");
login.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
String username = usernameField.getText();
String password = passwordField.getText();
String query = "Select * from user WHERE username ='" + username + "' and password ='" + password + "'";
ResultSet rs = null;
System.out.println("hahah:" + username);
try {
rs = dataBase.executeQuery(query);
dataBase.executeDataRowQuery(query);
while (rs.next()) {
if (username.equals(rs.getString("username"))) {
if (password.equals(rs.getString("password"))) {
switchScreen(createOverview());
} else{
alert.showAndWait();
}
} else{
alert.showAndWait();
}
alert.showAndWait();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
gridPane.add(new Label("Username: "), 0, 0);
gridPane.add(usernameField, 0, 1);
gridPane.add(new Label("Password: "), 1, 0);
gridPane.add(passwordField, 1, 1);
gridPane.add(new Label("Login!"), 2, 0);
gridPane.add(login, 2, 1);
return gridPane;
}
[Little bobby tables](http://bobby-tables.com/)會很自豪..如果沒有記錄,你不會輸入,因爲rs.next()返回false! – 2016-12-06 12:40:45