/**
   * 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;
    }
  }
Example #2
0
  public ArrayList<Customer> getCustomers(String searchType, String searchBy) {
    ArrayList<Customer> customerList = new ArrayList<Customer>();
    String query = "select * from customer where &searchtype like '%&searchBy%'";
    Constants constants = new Constants();
    HashMap<String, String> map = constants.searchTypeToColumn;
    if (searchType.equals("All")) query = "select * from customer";
    else {
      query = query.replace("&searchtype", map.get(searchType));
      query = query.replace("&searchBy", searchBy);
    }
    try {
      ResultSet rs = statement.executeQuery(query);
      while (rs.next()) {
        Customer cust = new Customer();
        cust.setId(rs.getInt(1));
        cust.setFirst_name(rs.getString(2));
        cust.setLast_name(rs.getString(3));
        cust.setAddress(rs.getString(4));
        cust.setTel(rs.getString(5));
        cust.setMobile(rs.getString(6));
        cust.setDob(rs.getTimestamp(7));
        cust.setDoj(rs.getTimestamp(8));

        customerList.add(cust);
      }
    } catch (SQLException ex) {
      Logger.getLogger(CustomerDBHelper.class.getName()).log(Level.SEVERE, null, ex);
    }

    return customerList;
  }