Facebook
From Chocolate Wigeon, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 187
  1. package login;
  2.  
  3. import afterLogin.StudentAfterLogin;
  4. import javafx.event.ActionEvent;
  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.PasswordField;
  12. import javafx.scene.control.TextField;
  13. import javafx.stage.Stage;
  14. import DBConnection.DBConnection;
  15. import tasks.Student;
  16.  
  17. import java.sql.*;
  18.  
  19. public class LoginController extends Student{
  20.  
  21.     /***
  22.  
  23.      Logowanie do panelu ucznia
  24.  
  25.      ***/
  26.  
  27.     Connection connection;
  28.  
  29.     /** Deklaracja buttonów, textFieldów **/
  30.     @FXML
  31.     private Button buttonReturn, buttonLogIn;
  32.  
  33.     @FXML public TextField studentLoginInput;
  34.     @FXML public PasswordField studentPasswordInput;
  35.  
  36.     /** Sprawdzanie czy w bazie danych jest użytkownik o wprowadzonym loginie i haśle **/
  37.     public boolean isLogin(String username, String password) throws SQLException {
  38.         boolean status = false;
  39.         PreparedStatement preparedStatement = null;
  40.         ResultSet resultSet = null;
  41.         try {
  42.             connection = DBConnection.getConnection();
  43.             preparedStatement = connection.prepareStatement("SELECT login, password, id_student FROM students WHERE login = ? AND password = ? ");
  44.             preparedStatement.setString(1, username);
  45.             preparedStatement.setString(2, password);
  46.  
  47.             resultSet = preparedStatement.executeQuery();
  48.  
  49.             if (resultSet.next()) {
  50.                 setUsername(username);
  51.                 setUserId(resultSet.getInt("id_student"));
  52.                 System.out.println("Zalogowano użytkownika " + getUsername() + " o ID " + getUserId());
  53.                 status = true;
  54.             } else {
  55.                 System.out.println("Logowanie zakończone niepowodzeniem.");
  56.                 status = false;
  57.             }
  58.  
  59.             preparedStatement.close();
  60.             resultSet.close();
  61.  
  62.         } catch (Exception e) {
  63.             e.printStackTrace();
  64.         }finally {
  65.             preparedStatement.close();
  66.             resultSet.close();
  67.         }
  68.         return status;
  69.     }
  70.  
  71.     /** Powrót do strony głównej **/
  72.     @FXML void backToMainPage(ActionEvent event) throws Exception{
  73.         Stage stage = (Stage) buttonReturn.getScene().getWindow();
  74.         Parent root = FXMLLoader.load(getClass().getResource("../sample/sample.fxml"));
  75.         Scene scene = new Scene(root, 511, 317);
  76.         stage.setScene(scene);
  77.         stage.show();
  78.     }
  79.  
  80.     /** Akcja po kliknięciu buttona zapisu **/
  81.     @FXML void handleButtonLogin(ActionEvent actionEvent) throws Exception{
  82.         try {
  83.             if(isLogin(studentLoginInput.getText(), studentPasswordInput.getText())){
  84.                 Stage primaryStage = (Stage) studentLoginInput.getScene().getWindow();
  85.                 FXMLLoader loader = new FXMLLoader();
  86.                 Parent root = loader.load(getClass().getResource("../afterLogin/StudentAfterLogin.fxml").openStream());
  87.                 StudentAfterLogin studentAfterLogin = (StudentAfterLogin) loader.getController();
  88.                 //afterLogin.getUsernameLabel(txtUsername.getText());
  89.                 Scene scene = new Scene(root, 600, 400);
  90.                 primaryStage.setScene(scene);
  91.                 primaryStage.show();
  92.  
  93.             }else{
  94.                 System.out.println("Niepoprawne hasło!");
  95.                 Alert alert = new Alert(Alert.AlertType.WARNING);
  96.                 alert.setTitle("Uwaga");
  97.                 alert.setHeaderText("Niepowodzenie logowania");
  98.                 alert.setContentText("Niepoprawna nazwa użytkownika lub hasło!");
  99.                 alert.showAndWait();
  100.             }
  101.         } catch (Exception e) {
  102.             e.printStackTrace();
  103.         }
  104.     }
  105.  
  106. }
  107.