- package login;
- import afterLogin.StudentAfterLogin;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.FXMLLoader;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Button;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.TextField;
- import javafx.stage.Stage;
- import DBConnection.DBConnection;
- import tasks.Student;
- import java.sql.*;
- public class LoginController extends Student{
- /***
- Logowanie do panelu ucznia
- ***/
- Connection connection;
- /** Deklaracja buttonów, textFieldów **/
- @FXML
- private Button buttonReturn, buttonLogIn;
- @FXML public TextField studentLoginInput;
- @FXML public PasswordField studentPasswordInput;
- /** Sprawdzanie czy w bazie danych jest użytkownik o wprowadzonym loginie i haśle **/
- public boolean isLogin(String username, String password) throws SQLException {
- boolean status = false;
- PreparedStatement preparedStatement = null;
- ResultSet resultSet = null;
- try {
- connection = DBConnection.getConnection();
- preparedStatement = connection.prepareStatement("SELECT login, password, id_student FROM students WHERE login = ? AND password = ? ");
- preparedStatement.setString(1, username);
- preparedStatement.setString(2, password);
- resultSet = preparedStatement.executeQuery();
- if (resultSet.next()) {
- setUsername(username);
- setUserId(resultSet.getInt("id_student"));
- System.out.println("Zalogowano użytkownika " + getUsername() + " o ID " + getUserId());
- status = true;
- } else {
- System.out.println("Logowanie zakończone niepowodzeniem.");
- status = false;
- }
- preparedStatement.close();
- resultSet.close();
- } catch (Exception e) {
- e.printStackTrace();
- }finally {
- preparedStatement.close();
- resultSet.close();
- }
- return status;
- }
- /** Powrót do strony głównej **/
- @FXML void backToMainPage(ActionEvent event) throws Exception{
- Stage stage = (Stage) buttonReturn.getScene().getWindow();
- Parent root = FXMLLoader.load(getClass().getResource("../sample/sample.fxml"));
- Scene scene = new Scene(root, 511, 317);
- stage.setScene(scene);
- stage.show();
- }
- /** Akcja po kliknięciu buttona zapisu **/
- @FXML void handleButtonLogin(ActionEvent actionEvent) throws Exception{
- try {
- if(isLogin(studentLoginInput.getText(), studentPasswordInput.getText())){
- Stage primaryStage = (Stage) studentLoginInput.getScene().getWindow();
- FXMLLoader loader = new FXMLLoader();
- Parent root = loader.load(getClass().getResource("../afterLogin/StudentAfterLogin.fxml").openStream());
- StudentAfterLogin studentAfterLogin = (StudentAfterLogin) loader.getController();
- //afterLogin.getUsernameLabel(txtUsername.getText());
- Scene scene = new Scene(root, 600, 400);
- primaryStage.setScene(scene);
- primaryStage.show();
- }else{
- System.out.println("Niepoprawne hasło!");
- Alert alert = new Alert(Alert.AlertType.WARNING);
- alert.setTitle("Uwaga");
- alert.setHeaderText("Niepowodzenie logowania");
- alert.setContentText("Niepoprawna nazwa użytkownika lub hasło!");
- alert.showAndWait();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }