Beispiel #1
0
  public static ArrayList<Customer> getAllCustomers() {
    ArrayList<Customer> list = new ArrayList<Customer>();

    DBHelper db = new DBHelper();

    try {
      String queryString = "SELECT * FROM customer";
      ResultSet rs = db.executeQuery(queryString);

      while (rs.next()) {
        Customer customer = new Customer();

        customer.setId(rs.getInt("id"));
        customer.setFirstName(rs.getString("first_name"));
        customer.setLastName(rs.getString("last_name"));
        customer.setPhone(rs.getString("phone"));
        customer.setAddress(rs.getString("address"));
        customer.setZipCode(rs.getInt("zipcode"));
        customer.setCity(rs.getString("city"));
        customer.setEmail(rs.getString("email"));
        customer.setPassword(rs.getString("password"));

        list.add(customer);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    db.close();

    return list;
  }
Beispiel #2
0
  /**
   * Returns the customer data.
   *
   * @param alias the customer alias
   * @param passwordHash the customer password-hash
   * @return the customer
   * @throws IllegalStateException if the login data is invalid
   * @throws SQLException if there is a problem with the underlying JDBC connection
   */
  public Customer queryCustomer(final String alias, final byte[] passwordHash) throws SQLException {
    final Customer customer = new Customer();

    synchronized (this.connection) {
      try (PreparedStatement statement = this.connection.prepareStatement(SQL_SELECT_CUSTOMER)) {
        statement.setString(1, alias);
        statement.setBytes(2, passwordHash);

        try (ResultSet resultSet = statement.executeQuery()) {
          if (!resultSet.next()) throw new IllegalStateException("customer doesn't exist.");
          customer.setIdentity(resultSet.getLong("identity"));
          customer.setAlias(resultSet.getString("alias"));
          customer.setGivenName(resultSet.getString("givenName"));
          customer.setFamilyName(resultSet.getString("familyName"));
          customer.setStreet(resultSet.getString("street"));
          customer.setPostcode(resultSet.getString("postcode"));
          customer.setCity(resultSet.getString("city"));
          customer.setEmail(resultSet.getString("email"));
          customer.setPhone(resultSet.getString("phone"));
        }
      }
    }

    return customer;
  }
Beispiel #3
0
  public static void main(String[] args) throws Exception {
    System.out.println("hello from Customer.java");

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("customerPU");
    EntityManager manager = factory.createEntityManager();
    Query q1 = manager.createQuery("SELECT COUNT(c) FROM Customer c");
    Long count = (Long) q1.getSingleResult();
    if (count == 0) {
      // record is empty, read from data.txst
      System.out.println("record empty, read from data.txt...");
      // try {
      FileReader fr = new FileReader("data.txt");
      BufferedReader br = new BufferedReader(fr);
      String s;
      while ((s = br.readLine()) != null) {

        // System.out.println(s);
        // split the string s
        Object[] items = s.split("\\|");
        // store in string list
        // List<String> itemList= new ArrayList<String>(Arrays.asList(items));
        // string list converted to array

        // Object[] itemArray = itemList.toArray();
        // insert data into database table
        manager.getTransaction().begin();
        Customer c = new Customer();

        // add email
        c.setEmail((String) items[0]);

        // add pass
        c.setPass((String) items[1]);

        // add name
        c.setName((String) items[2]);

        // add address
        c.setAddress((String) items[3]);

        // add yob
        c.setYob((String) items[4]);

        // change to managed state
        manager.persist(c);
        manager.getTransaction().commit();
      }
      fr.close();
    }

    // display the records
    Query q2 = manager.createNamedQuery("Customer.findAll");
    List<Customer> customers = q2.getResultList();
    for (Customer c : customers) {
      System.out.println(c.getName() + ", " + c.getEmail());
    }

    manager.close();
    factory.close();
  }
  /**
   * Get a customer by id
   *
   * @param id customer id
   * @return a customer
   */
  public Customer getACustomer(int id) {
    String sql = "Select * From Customer where cid = ?";

    try {

      PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
      statement.setInt(1, id);
      ResultSet rs = statement.executeQuery();

      if (rs.next()) {
        Customer customer = new Customer();
        customer.setEmail(rs.getString("email"));
        customer.setFirstName(rs.getString("firstName"));
        customer.setLastName(rs.getString("lastName"));
        customer.setId(rs.getInt("cid"));

        if (statement != null) statement.close();

        return customer;
      } else {
        return null;
      }

    } catch (SQLException e) {
      e.printStackTrace();
      System.out.println("Error at get a customer");
      return null;
    }
  }
  public void register(ActionEvent actionEvent) {
    CustomerDAO cdao = HibernateDAOFactory.instance().getCustomerDAO();

    RequestContext context = RequestContext.getCurrentInstance();
    FacesMessage msg = null;
    boolean loggedIn = false;
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();

    if (cdao.checklemail(email)) {
      Customer cs = new Customer();
      cs.setEmail(email);
      cs.setPass(pass);
      cs.setFullName(fullName);
      cs.setAddress(address);
      cs.setPhone(phone);
      cs.setVip(vip);
      cdao.add(cs);
      loggedIn = true;
      msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Registe succes! Welcome", email);
      String url = "index.xhtml";
      try {
        ec.redirect(url);
      } catch (IOException ex) {

      }
    } else {
      loggedIn = false;
      msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Registe Error", "Invalid credentials");
    }

    FacesContext.getCurrentInstance().addMessage(null, msg);
    context.addCallbackParam("loggedIn", loggedIn);
  }
Beispiel #6
0
 public static Customer getCustomer(int id) {
   Customer customer = new Customer();
   DBHelper db = new DBHelper();
   try {
     String queryString = "SELECT * FROM customer WHERE id = " + id;
     ResultSet rs = db.executeQuery(queryString);
     if (rs != null && rs.next()) {
       customer.setId(rs.getInt("id"));
       customer.setFirstName(rs.getString("first_name"));
       customer.setLastName(rs.getString("last_name"));
       customer.setPhone(rs.getString("phone"));
       customer.setAddress(rs.getString("address"));
       customer.setZipCode(rs.getInt("zipcode"));
       customer.setCity(rs.getString("city"));
       customer.setEmail(rs.getString("email"));
       customer.setPassword(rs.getString("password"));
     }
     db.close();
   } catch (Exception e) {
     e.printStackTrace();
   }
   return customer;
 }