Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String createAccount(HttpServletRequest request) throws SQLException, ClassNotFoundException {
- String view = "redirect:";
- Random r = new Random();
- int Low = 100000000;
- int High = 999999999;
- int newUserID = r.nextInt(High - Low) + Low;
- int accountID = r.nextInt(High - Low) + Low;
- int chequingID = r.nextInt(High - Low) + Low;
- int savingsID = r.nextInt(High - Low) + Low;
- PreparedStatement ps = null;
- PreparedStatement ps2 = null;
- PreparedStatement ps3 = null;
- PreparedStatement ps4 = null;
- //Check for existing user
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sheridanbank?", "root", "root");
- ps = con.prepareStatement("select * from users;");
- ps2 = con.prepareStatement("select * from customer_accounts;");
- ps3 = con.prepareStatement("select * from chequing_acc_table;");
- ps4 = con.prepareStatement("select * from savings_acc_table;");
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- String role = request.getParameter("role");
- String email = request.getParameter("email");
- String address = request.getParameter("address");
- String city = request.getParameter("city");
- String province = request.getParameter("province");
- String postalCode = request.getParameter("postalCode");
- String phoneNumber = request.getParameter("phoneNumber");
- HttpSession session = request.getSession();
- if (session == null || session.getAttribute("customer") == null) {
- return "expired"; // show "your session has expired" with "expired.jsp"
- } else {
- //Checking User table
- Customer customer = (Customer) session.getAttribute("customer");
- ResultSet rs = ps.executeQuery();
- Account accountCheck = new Account();
- Chequing chequingCheck = new Chequing();
- Savings savingsCheck = new Savings();
- Customer customerCheck = new Customer();
- while (rs.next()) {
- customerCheck.setUser_id(rs.getInt("user_id"));
- customerCheck.setUsername(rs.getString("username"));
- //Keep randoming unitl a valid number is found
- while (customerCheck.getUser_id() == newUserID) {
- newUserID = r.nextInt(High - Low) + Low;
- }
- }
- ps.clearParameters();
- ps = con.prepareStatement("INSERT INTO users (user_id, username, password, role, email, address, city, province, postalCode, phoneNumber) VALUES (?,?,?,?,?,?,?,?,?,?)");
- //Checking customer_accounts table
- ResultSet rs2 = ps2.executeQuery();
- while (rs2.next()) {
- accountCheck.setAccount_id(rs2.getInt("account_id"));
- //Keep randoming unitl a valid number is found
- while (accountCheck.getUser_id() == accountID) {
- accountID = r.nextInt(High - Low) + Low;
- }
- }
- ps2.clearParameters();
- ps2 = con.prepareStatement("INSERT INTO customer_accounts (account_id, user_id, cheq_id, saving_id) VALUES (?,?,?,?)");
- //Checking chequing table
- ResultSet rs3 = ps3.executeQuery();
- while (rs3.next()) {
- chequingCheck.setCheq_id(rs3.getInt("cheq_id"));
- //Keep randoming unitl a valid number is found
- while (chequingCheck.getCheq_id() == chequingID) {
- chequingID = r.nextInt(High - Low) + Low;
- }
- }
- ps3.clearParameters();
- ps3 = con.prepareStatement("INSERT INTO chequing_acc_table (cheq_id, account_id, user_id, balance) VALUES (?,?,?,?)");
- //Checking savings table
- ResultSet rs4 = ps4.executeQuery();
- while (rs4.next()) {
- savingsCheck.setSaving_id(rs4.getInt("saving_id"));
- //Keep randoming unitl a valid number is found
- while (savingsCheck.getUser_id() == savingsID) {
- savingsID = r.nextInt(High - Low) + Low;
- }
- }
- ps4.clearParameters();
- ps4 = con.prepareStatement("INSERT INTO savings_acc_table (saving_id, account_id, user_id, balance) VALUES (?,?,?,?)");
- if (newUserID == accountID) {
- }
- //User
- ps.setInt(1, newUserID);
- ps.setString(2, username);
- ps.setString(3, password);
- ps.setString(4, role);
- ps.setString(5, email);
- ps.setString(6, address);
- ps.setString(7, city);
- ps.setString(8, province);
- ps.setString(9, postalCode);
- ps.setString(10, phoneNumber);
- //Account
- ps2.setInt(1, accountID);
- ps2.setInt(2, newUserID);
- ps2.setInt(3, chequingID);
- ps2.setInt(4, savingsID);
- //Chequing
- ps3.setInt(1, chequingID);
- ps3.setInt(2, accountID);
- ps3.setInt(3, newUserID);
- ps3.setInt(4, 100);
- //Savings
- ps4.setInt(1, savingsID);
- ps4.setInt(2, accountID);
- ps4.setInt(3, newUserID);
- ps4.setInt(4, 100);
- ps.executeUpdate();
- ps2.executeUpdate();
- ps3.executeUpdate();
- ps4.executeUpdate();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- view = "createAccount";
- return view;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement