Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Login;
- import DbConnection.ConnectionManager;
- import WelcomePage.WelcomeController;
- 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.TextField;
- import javafx.stage.Stage;
- import javafx.event.ActionEvent;
- import java.sql.*;
- public class LoginController {
- Connection connection;
- public static String username;
- public static Integer userId;
- @FXML public TextField txtUsername, txtPassword;
- @FXML private Button buttonBack;
- public void setUsername(String username){
- this.username = username;
- }
- public String getUsername(){
- return this.username;
- }
- public void setUserId(Integer userId){
- this.userId = userId;
- }
- public Integer getUserId(){
- return this.userId;
- }
- public boolean isLogin(String username, String password) throws SQLException {
- boolean status = false;
- PreparedStatement preparedStatement = null;
- ResultSet resultSet = null;
- try {
- connection = ConnectionManager.getConnection();
- preparedStatement = connection.prepareStatement("SELECT id_user, username, password FROM users WHERE username = ? AND password = ? ");
- preparedStatement.setString(1, username);
- preparedStatement.setString(2, password);
- resultSet = preparedStatement.executeQuery();
- if (resultSet.next()) {
- setUsername(username);
- setUserId(resultSet.getInt("id_user"));
- System.out.println("User authenticated successfully " + getUsername() + " ID " + getUserId());
- status = true;
- } else {
- System.out.println("Error ");
- status = false;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally {
- preparedStatement.close();
- resultSet.close();
- }
- return status;
- }
- @FXML void backToMainPage(ActionEvent event) throws Exception{
- Stage stage = (Stage) buttonBack.getScene().getWindow();
- Parent root = FXMLLoader.load(getClass().getResource("../sample/sample.fxml"));
- Scene scene = new Scene(root, 1024, 768);
- stage.setScene(scene);
- stage.show();
- }
- @FXML void handleButtonLogin(ActionEvent actionEvent) throws Exception{
- try {
- if(isLogin(txtUsername.getText(), txtPassword.getText())){
- Stage primaryStage = (Stage) txtUsername.getScene().getWindow();
- FXMLLoader loader = new FXMLLoader();
- Parent root = loader.load(getClass().getResource("../WelcomePage/Welcome.fxml").openStream());
- WelcomeController welcomeController = (WelcomeController)loader.getController();
- welcomeController.getUsernameLabel(txtUsername.getText());
- Scene scene = new Scene(root, 1024, 768);
- primaryStage.setScene(scene);
- primaryStage.show();
- }else{
- System.out.println("Password incorrect!");
- Alert alert = new Alert(Alert.AlertType.WARNING);
- alert.setTitle("Błąd");
- alert.setHeaderText("Niepowodzenie logowania");
- alert.setContentText("Niepoprawna nazwa użytkownika lub hasło!");
- alert.showAndWait();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement