示例#1
0
  public static CreditCard searchById(int Id) {
    // Person person = null;
    Statement stmt = null;
    CreditCard creditCard = null;
    String searchQuery = "select * from creditcard where ID ='" + Id + "' ";

    try {
      // connect to DB
      currentCon = DBConnectionManager.getConnection();
      stmt = currentCon.createStatement();
      rs = stmt.executeQuery(searchQuery);
      boolean more = rs.next();

      // if user does not exist set the isValid variable to false
      if (!more) {
        System.out.println("member with the ID = " + Id + " does not exst");
      }

      // if user exists set the isValid variable to true
      else if (more) {

        int id = rs.getInt("id");
        String cardNumber = rs.getString("cardNumber");
        int expiryMonth = rs.getInt("expiryMonth");
        int expiryYear = rs.getInt("expiryYear");
        String expiryDate = rs.getString("expiryDate");
        String cardHolderName = rs.getString("cardHolderName");
        String country = rs.getString("country");
        int CVC = rs.getInt("CVC");
        String streetAddress = rs.getString("streetAddress");
        String cardType = rs.getString("cardType");

        creditCard = new CreditCard();
        creditCard.setId(id);
        creditCard.setCardNumber(cardNumber);
        creditCard.setExpiryMonth(expiryMonth);
        creditCard.setExpiryYear(expiryYear);
        creditCard.setExpiryDate(expiryDate);
        creditCard.setCountry(country);
        creditCard.setCVC(CVC);
        creditCard.setStreetAddress(streetAddress);
        creditCard.setCardType(cardType);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (rs != null) {
        try {
          rs.close();
        } catch (Exception e) {
        }
        rs = null;
      }

      if (stmt != null) {
        try {
          stmt.close();
        } catch (Exception e) {
        }
        stmt = null;
      }

      if (currentCon != null) {
        try {
          currentCon.close();
        } catch (Exception e) {
        }

        currentCon = null;
      }
    }

    return creditCard;
  }