Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. package sample.controller;
  2.  
  3. import com.jfoenix.controls.JFXPasswordField;
  4. import com.jfoenix.controls.JFXTextField;
  5. import javafx.fxml.FXML;
  6. import javafx.fxml.FXMLLoader;
  7. import javafx.fxml.Initializable;
  8. import javafx.scene.Parent;
  9. import javafx.scene.Scene;
  10. import javafx.scene.input.MouseEvent;
  11. import javafx.stage.Stage;
  12. import javafx.stage.StageStyle;
  13. import sample.connection.Connector;
  14.  
  15. import java.io.IOException;
  16. import java.net.URL;
  17. import java.sql.Connection;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21. import java.util.ResourceBundle;
  22.  
  23. public class LoginController implements Initializable {
  24.     private Connection connection;
  25.     private Statement statement;
  26.     private ResultSet set;
  27.  
  28.     @FXML
  29.     private JFXTextField tfUsername;
  30.     @FXML
  31.     private JFXPasswordField tfPassword;
  32.  
  33.     @Override
  34.     public void initialize(URL location, ResourceBundle resources){
  35.  
  36.     }
  37.     @FXML
  38.     private void handleCancelButtonAction(MouseEvent event) {
  39.         System.exit(0);
  40.     }
  41.  
  42.     @FXML
  43.     private void handleLoginButtonAction(MouseEvent event) throws SQLException {
  44.  
  45.         connection = Connector.getConnection();
  46.         statement = connection.createStatement();
  47.         StringBuilder query = new StringBuilder();
  48.  
  49.  
  50.         query.append("SELECT * FROM Korisnici where username='").append(tfUsername.getText()).append("' ").append(
  51.                 "and ").append("password='").append(tfPassword.getText()).append("'");
  52.  
  53.         set = statement.executeQuery(query.toString());
  54.           loadUserScreen();
  55.           closeStage();
  56.         if(set.first()){
  57.             closeStage();
  58.             loadUserScreen();
  59.         }else {
  60.             tfPassword.setText("");
  61.             tfUsername.setText("");
  62.             tfUsername.getStyleClass().add("wrong-credentials");
  63.             tfPassword.getStyleClass().add("wrong-credentials");
  64.         }
  65.     }
  66.  
  67.  
  68.     private void closeStage() {
  69.         ((Stage) tfUsername.getScene().getWindow()).close();
  70.     }
  71.  
  72.     public void loadUserScreen()   {
  73.         try {
  74.             Parent parent = FXMLLoader.load(getClass().getResource("../View/user.fxml"));
  75.             Stage stage = new Stage(StageStyle.DECORATED);
  76.             stage.setTitle("Obracun puta");
  77.             stage.setScene(new Scene(parent));
  78.             stage.show();
  79.         } catch (IOException e) {
  80.             e.printStackTrace();
  81.         }
  82.  
  83.     }
  84.  
  85.  
  86.     void loadAdminScreen() {
  87.         try {
  88.             Parent parent = FXMLLoader.load(getClass().getResource("../View/admin.fxml"));
  89.             Stage stage = new Stage(StageStyle.DECORATED);
  90.             stage.setTitle("Obračun poslovnih puteva - Admin Deo");
  91.             stage.setScene(new Scene(parent));
  92.             stage.show();
  93.         }
  94.         catch (IOException ex) {
  95.             System.err.println("Error");
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement