Guest User

Untitled

a guest
Jun 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. public class LogareController implements Initializable {
  2.  
  3. LoginVerifier loginVerifier = new LoginVerifier();
  4.  
  5.  
  6. @FXML
  7. private TextField Numeutilzator;
  8.  
  9. @FXML
  10. private PasswordField Parola;
  11.  
  12. @FXML
  13. private Label Stare;
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. @Override
  21. public void initialize(URL location, ResourceBundle resources) {
  22.  
  23.  
  24. if (loginVerifier.Conexiune()) {
  25. Stare.setText("");
  26. } else {
  27.  
  28. Stare.setText("Conexiune nereusita!");
  29.  
  30. }
  31.  
  32. }
  33.  
  34. public void Autentificare (ActionEvent event) {
  35. try {
  36. if(loginVerifier.testaredate(Numeutilzator.getText(), Parola.getText())) {
  37. Stare.setText("Autentificare reusita !");
  38. ((Node)event.getSource()).getScene().getWindow().hide();
  39. Stage st= new Stage();
  40. FXMLLoader loader= new FXMLLoader();
  41. Pane Pane = loader.load(getClass().getResource("/LicentaApp/Meniu.fxml").openStream());
  42.  
  43. Scene scene = new Scene(Pane);
  44. scene.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
  45. st.setScene(scene);
  46. st.show();
  47.  
  48. }
  49. else {
  50. Stare.setText("Nume de utilizator sau parola incorect");
  51.  
  52. }
  53.  
  54.  
  55.  
  56.  
  57. } catch (SQLException e) {
  58.  
  59.  
  60.  
  61.  
  62.  
  63. e.printStackTrace();
  64. } catch (IOException e) {
  65.  
  66. e.printStackTrace();
  67. }
  68. }
  69. @FXML
  70. public void Inregistrare(ActionEvent event) {
  71. try {
  72. ((Node)event.getSource()).getScene().getWindow().hide();
  73. Stage PS= new Stage();
  74. FXMLLoader loader= new FXMLLoader();
  75. Pane Pane1 = loader.load(getClass().getResource("/LicentaApp/InregistrareUser.fxml").openStream());
  76. Scene scena = new Scene(Pane1);
  77. scena.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
  78. PS.setScene(scena);
  79. PS.show();
  80. } catch (Exception e) {
  81.  
  82. }
  83.  
  84.  
  85.  
  86. }
  87. // This is what I came up with, I know its bad, but I can't think of anything else, in this method I am trying to save the value of "Admin" from the table in a variable, which I can you to determen if the user has 0 or 1 at the admin, If he has 1 then a new fxml will be loaded for him, if he has 0 he is a
  88. regular user
  89.  
  90. public boolean admin(int admin) throws SQLException {
  91. ConectaredB ConectaredB=new ConectaredB();
  92. Connection conectare=ConectaredB.logareDB();
  93. PreparedStatement PSMG= null;
  94. ResultSet RSMG = null;
  95. String Interogare = "SELECT Admin FROM accounts where Admin='1'";
  96. try {
  97. PSMG = conectare.prepareStatement(Interogare);
  98. PSMG.setLong(1, admin);
  99.  
  100.  
  101. LogareController Adminstatus = new LogareController();
  102.  
  103. String Adminstatus = admin.getBytes() //IT only lets me to use getBytes(), i wanted to get the value from admin, after the query executed, this causes a confict with the primitive type "int".
  104.  
  105. } catch (Exception exceptie2) {
  106. return true;
  107.  
  108. }
  109. return false;
  110. }
  111.  
  112.  
  113. }
  114. }
  115.  
  116. public class LoginVerifier {
  117.  
  118.  
  119.  
  120. public LoginVerifier () {
  121.  
  122.  
  123. ConectaredB ConectaredB=new ConectaredB();
  124. Connection conectare=ConectaredB.logareDB();
  125.  
  126.  
  127.  
  128. if (conectare == null) {
  129.  
  130. System.out.println("Conectare nereusita!");
  131. System.exit(1);}
  132. }
  133.  
  134. public boolean Conexiune() {
  135. ConectaredB ConectaredB=new ConectaredB();
  136. Connection conectare=ConectaredB.logareDB();
  137. try {
  138. return !conectare.isClosed();
  139. } catch (SQLException e) {
  140.  
  141. e.printStackTrace();
  142. return false;
  143. }
  144.  
  145. }
  146. public boolean testaredate(String numeutil, String parola) throws SQLException {
  147. ConectaredB ConectaredB=new ConectaredB();
  148. Connection conectare=ConectaredB.logareDB();
  149. PreparedStatement PSMG= null;
  150. ResultSet RSMG = null;
  151. String Interogare = "SELECT * FROM accounts where Username=? and Password=?";
  152. try {
  153. PSMG = conectare.prepareStatement(Interogare);
  154. PSMG.setString(1, numeutil);
  155. PSMG.setString(2, parola);
  156.  
  157. RSMG = PSMG.executeQuery();
  158. if(RSMG.next()){
  159. return true;
  160. }
  161. else {
  162. return false;
  163. }
  164.  
  165. } catch (Exception exceptie2) {
  166. return false;
  167.  
  168. }
  169. }
  170. }
Add Comment
Please, Sign In to add comment