@Override public ArrayList<User> getAll(int limit, int offset) { ArrayList<User> result = new ArrayList<>(); String sql = "select * from user limit ? offset ?"; Connection con = new ConnectionPool().getConnection(); PreparedStatement ps; try { ps = con.prepareStatement(sql); ps = con.prepareStatement("select * from user limit ? offset ?"); ps.setInt(1, limit); ps.setInt(2, offset); ResultSet rs = ps.executeQuery(); while (rs.next()) { User user = new User(); user.setIdUser(rs.getString("idUser")); user.setFullName(rs.getString("fullName")); user.setEmail(rs.getString("email")); user.setUserName(rs.getString("userName")); user.setPhone(rs.getString("phone")); user.setAddress(rs.getString("address")); user.setImg(rs.getString("img")); user.setActive(rs.getInt("active")); result.add(user); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } return result; }
// these build the collections to return to the servlets, controlles private void loadUser(User user, ResultSet resultSet, int index) throws SQLException { user.setUserID(resultSet.getInt(index++)); user.setUsername(resultSet.getString(index++)); user.setPassword(resultSet.getString(index++)); user.setEmail(resultSet.getString(index++)); user.setAccountType(resultSet.getString(index++)); user.setFname(resultSet.getString(index++)); user.setLname(resultSet.getString(index++)); }
public static User createUser() { // TODO: This should work! - DONE User usr = new User(); usr.setFirstName(enterFirstName()); usr.setLastName(enterLastName()); usr.setEmail(enterEmail()); usr.setUsername(enterUsername()); usr.setPassword(enterPassword()); usr.setType(enterUserType()); return usr; }
private static User constructUserFromResultSet(ResultSet rs) { try { User user = new User(); user.setUid(Integer.parseInt(rs.getString("uid"))); user.setUserName(rs.getString("userName")); user.setPassword(rs.getString("password")); user.setEmail(rs.getString("email")); return user; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User u = new User(); UserDAO userDao = new UserDAO(); u.setUserName(request.getParameter("user")); u.setPassword(request.getParameter("password")); u.setEmail(request.getParameter("email")); userDao.AddUser(u); RequestDispatcher rd = request.getRequestDispatcher("/home.jsp"); try { rd.forward(request, response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
@Override public User getUser(String email) { String sql = "select * from user where email = ?"; Connection c = new ConnectionPool().getConnection(); User us = new User(); try { PreparedStatement sqlui = c.prepareStatement(sql); sqlui.setString(1, email); ResultSet rs = sqlui.executeQuery(); if (rs.next()) { us.setIdUser(rs.getString("idUser")); us.setFullName(rs.getString("fullName")); us.setUserName(rs.getString("userName")); us.setEmail(rs.getString("email")); us.setImg(rs.getString("img")); us.setPhone(rs.getString("phone")); us.setAddress(rs.getString("address")); } rs.close(); } catch (SQLException e) { e.printStackTrace(); } return us; }
public void attemptRegister() { // Reset errors. etEmail.setError(null); etPassword.setError(null); DaoUser daoUser = new DaoUser(getApplicationContext()); // Store values at the time of the login attempt. String login = etLogin.getText().toString(); String email = etEmail.getText().toString(); String password = etPassword.getText().toString(); User user = new User(); user.setLogin(login); user.setEmail(email); user.setPassword(password); boolean cancel = false; View focusView = null; // Check for a valid password, if the user entered one. if (!TextUtils.isEmpty(password) && !isPasswordValid(password) && password.equals(etRepPass.getText().toString())) { etPassword.setError(getString(R.string.error_invalid_password)); focusView = etPassword; cancel = true; } // Check for a valid email address. if (TextUtils.isEmpty(email)) { etEmail.setError(getString(R.string.error_field_required)); focusView = etEmail; cancel = true; } else if (!isEmailValid(email)) { etEmail.setError(getString(R.string.error_invalid_email)); focusView = etEmail; cancel = true; } try { daoUser.open(); } catch (SQLException e) { Toast.makeText(this, "Cannot open connection to database.", Toast.LENGTH_SHORT).show(); } if (cancel) { focusView.requestFocus(); } else { if (daoUser.checkUser(email)) { if (daoUser.createUser(user)) { Toast.makeText(this, "User successfull created!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Not today!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "User already exist in database.", Toast.LENGTH_SHORT).show(); } } daoUser.close(); }