Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. public static String createAccount(HttpServletRequest request) throws SQLException, ClassNotFoundException {
  2. String view = "redirect:";
  3. Random r = new Random();
  4. int Low = 100000000;
  5. int High = 999999999;
  6. int newUserID = r.nextInt(High - Low) + Low;
  7. int accountID = r.nextInt(High - Low) + Low;
  8. int chequingID = r.nextInt(High - Low) + Low;
  9. int savingsID = r.nextInt(High - Low) + Low;
  10.  
  11. PreparedStatement ps = null;
  12. PreparedStatement ps2 = null;
  13. PreparedStatement ps3 = null;
  14. PreparedStatement ps4 = null;
  15.  
  16. //Check for existing user
  17. try {
  18. Class.forName("com.mysql.jdbc.Driver");
  19. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sheridanbank?", "root", "root");
  20. ps = con.prepareStatement("select * from users;");
  21. ps2 = con.prepareStatement("select * from customer_accounts;");
  22. ps3 = con.prepareStatement("select * from chequing_acc_table;");
  23. ps4 = con.prepareStatement("select * from savings_acc_table;");
  24.  
  25. String username = request.getParameter("username");
  26. String password = request.getParameter("password");
  27. String role = request.getParameter("role");
  28. String email = request.getParameter("email");
  29. String address = request.getParameter("address");
  30. String city = request.getParameter("city");
  31. String province = request.getParameter("province");
  32. String postalCode = request.getParameter("postalCode");
  33. String phoneNumber = request.getParameter("phoneNumber");
  34.  
  35. HttpSession session = request.getSession();
  36. if (session == null || session.getAttribute("customer") == null) {
  37. return "expired"; // show "your session has expired" with "expired.jsp"
  38. } else {
  39. //Checking User table
  40. Customer customer = (Customer) session.getAttribute("customer");
  41. ResultSet rs = ps.executeQuery();
  42.  
  43. Account accountCheck = new Account();
  44. Chequing chequingCheck = new Chequing();
  45. Savings savingsCheck = new Savings();
  46. Customer customerCheck = new Customer();
  47.  
  48. while (rs.next()) {
  49.  
  50. customerCheck.setUser_id(rs.getInt("user_id"));
  51. customerCheck.setUsername(rs.getString("username"));
  52.  
  53. //Keep randoming unitl a valid number is found
  54. while (customerCheck.getUser_id() == newUserID) {
  55. newUserID = r.nextInt(High - Low) + Low;
  56. }
  57. }
  58.  
  59. ps.clearParameters();
  60. ps = con.prepareStatement("INSERT INTO users (user_id, username, password, role, email, address, city, province, postalCode, phoneNumber) VALUES (?,?,?,?,?,?,?,?,?,?)");
  61.  
  62. //Checking customer_accounts table
  63. ResultSet rs2 = ps2.executeQuery();
  64. while (rs2.next()) {
  65.  
  66. accountCheck.setAccount_id(rs2.getInt("account_id"));
  67.  
  68. //Keep randoming unitl a valid number is found
  69. while (accountCheck.getUser_id() == accountID) {
  70. accountID = r.nextInt(High - Low) + Low;
  71. }
  72. }
  73. ps2.clearParameters();
  74. ps2 = con.prepareStatement("INSERT INTO customer_accounts (account_id, user_id, cheq_id, saving_id) VALUES (?,?,?,?)");
  75.  
  76. //Checking chequing table
  77. ResultSet rs3 = ps3.executeQuery();
  78. while (rs3.next()) {
  79.  
  80. chequingCheck.setCheq_id(rs3.getInt("cheq_id"));
  81.  
  82. //Keep randoming unitl a valid number is found
  83. while (chequingCheck.getCheq_id() == chequingID) {
  84. chequingID = r.nextInt(High - Low) + Low;
  85. }
  86. }
  87.  
  88. ps3.clearParameters();
  89. ps3 = con.prepareStatement("INSERT INTO chequing_acc_table (cheq_id, account_id, user_id, balance) VALUES (?,?,?,?)");
  90.  
  91. //Checking savings table
  92. ResultSet rs4 = ps4.executeQuery();
  93. while (rs4.next()) {
  94.  
  95. savingsCheck.setSaving_id(rs4.getInt("saving_id"));
  96.  
  97. //Keep randoming unitl a valid number is found
  98. while (savingsCheck.getUser_id() == savingsID) {
  99. savingsID = r.nextInt(High - Low) + Low;
  100. }
  101. }
  102. ps4.clearParameters();
  103. ps4 = con.prepareStatement("INSERT INTO savings_acc_table (saving_id, account_id, user_id, balance) VALUES (?,?,?,?)");
  104.  
  105. if (newUserID == accountID) {
  106. }
  107. //User
  108. ps.setInt(1, newUserID);
  109. ps.setString(2, username);
  110. ps.setString(3, password);
  111. ps.setString(4, role);
  112. ps.setString(5, email);
  113. ps.setString(6, address);
  114. ps.setString(7, city);
  115. ps.setString(8, province);
  116. ps.setString(9, postalCode);
  117. ps.setString(10, phoneNumber);
  118.  
  119. //Account
  120. ps2.setInt(1, accountID);
  121. ps2.setInt(2, newUserID);
  122. ps2.setInt(3, chequingID);
  123. ps2.setInt(4, savingsID);
  124.  
  125. //Chequing
  126. ps3.setInt(1, chequingID);
  127. ps3.setInt(2, accountID);
  128. ps3.setInt(3, newUserID);
  129. ps3.setInt(4, 100);
  130.  
  131. //Savings
  132. ps4.setInt(1, savingsID);
  133. ps4.setInt(2, accountID);
  134. ps4.setInt(3, newUserID);
  135. ps4.setInt(4, 100);
  136.  
  137. ps.executeUpdate();
  138. ps2.executeUpdate();
  139. ps3.executeUpdate();
  140. ps4.executeUpdate();
  141.  
  142. }
  143.  
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. }
  147.  
  148. view = "createAccount";
  149.  
  150. return view;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement