Advertisement
Guest User

Untitled

a guest
May 1st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. @FXML
  2. void submitUser(ActionEvent event) throws SQLException {
  3. if(errorChecking() == true){//puts data through error checking before doing anything with it - avoids/handles exceptions
  4. //create user object
  5. newUser user = new newUser();
  6.  
  7. //---Begin Anna 1---------------------------------------------------------------------------------------------
  8. //create string to concat the createdby and createdby ID number together
  9. String createdIDandName = "";
  10. //---End Anna 1-----------------------------------------------------------------------------------------------
  11.  
  12. //set values from the GUI
  13. user.setName(tfName.getText());
  14. user.setRole(((Labeled) roleGroup.getSelectedToggle()).getText().toString());
  15. user.setPassword(tfPass.getText());
  16. user.setCreateDate(today);
  17.  
  18. //---Begin Anna 2---------------------------------------------------------------------------------------------
  19. //concatenates the logged on user's ID and name togther and sets this for the createdBY in the database- Anna
  20. createdIDandName = loginController.loggedOnUser.getName() + ", " + loginController.loggedOnUser.getID();
  21. user.setCreatedBy(createdIDandName);
  22. //---End Anna 2-----------------------------------------------------------------------------------------------
  23.  
  24. try {
  25. //get the unique ID
  26. user.setID(createUserID(roleGroup.getSelectedToggle().getUserData().toString()));
  27. } catch (SQLException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }
  31.  
  32. //print out the user created
  33. System.out.println("User: " + user);
  34.  
  35. //send into database
  36. //create query to send appt in db
  37. String newUserQuery = "INSERT INTO `Users`(`idNum`, `password`, `role`, `FullName`, `CreatedBy`, `CreatedDate`) VALUES (?,?,?,?,?,?)";
  38. //try to connect to db
  39. try (
  40. PreparedStatement createUser = conn.prepareStatement(newUserQuery,Statement.RETURN_GENERATED_KEYS);)
  41. {
  42. //set info into the query
  43. createUser.setString(1, user.getID());
  44. createUser.setString(2, user.getPassword());
  45. createUser.setString(3, user.getRole());
  46. createUser.setString(4, user.getName());
  47. createUser.setString(5, user.getCreatedBy());
  48. createUser.setString(6, user.getCreateDate());
  49.  
  50. //print query
  51. System.out.println("Query Sent " + createUser.toString());
  52.  
  53. //pass the query in
  54. createUser.executeUpdate();
  55. }catch (SQLException e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. }//end catch
  59.  
  60. /* ================================================================================================
  61. * @author Danni
  62. * ==============================================================================================*/
  63. try{
  64. //creates alert of the information type - default: OK button and i image
  65. Alert idNum = new Alert(AlertType.INFORMATION);
  66. //needed in order to add css to the box
  67. DialogPane dialogPane = idNum.getDialogPane();
  68. //css for ID number alert box
  69.  
  70. //--Anna, made sure it works with the new style sheets
  71. dialogPane.getStylesheets().add(
  72. getClass().getResource("/application/application.css").toExternalForm());
  73. //--Anna
  74.  
  75. idNum.setTitle("Auto-Generated ID Number");
  76. //in box itself
  77. idNum.setHeaderText("ID Number: " + user.getID() + "\n" + "Password: " + user.getPassword());
  78. idNum.setContentText("New user created! " + user.getName() + " now has access to the Mentcare system.");
  79. //displays the alert box
  80. Optional<ButtonType> result = idNum.showAndWait();
  81. if(result.get() == ButtonType.OK){
  82. //clears current data so the user can create more new users without having to leave the page
  83. stage = (Stage) ((Button) event.getSource()).getScene().getWindow();
  84. root = FXMLLoader.load(getClass().getResource("/view/addUserView.fxml"));
  85. scene = new Scene(root);
  86. stage.setScene(scene);
  87. }
  88. }catch(Exception e){
  89. e.getMessage();
  90. }
  91. //=================================================================================================
  92. }else{
  93. System.out.println("Failed error checking.");
  94. }
  95. }//end method
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement