Пример #1
0
  /**
   * returns user base on id
   *
   * @param con DB connection
   * @param userId user id
   * @return user object
   */
  public static User getUser(Connection con, Long userId) {

    User user = null;
    try {
      PreparedStatement stmt = con.prepareStatement("select * from  users where id=?");
      stmt.setLong(1, userId);
      ResultSet rs = stmt.executeQuery();

      while (rs.next()) {
        user = new User();
        user.setId(rs.getLong("id"));
        user.setFirstNm(rs.getString(FIRST_NM));
        user.setLastNm(rs.getString(LAST_NM));
        user.setEmail(rs.getString(EMAIL));
        user.setUsername(rs.getString(USERNAME));
        user.setPassword(rs.getString("password"));
        user.setUserType(rs.getString(USER_TYPE));
        user.setSalt(rs.getString("salt"));
        user.setProfileList(UserProfileDB.getProfilesByUser(con, userId));
      }
      DBUtils.closeRs(rs);
      DBUtils.closeStmt(stmt);

    } catch (Exception e) {
      e.printStackTrace();
    }

    return user;
  }
Пример #2
0
  /**
   * returns users based on sort order defined
   *
   * @param sortedSet object that defines sort order
   * @return sorted user list
   */
  public static SortedSet getUserSet(SortedSet sortedSet) {

    ArrayList<User> userList = new ArrayList<>();

    String orderBy = "";
    if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) {
      orderBy = "order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection();
    }
    String sql = "select * from  users where enabled=true " + orderBy;

    Connection con = null;
    try {
      con = DBUtils.getConn();
      PreparedStatement stmt = con.prepareStatement(sql);
      ResultSet rs = stmt.executeQuery();

      while (rs.next()) {
        User user = new User();
        user.setId(rs.getLong("id"));
        user.setFirstNm(rs.getString(FIRST_NM));
        user.setLastNm(rs.getString(LAST_NM));
        user.setEmail(rs.getString(EMAIL));
        user.setUsername(rs.getString(USERNAME));
        user.setPassword(rs.getString("password"));
        user.setUserType(rs.getString(USER_TYPE));
        userList.add(user);
      }
      DBUtils.closeRs(rs);
      DBUtils.closeStmt(stmt);

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      DBUtils.closeConn(con);
    }

    sortedSet.setItemList(userList);
    return sortedSet;
  }