@Override public User retrieveUser(User user) { final String SQL = "SELECT * FROM users WHERE username = ?"; PreparedStatement preparedStatement = PreparedStatementCreator.createPreparedStatement(SQL); ResultSet resultSet = null; try { preparedStatement.setString(1, user.getUsername()); resultSet = preparedStatement.executeQuery(); rowAffected = resultSet.getFetchSize(); System.out.println("Row: " + rowAffected); } catch (SQLException e) { e.printStackTrace(); } User retrievedUser = null; try { while (resultSet.next()) { retrievedUser = new User(); retrievedUser.setFirstName(resultSet.getString("first_name")); retrievedUser.setLastName(resultSet.getString("last_name")); retrievedUser.setUsername(resultSet.getString("username")); retrievedUser.setEmail(resultSet.getString("email")); retrievedUser.setPassword(resultSet.getString("password")); retrievedUser.setType(resultSet.getString("user_type")); } } catch (SQLException e) { e.printStackTrace(); } return retrievedUser; }
@Override public Integer updateUser(User oldUserInfo, User newUserInfo) { final String SQL = "UPDATE users SET email = ? WHERE email = ?"; PreparedStatement preparedStatement = PreparedStatementCreator.createPreparedStatement(SQL); try { preparedStatement.setString(1, newUserInfo.getEmail()); preparedStatement.setString(2, oldUserInfo.getEmail()); } catch (SQLException e) { e.printStackTrace(); } Integer numberOfUpdates = null; try { numberOfUpdates = preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return numberOfUpdates; }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String firstname = request.getParameter("firstname"); String lastname = request.getParameter("lastname"); String email = request.getParameter("email"); String username = request.getParameter("username"); String password1 = request.getParameter("password"); String password2 = request.getParameter("password1"); String gender = request.getParameter("gender"); String address = request.getParameter("address"); String contact = request.getParameter("contact"); String superPassword = request.getParameter("superPassword"); User user = new User(); UserService service = new UserService(user); MySQLUserDao userDao = new MySQLUserDao(); // if(!service.isUsernameAvailable()){ // response.sendRedirect("Error.jsp"); // return; // } // // else if(!service.isPasswordMatch(password1, password2)){ // response.sendRedirect("Error.jsp"); // return; // } // user.setFirstName(firstname); user.setLastName(lastname); user.setUsername(username); user.setEmail(email); user.setPassword(password1); user.setAddress(address); user.setContactNumber(contact); user.setType("admin"); int status = userDao.createUser(user); if (status > 0) { RequestDispatcher rd = request.getRequestDispatcher("AdminLogin"); rd.forward(request, response); } else { RequestDispatcher rd = request.getRequestDispatcher("login.jsp"); request.setAttribute("error", true); rd.forward(request, response); } }
@Override public Integer createUser(User user) { final String query = "INSERT INTO users (first_name, last_name, username, email, password, contact, address, user_type) values (?, ?, ?, ?, ?, ?, ?, ?)"; PreparedStatement preparedStatement = PreparedStatementCreator.createPreparedStatement(query); Integer numberOfRecordsAdded = 0; try { preparedStatement.setString(1, user.getFirstName()); preparedStatement.setString(2, user.getLastName()); preparedStatement.setString(3, user.getUsername()); preparedStatement.setString(4, user.getEmail()); preparedStatement.setString(5, user.getPassword()); preparedStatement.setString(6, user.getContactNumber()); preparedStatement.setString(7, user.getAddress()); preparedStatement.setString(8, user.getType()); numberOfRecordsAdded = preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return numberOfRecordsAdded; }