Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
128
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 Main extends Application {
  2. @Override
  3. public void start(Stage primaryStage) {
  4. try {
  5. Parent root = FXMLLoader.load(getClass().getResource("view/Login.fxml"));
  6. Scene scene = new Scene(root);
  7. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  8. primaryStage.setScene(scene);
  9. primaryStage.show();
  10. } catch(Exception e) {
  11. e.printStackTrace();
  12. }
  13. }
  14.  
  15. public static void main(String[] args) {
  16. launch(args);
  17. }
  18. }
  19.  
  20. public class LoginController implements Initializable {
  21. public LoginModel loginModel = new LoginModel();
  22.  
  23. @FXML
  24. private Label isConnected;
  25. @FXML
  26. private TextField txtUsername;
  27. @FXML
  28. private TextField txtPassword;
  29.  
  30. @Override
  31. public void initialize(URL location, ResourceBundle resources) {
  32. // TODO Auto-generated method stub
  33. if (loginModel.isDbConnected()) {
  34. isConnected.setText("Connected");
  35. } else {
  36. isConnected.setText("Not Connected");
  37. }
  38. }
  39. public void Login(ActionEvent event){
  40. try {
  41. if (loginModel.isLogin(txtUsername.getText(), txtPassword.getText())){
  42. isConnected.setText("username and password is correct");
  43. ((Node)event.getSource()).getScene().getWindow().hide();
  44. Stage primaryStage = new Stage();
  45. FXMLLoader loader = new FXMLLoader();
  46. Pane root = loader.load(getClass().getResource("view/test1.fxml").openStream());
  47. CompanyMainController companyMainController = (CompanyMainController)loader.getController();
  48. companyMainController.GetUser(txtUsername.getText());
  49. Scene scene = new Scene(root);
  50. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  51. primaryStage.setScene(scene);
  52. primaryStage.show();
  53. } else {
  54. isConnected.setText("username and password is not correct");
  55. }
  56. } catch (SQLException e) {
  57. isConnected.setText("username and password is not correct");
  58. // TODO Auto-generated catch block
  59. e.printStackTrace();
  60. } catch (IOException e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. }
  64. }
  65. }
  66.  
  67. public class CompanyMainController implements Initializable {
  68. @FXML
  69. private Label userLbl;
  70. @Override
  71. public void initialize(URL location, ResourceBundle resources) {
  72. // TODO Auto-generated method stub
  73. }
  74.  
  75. public void GetUser(String user) {
  76. // TODO Auto-generated method stub
  77. userLbl.setText(user);
  78. }
  79.  
  80. public void SignOut(ActionEvent event) {
  81. try {
  82. ((Node)event.getSource()).getScene().getWindow().hide();
  83. Stage primaryStage = new Stage();
  84. FXMLLoader loader = new FXMLLoader();
  85. Pane root = loader.load(getClass().getResource("/application/Login.fxml").openStream());
  86.  
  87. Scene scene = new Scene(root);
  88. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  89. primaryStage.setScene(scene);
  90. primaryStage.show();
  91. } catch (Exception e) {
  92. // TODO: handle exception
  93. }
  94. }
  95. }
  96.  
  97. public class LoginModel {
  98. Connection conection;
  99. public LoginModel () {
  100. conection = SqliteConnection.Connector();
  101.  
  102. //*******************************if not conected all closes************************************
  103. if (conection == null) {
  104.  
  105. System.out.println("connection not successful");
  106. System.exit(1);}
  107. }
  108.  
  109. public boolean isDbConnected() {
  110. try {
  111. return !conection.isClosed();
  112. } catch (SQLException e) {
  113. // TODO Auto-generated catch block
  114. e.printStackTrace();
  115. return false;
  116. }
  117. }
  118. public boolean isLogin(String user, String pass) throws SQLException {
  119. PreparedStatement preparedStatement = null;
  120. ResultSet resultSet = null;
  121. // soo this one is searcing in db employee section for username and password
  122. String query = "select * from employee where username = ? and password = ?";
  123. try {
  124. preparedStatement = conection.prepareStatement(query);
  125. preparedStatement.setString(1, user);
  126. preparedStatement.setString(2, pass);
  127.  
  128. resultSet = preparedStatement.executeQuery();
  129. if (resultSet.next()) {
  130. return true;
  131. }
  132. else {
  133. return false;
  134. }
  135.  
  136. } catch (Exception e) {
  137. return false;
  138. // TODO: handle exception
  139. } finally {
  140. preparedStatement.close();
  141. resultSet.close();
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement