Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package projet;
  7.  
  8. import com.mysql.jdbc.Connection;
  9. import com.mysql.jdbc.Statement;
  10. import java.io.IOException;
  11. import java.net.URL;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.util.ResourceBundle;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import javafx.event.ActionEvent;
  19. import javafx.event.EventHandler;
  20. import javafx.fxml.FXML;
  21. import javafx.fxml.FXMLLoader;
  22. import javafx.fxml.Initializable;
  23. import javafx.scene.Parent;
  24. import javafx.scene.Scene;
  25. import javafx.scene.control.Button;
  26. import javafx.scene.control.PasswordField;
  27. import javafx.scene.control.TextField;
  28. import javafx.stage.Stage;
  29.  
  30. /**
  31.  * FXML Controller class
  32.  *
  33.  * @author Toubi
  34.  */
  35. public class LoginScreenController extends Projet implements Initializable  {
  36.  
  37.     /**
  38.      * Initializes the controller class.
  39.      */
  40.    
  41.     @FXML private TextField matricule;
  42.     @FXML private TextField password;
  43.     @FXML private Button btnReinitialiser;
  44.     @FXML private Button btnConnexion;
  45.    
  46.     @Override
  47.     public void initialize(URL url, ResourceBundle rb) {
  48.         // TODO
  49.         btnReinitialiser.setOnAction((ActionEvent reinit) -> {
  50.             reinitialiser();
  51.         });
  52.         btnConnexion.setOnAction((ActionEvent connexion) -> {
  53.             connexion();
  54.         });
  55.     }
  56.    
  57.     /**
  58.      *
  59.      * @return
  60.      */
  61.     public ActionEvent reinitialiser() {
  62.         matricule.setText("");
  63.         password.setText("");
  64.         return null;
  65.     }
  66.    
  67.     public ActionEvent connexion(){
  68.         String login = "root";
  69.         String pass = "toor";
  70.         String url = "jdbc:mysql://localhost:3306/pharmacie";
  71.         System.out.println(password.getText());
  72.        
  73.         try {
  74.             Class.forName("org.mariadb.jdbc.Driver");
  75.             Connection cn = (Connection) DriverManager.getConnection(url, login, pass);
  76.             Statement st = (Statement) cn.createStatement();
  77.             String sql = "select password from pharmacie where matricule = '" + matricule.getText() + "';";
  78.             ResultSet rs = st.executeQuery(sql);
  79.             if ((rs.equals(password.getText()))) {
  80.                 sceneSwitch();
  81.             } else {
  82.                 System.out.println("Failed");
  83.             }
  84.             cn.close();
  85.             st.close();
  86.             rs.close();
  87.         } catch (ClassNotFoundException ex) {
  88.             System.out.println("Driver non reconnu");
  89.         } catch (SQLException ex) {
  90.             System.out.println("Connexion non établie");
  91.         }
  92.         return null;
  93.     }
  94.    
  95.     public void sceneSwitch() {
  96.         Parent menu;
  97.         try {
  98.             menu = FXMLLoader.load(LoginScreenController.class.getResource("Menu.fxml"));
  99.             Scene sceneMenu = new Scene(menu);
  100.             primaryStage.setScene(sceneMenu);
  101.             primaryStage.show();
  102.         } catch (IOException ex) {
  103.             Logger.getLogger(LoginScreenController.class.getName()).log(Level.SEVERE, null, ex);
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement