Advertisement
Guest User

Untitled

a guest
Nov 26th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.Scanner;
  7.  
  8. public class Admin {
  9. public static void AdminMenu() throws Exception { //main menu for administration functions
  10. System.out.println("Admin Menu \n1 - Add Profile\n2 - Delete Profile\n3 - Update Profile\n4 - Main Menu\n5 - Exit");
  11. Scanner reader = new Scanner(System.in);
  12. System.out.println("Enter a number: ");
  13. int n = reader.nextInt();
  14.  
  15. if(n==1){
  16. Admin.AddProfile();
  17. }
  18. if(n==2){
  19. Admin.DeleteProfile();
  20. }
  21. if(n==3){
  22. UpdateProfile();
  23. }
  24. if(n==4){
  25. Main.Menu();
  26. }
  27. if(n==5){
  28. System.out.println("Application Exited.");
  29. System.exit(5);
  30. }
  31. }
  32. public static void AddProfile() throws SQLException { //creates new profile for a user
  33.  
  34. Connection con = null;
  35. PreparedStatement state = null;
  36. Scanner scanner = null;
  37.  
  38. try {
  39. scanner = new Scanner(System.in);
  40.  
  41. System.out.print("Enter the password: ");
  42. String password = scanner.nextLine();
  43.  
  44. if( !password.equals(password.toLowerCase()) && //makes sure password has a capital letter, has at least 6 characters and a symbol or number
  45. !password.equals(password.toUpperCase()) &&
  46. password.matches(".*\\d+.*") &&
  47. password.length() >= 6
  48. ){
  49. System.out.println("Success");
  50. }else{
  51. System.out.println("The password must contain a capital letter, lowercase letter, a symbol and contain at least 6 characters");
  52. Admin.AddProfile();
  53. }
  54. //user enters in relevant details
  55. System.out.print("Enter the username: ");
  56. String username = scanner.nextLine();
  57.  
  58. System.out.print("Enter the surname: ");
  59. String surname = scanner.nextLine();
  60.  
  61. System.out.print("Enter the middle name(s): ");
  62. String middleName = scanner.nextLine();
  63.  
  64. System.out.print("Enter the first name: ");
  65. String firstName = scanner.nextLine();
  66.  
  67. System.out.print("Enter the birthday (YYYY-MM-DD): ");
  68. String birthday = scanner.nextLine();
  69.  
  70. System.out.print("Enter the home phone: ");
  71. String homePhone = scanner.nextLine();
  72.  
  73. System.out.print("Enter the work phone: ");
  74. String workPhone = scanner.nextLine();
  75.  
  76. System.out.print("Enter the E-Mail: ");
  77. String eMail = scanner.nextLine();
  78.  
  79. System.out.print("Enter the Address: ");
  80. String address = scanner.nextLine();
  81.  
  82. System.out.print("Enter the Role (student/staff/tech): "); //technician being IT technician
  83. String role = scanner.nextLine();
  84.  
  85. System.out.print("Enter the any other relevant information: ");
  86. String note = scanner.nextLine();
  87.  
  88. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/admin","root","xcaliz0rz");
  89.  
  90. String sql = "insert into profiles " //inserts information into profiles database
  91. + " (username, password, surname, middleName, firstName, birthday, homePhone, workPhone, eMail, address, note, role )" + " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  92.  
  93. state = con.prepareStatement(sql);
  94.  
  95. state.setString(1, username);
  96. state.setString(2, password);
  97. state.setString(3, surname);
  98. state.setString(4, middleName);
  99. state.setString(5, firstName);
  100. state.setString(6, birthday);
  101. state.setString(7, homePhone);
  102. state.setString(8, workPhone);
  103. state.setString(9, eMail);
  104. state.setString(10, address);
  105. state.setString(11, note);
  106. state.setString(12, role);
  107.  
  108. state.executeUpdate(); //execute the function of adding the new information to the database
  109.  
  110. System.out.println("Profile details added."); //informs user that the action is complete
  111. Admin.AdminMenu(); //sends user back to the admin menu
  112. } catch (Exception exc) {
  113. exc.printStackTrace();
  114. } finally {
  115. if (state != null) {
  116. state.close();
  117. }
  118. if (con != null) {
  119. con.close();
  120. }
  121. if (scanner != null) {
  122. scanner.close();
  123. }
  124. }
  125. }
  126. public static void DeleteProfile() throws SQLException { //deletes profile from system
  127.  
  128. Connection con = null;
  129. PreparedStatement state = null;
  130. Scanner scanner = null;
  131. try {
  132. scanner = new Scanner(System.in);
  133. System.out.print("Enter the username: "); //user enters username of profile to delete
  134. String input = scanner.nextLine();
  135.  
  136. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/admin","root","xcaliz0rz");
  137.  
  138. String sql = "delete from profiles where username = ?";
  139.  
  140. state = con.prepareStatement(sql);
  141.  
  142. state.setString(1, input);
  143.  
  144. state.executeUpdate();
  145.  
  146. System.out.println("Profile deleted.");
  147. Admin.AdminMenu();
  148. } catch (Exception exc) {
  149. exc.printStackTrace();
  150. } finally {
  151. if (state != null) {
  152. state.close();
  153. }
  154. if (con != null) {
  155. con.close();
  156. }
  157. if (scanner != null) {
  158. scanner.close();
  159. }
  160.  
  161. }
  162. }
  163. public static void UpdateProfile() throws SQLException { //changes some detail to the correct current form
  164. Connection con = null;
  165. PreparedStatement state = null;
  166. Scanner scanner = null;
  167. String username;
  168. String sql = null;
  169. try {
  170. scanner = new Scanner(System.in);
  171.  
  172. System.out.println("Enter the username: ");
  173. username = scanner.nextLine();
  174. System.out.println("Enter the detail type to update: "); //allows user to choose one detail to update, as opposed to entering all of the data again
  175. String col = scanner.nextLine();
  176. System.out.println("Enter the new value: ");
  177. String value = scanner.nextLine();
  178.  
  179. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/admin","root","xcaliz0rz");
  180.  
  181. String verify = "SELECT * from profiles WHERE username = '" + username +"'"; //makes sure the username typed exists
  182. state = con.prepareStatement(verify);
  183. ResultSet res = state.executeQuery();
  184. if(res.next()){
  185. System.out.println("User selected.");
  186. }
  187. else{
  188. System.out.println("Incorrect user.");
  189. Admin.UpdateProfile();
  190. }
  191. sql = "update profiles set " + col + "= ?" +" where username = ?";
  192.  
  193. state = con.prepareStatement(sql);
  194.  
  195. state.setString(1, value);
  196. state.setString(2, username);
  197.  
  198. state.executeUpdate();
  199.  
  200. System.out.println("Record altered.");
  201. Admin.AdminMenu();
  202. } catch (Exception exc) {
  203. exc.printStackTrace();
  204. } finally {
  205. if (state != null) {
  206. state.close();
  207. }
  208. if (con != null) {
  209. con.close();
  210. }
  211. if (scanner != null) {
  212. scanner.close();
  213. }
  214.  
  215. }
  216. }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement