Advertisement
Guest User

Untitled

a guest
Aug 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. package Login;
  2.  
  3. import DbConnection.ConnectionManager;
  4. import WelcomePage.WelcomeController;
  5. import javafx.fxml.FXML;
  6. import javafx.fxml.FXMLLoader;
  7. import javafx.scene.Parent;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Alert;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.control.TextField;
  12. import javafx.stage.Stage;
  13. import javafx.event.ActionEvent;
  14.  
  15. import java.sql.*;
  16.  
  17. public class LoginController {
  18. Connection connection;
  19.  
  20. public static String username;
  21. public static Integer userId;
  22.  
  23. @FXML public TextField txtUsername, txtPassword;
  24.  
  25. @FXML private Button buttonBack;
  26.  
  27. public void setUsername(String username){
  28. this.username = username;
  29. }
  30. public String getUsername(){
  31. return this.username;
  32. }
  33.  
  34. public void setUserId(Integer userId){
  35. this.userId = userId;
  36. }
  37. public Integer getUserId(){
  38. return this.userId;
  39. }
  40.  
  41. public boolean isLogin(String username, String password) throws SQLException {
  42. boolean status = false;
  43. PreparedStatement preparedStatement = null;
  44. ResultSet resultSet = null;
  45. try {
  46. connection = ConnectionManager.getConnection();
  47. preparedStatement = connection.prepareStatement("SELECT id_user, username, password FROM users WHERE username = ? AND password = ? ");
  48. preparedStatement.setString(1, username);
  49. preparedStatement.setString(2, password);
  50.  
  51. resultSet = preparedStatement.executeQuery();
  52.  
  53. if (resultSet.next()) {
  54. setUsername(username);
  55. setUserId(resultSet.getInt("id_user"));
  56. System.out.println("User authenticated successfully " + getUsername() + " ID " + getUserId());
  57. status = true;
  58. } else {
  59. System.out.println("Error ");
  60. status = false;
  61. }
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }finally {
  65. preparedStatement.close();
  66. resultSet.close();
  67. }
  68. return status;
  69. }
  70.  
  71. @FXML void backToMainPage(ActionEvent event) throws Exception{
  72. Stage stage = (Stage) buttonBack.getScene().getWindow();
  73. Parent root = FXMLLoader.load(getClass().getResource("../sample/sample.fxml"));
  74. Scene scene = new Scene(root, 1024, 768);
  75. stage.setScene(scene);
  76. stage.show();
  77. }
  78.  
  79. @FXML void handleButtonLogin(ActionEvent actionEvent) throws Exception{
  80. try {
  81. if(isLogin(txtUsername.getText(), txtPassword.getText())){
  82. Stage primaryStage = (Stage) txtUsername.getScene().getWindow();
  83. FXMLLoader loader = new FXMLLoader();
  84. Parent root = loader.load(getClass().getResource("../WelcomePage/Welcome.fxml").openStream());
  85. WelcomeController welcomeController = (WelcomeController)loader.getController();
  86. welcomeController.getUsernameLabel(txtUsername.getText());
  87. Scene scene = new Scene(root, 1024, 768);
  88. primaryStage.setScene(scene);
  89. primaryStage.show();
  90.  
  91. }else{
  92. System.out.println("Password incorrect!");
  93. Alert alert = new Alert(Alert.AlertType.WARNING);
  94. alert.setTitle("Błąd");
  95. alert.setHeaderText("Niepowodzenie logowania");
  96. alert.setContentText("Niepoprawna nazwa użytkownika lub hasło!");
  97. alert.showAndWait();
  98. }
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement