我有3個輸入字段:用戶名,密碼和類型。基於輸入字段分解錯誤消息
如果其中任何一個不正確,我只會收到1條錯誤消息,其中指出「usern &通過不正確」。
如何分解錯誤消息,以便顯示密碼,用戶名或類型的值是否輸入錯誤。
這些是我使用的2個文件:Login Model和LoginController。順便說一句,下面的代碼沒有錯誤。
它完美的作品。我只是想擴展它來分解錯誤信息。
的LoginController文件:
public class LoginController implements Initializable {
/**
* Initializes the controller class.
*/
public LoginModel loginModel = new LoginModel();
@FXML private Label isConnected;
@FXML private JFXTextField txtUsername;
@FXML private JFXPasswordField txtPassword;
@FXML private ComboBox<String> comboType;
ObservableList<String> list = FXCollections.observableArrayList("admin", "manager", "clerk");
@Override
public void initialize(URL url, ResourceBundle rb) {
comboType.setItems(list);
if(loginModel.isDbConnected()) {
isConnected.setText("Connected");
}
else {
isConnected.setText("Not Connected");
}
}
public void Login (ActionEvent event) {
try {
if(loginModel.isLogin(comboType.getValue(), txtUsername.getText(), txtPassword.getText())) {
isConnected.setText("usern & pass is correct");
//closes login fxml file
((Node)event.getSource()).getScene().getWindow().hide();
//loads main interface fxml file
Stage primaryStage = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("/gui/uicomponents/Main.fxml").openStream());
MainController mainController = (MainController)loader.getController();
mainController.getUser("Hi " + txtUsername.getText());
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("/resources/css/Consolidated.css").toExternalForm());
primaryStage.setMaximized(true);
primaryStage.setTitle("Main Interface");
primaryStage.setScene(scene);
primaryStage.show();
}
else {
isConnected.setText("usern & pass is not correct");
}
} catch (SQLException ex) {
isConnected.setText("usern & pass is not correct");
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
LoginModel文件:
public class LoginModel {
Connection connection;
public LoginModel() {
connection = SqliteConnection.Connector();
if(connection == null) {
System.out.println("Connection to DB Failed");
System.exit(1);
}
}
public boolean isDbConnected() {
try {
return !connection.isClosed();
} catch (SQLException ex) {
Logger.getLogger(LoginModel.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
public boolean isLogin(String type, String user, String pass) throws SQLException {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String query = "select * from users where type = ? and username = ? and password = ?";
try {
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, type);
preparedStatement.setString(2, user);
preparedStatement.setString(3, pass);
resultSet = preparedStatement.executeQuery();
//if returns result from db
if(resultSet.next()) {
return true;
}
else {
return false;
}
}
catch(Exception e) {
System.out.println(e);
return false;
}
finally {
preparedStatement.close();
resultSet.close();
}
}
}
首先感謝您的時間和答覆。另外,當涉及到安全性時,這一點很重要。我應該如何在數據庫中存儲密碼和用戶名?我應該使用第三方軟件加密嗎? – Roger
你應該做一些研究。 「正常」的方法是使用密碼散列函數,您可以使用「salted」密碼。然後將結果存儲在數據庫中。稍後,您只需重複該過程,並在結果與數據庫條目相匹配時,密碼正確。但正如所說:這是基本的東西,你可以讀到你自己... – GhostCat