コード例 #1
0
  @Override
  public boolean create(User u) {
    Connection conn = DbUtils.getConnection();
    PreparedStatement ps = null;
    final String query = PropertyContainer.get(Const.INSERT_USER);

    try {
      ps = conn.prepareStatement(query);
      int k = 1;
      ps.setString(k++, u.getEmail());
      ps.setString(k++, u.getPassword());
      ps.setString(k++, u.getFirstName());
      ps.setString(k++, u.getLastName());
      ps.setString(k++, u.getDocumentTag());
      ps.setLong(k++, u.getRole().getId());

      int count = ps.executeUpdate();
      conn.commit();
      if (count > 0) {
        // user.setId ?? rs = getGeneratedKeys...
        return true;
      }
      return false;
    } catch (SQLException e) {
      DbUtils.rollback(conn);
      LOG.error(e.getMessage());
      throw new DbException(e.getMessage(), e);
    } finally {
      DbUtils.close(conn, ps, null);
    }
  }
コード例 #2
0
  @Override
  public User getUserByEmail(String email) {
    Connection conn = DbUtils.getConnection();
    PreparedStatement ps = null;
    ResultSet rs = null;
    User user = null;
    final String query = PropertyContainer.get(Const.GET_USER_BY_EMAIL);

    try {
      ps = conn.prepareStatement(query);
      ps.setString(1, email);

      rs = ps.executeQuery();
      if (rs.next()) {
        user = extractUser(rs);
      }
    } catch (SQLException e) {
      LOG.error(e.getMessage());
      throw new DbException(e.getMessage(), e);
    } finally {
      DbUtils.close(conn, ps, rs);
    }
    return user;
  }