Guest User

Untitled

a guest
Oct 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. package controllers;
  2.  
  3. import IWS.DBConnect;
  4. import IWS.Main;
  5. import javafx.fxml.FXML;
  6. import javafx.fxml.FXMLLoader;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.control.PasswordField;
  11. import javafx.scene.control.TextField;
  12. import javafx.scene.layout.GridPane;
  13. import javafx.stage.Stage;
  14.  
  15. import java.awt.event.ActionEvent;
  16. import java.io.IOException;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19.  
  20. public class RegisterController {
  21.  
  22. @FXML
  23. private GridPane root;
  24.  
  25. @FXML
  26. private PasswordField regpassword;
  27.  
  28. @FXML
  29. private TextField regusername;
  30.  
  31. @FXML
  32. private PasswordField regconpassword;
  33.  
  34. @FXML
  35. private Button toregister;
  36.  
  37. @FXML
  38. private Button backToLogin;
  39.  
  40. @FXML
  41. private Label regstatus;
  42.  
  43. @FXML
  44. void newRegister(javafx.event.ActionEvent event) {
  45. if(regusername.getText().isEmpty()||regpassword.getText().isEmpty()||
  46. regconpassword.getText().isEmpty())
  47. {
  48. regstatus.setText("please fill all details");
  49. }
  50. else if(!regpassword.getText().equals(regconpassword.getText()))
  51. {regstatus.setText("password do not match");
  52.  
  53. }
  54.  
  55. else if(doesUserExist(regusername.getText()))
  56. {
  57. regstatus.setText("username already exists");
  58. }
  59.  
  60. else
  61. {
  62. //register user
  63.  
  64. String query = "INSERT INTO `users`( `username`, `password`) VALUES ('%s','%s')";
  65. query = String.format(query, regusername.getText(), regpassword.getText());
  66.  
  67. try
  68. {
  69. DBConnect.getStatement().executeUpdate(query);
  70. regstatus.setText("user registerd");
  71. }
  72.  
  73. catch(SQLException e)
  74. {
  75. e.printStackTrace();
  76. }
  77.  
  78. }
  79.  
  80. }
  81.  
  82. boolean doesUserExist(String username)
  83. {
  84. boolean exist=false;
  85. String query="SELECT * FROM `users` WHERE 'username'='%s'";
  86.  
  87. try
  88. {
  89. ResultSet set=
  90. DBConnect.getStatement().executeQuery(String.format(query,username));
  91. exist=set.next();
  92. set.close();
  93. }catch(SQLException e)
  94. {
  95. e.printStackTrace();
  96. }return exist;
  97.  
  98. }
  99.  
  100. @FXML
  101. void openLogin(javafx.event.ActionEvent event)throws IOException {
  102. Stage loginStage= Main.stage;
  103. loginStage.setTitle("Login");
  104. root= FXMLLoader.load(getClass().getResource("/fxml/login.fxml"));
  105. loginStage.setScene(new Scene(root));
  106. loginStage.show();
  107.  
  108. }
  109.  
  110. }
  111.  
  112. package controllers;
  113.  
  114. import IWS.DBConnect;
  115. import IWS.Main;
  116. import com.sun.org.apache.xerces.internal.util.Status;
  117. import javafx.fxml.FXML;
  118. import javafx.fxml.FXMLLoader;
  119. import javafx.scene.Parent;
  120. import javafx.scene.Scene;
  121. import javafx.scene.control.Button;
  122. import javafx.scene.control.Label;
  123. import javafx.scene.control.PasswordField;
  124. import javafx.scene.control.TextField;
  125. import javafx.scene.layout.GridPane;
  126. import javafx.stage.Stage;
  127.  
  128.  
  129. import java.io.IOException;
  130. import java.sql.ResultSet;
  131. import java.sql.SQLException;
  132.  
  133. import static javafx.fxml.FXMLLoader.load;
  134.  
  135. public class LoginController {
  136.  
  137.  
  138. @FXML
  139. private GridPane root;
  140.  
  141. @FXML
  142. private TextField username;
  143.  
  144. @FXML
  145. private PasswordField password;
  146.  
  147. @FXML
  148. private Button login;
  149.  
  150. @FXML
  151. private Button register;
  152.  
  153. @FXML
  154. private Label status;
  155.  
  156. @FXML
  157. void onLogin(javafx.event.ActionEvent event) throws IOException {
  158. String query="SELECT * FROM `users` WHERE username= 'abc' AND password = 'abc'";
  159. query=String.format(query,username.getText(),password.getText());
  160.  
  161. if(username.getText().isEmpty()||password.getText().isEmpty())
  162. {
  163. status.setText("username and password cant be empty");
  164. }
  165. else
  166. {
  167. try
  168. {
  169. ResultSet set= DBConnect.getStatement().executeQuery(query);
  170. if(set.next())
  171. {
  172. status.setText("Logged in successfully");
  173. Stage mainscreenStage= Main.stage;
  174. mainscreenStage.setTitle("Register");
  175. root=FXMLLoader.load(getClass().getResource("/fxml/mainscreen.fxml"));
  176. mainscreenStage.setScene(new Scene(root));
  177. mainscreenStage.show();
  178.  
  179. }
  180. else
  181. {
  182. status.setText("invalid username or password");
  183. }
  184. set.close();
  185. }catch (SQLException e)
  186. {
  187. e.printStackTrace();
  188. }
  189. }
  190.  
  191.  
  192.  
  193. }
  194.  
  195. @FXML
  196. void onRegister(javafx.event.ActionEvent event)throws IOException {
  197. Stage registerStage= Main.stage;
  198. registerStage.setTitle("Register");
  199. root=FXMLLoader.load(getClass().getResource("/fxml/register.fxml"));
  200. registerStage.setScene(new Scene(root));
  201. registerStage.show();
  202.  
  203.  
  204.  
  205. }
  206.  
  207. }
Add Comment
Please, Sign In to add comment