/**
   * deze methode geeft een ArrayList terug waarin alle ploegen zitten met de opgegeven categorie
   *
   * @param categorie de opgegegeven categorie
   * @return
   * @throws ApplicationException
   * @throws DBException
   */
  public ArrayList<Ploeg> zoekPloegenCategorie(Categorie categorie)
      throws ApplicationException, DBException {
    ArrayList<Ploeg> kl = new ArrayList<>();
    // connectie tot stand brengen (en automatisch sluiten)
    try (Connection conn = ConnectionManager.getConnection(); ) {
      // preparedStatement opstellen (en automtisch sluiten)
      try (PreparedStatement stmt =
          conn.prepareStatement(
              "select id, naam, niveau, trainer_id from ploeg where niveau=?"); ) {
        stmt.setString(1, categorie.getTekst());
        // execute voert elke sql-statement uit, executeQuery enkel de eenvoudige
        stmt.execute();
        // result opvragen (en automatisch sluiten)
        try (ResultSet r = stmt.getResultSet()) {
          // van alle spelers uit de database Ploeg-objecten maken

          while (r.next()) {
            Ploeg k = new Ploeg();
            k.setId(r.getInt("id"));
            k.setNaam(r.getString("naam"));
            k.setCategorie(r.getString("niveau"));
            if (r.getObject("trainer_id") == null) {
              k.setTrainer(null);
            } else {
              k.setTrainer(r.getInt("trainer_id"));
            }

            kl.add(k);
          }
          return kl;
        } catch (SQLException sqlEx) {
          throw new DBException(
              "SQL-exception in zoekPloegenCategorie(Categorie categorie) - resultset" + sqlEx);
        }
      } catch (SQLException sqlEx) {
        throw new DBException(
            "SQL-exception in zoekPloegenCategorie(Categorie categorie) - statement" + sqlEx);
      }
    } catch (SQLException sqlEx) {
      throw new DBException(
          "SQL-exception in zoekPloegenCategorie(Categorie categorie) - connection" + sqlEx);
    }
  }
  /**
   * @param id het id van de ploeg die je wilt zoeken
   * @return De te zoeken ploeg wordt geretourneerd.
   * @throws DBException
   * @throws ApplicationException
   */
  public Ploeg zoekPloeg(int id) throws DBException, ApplicationException {
    Ploeg returnPloeg = null;
    // connectie tot stand brengen (en automatisch sluiten)
    try (Connection conn = ConnectionManager.getConnection(); ) {
      // preparedStatement opstellen (en automtisch sluiten)
      try (PreparedStatement stmt =
          conn.prepareStatement("select id, naam,niveau,trainer_id from ploeg where id = ?"); ) {
        stmt.setInt(1, id);
        // execute voert het SQL-statement uit
        stmt.execute();
        // result opvragen (en automatisch sluiten)
        try (ResultSet r = stmt.getResultSet()) {
          // van de ploeg uit de database een Ploeg-object maken
          Ploeg k = new Ploeg();

          // er werd een ploeg gevonden
          if (r.next()) {
            k.setId(r.getInt("id"));
            k.setNaam(r.getString("naam"));
            k.setCategorie(r.getString("niveau"));
            if (r.getObject("trainer_id") == null) {
              k.setTrainer(null);
            } else {
              k.setTrainer(r.getInt("trainer_id"));
            }

            returnPloeg = k;
          }

          return returnPloeg;
        } catch (SQLException sqlEx) {
          throw new DBException("SQL-exception in zoekPloeg(int id) - resultset" + sqlEx);
        }
      } catch (SQLException sqlEx) {
        throw new DBException("SQL-exception in zoekPloeg(int id) - statement" + sqlEx);
      }
    } catch (SQLException sqlEx) {
      throw new DBException("SQL-exception in zoekPloeg(int id) - connection");
    }
  }
  /**
   * @return ArrayList met alle ploegen
   * @throws DBException
   * @throws ApplicationException
   */
  public ArrayList<Ploeg> zoekAllePloegen() throws DBException, ApplicationException {
    ArrayList ploegen = new ArrayList();
    // connectie tot stand brengen (en automatisch sluiten)
    try (Connection conn = ConnectionManager.getConnection(); ) {
      // preparedStatement opstellen (en automtisch sluiten)
      try (PreparedStatement stmt =
          conn.prepareStatement("select id, naam,niveau,trainer_id from ploeg"); ) {

        // execute voert het SQL-statement uit
        stmt.execute();
        // result opvragen (en automatisch sluiten)
        try (ResultSet r = stmt.getResultSet()) {
          for (int i = 0; i < ploegen.size(); i++) {
            Ploeg p = new Ploeg();
            if (r.next()) {
              p.setId(r.getInt("id"));
              p.setNaam(r.getString("naam"));
              p.setCategorie(r.getString("niveau"));
              p.setTrainer(r.getInt("trainer_id"));

              ploegen.add(p);
            }
          }

          return ploegen;

        } catch (SQLException sqlEx) {
          throw new DBException("SQL-exception in zoekAllePloegen() - resultset" + sqlEx);
        }
      } catch (SQLException sqlEx) {
        throw new DBException("SQL-exception in zoekAllePloegen() - statement" + sqlEx);
      }
    } catch (SQLException sqlEx) {
      throw new DBException("SQL-exception in zoekPloeg(String naam) - connection");
    }
  }
Пример #4
0
  public static void main(String[] args) {
    System.out.print("Geef een ploeg in: ");
    ploegske = new Ploeg(sc.nextLine());

    String naam, voornaam, straat, gemeente;
    int nummer, postcode;
    System.out.println("Geef de gegevens van de trainer in: ");
    System.out.print("Voornaam: ");
    voornaam = sc.nextLine();
    System.out.print("Naam: ");
    naam = sc.nextLine();
    System.out.print("straat");
    straat = sc.nextLine();
    System.out.print("nummer: ");
    nummer = Integer.parseInt(sc.nextLine());
    System.out.print("postcode: ");
    postcode = Integer.parseInt(sc.nextLine());
    System.out.print("gemeente: ");
    gemeente = sc.nextLine();

    Adres adreske = new Adres();
    adreske.setGemeente(gemeente);
    adreske.setNummer(nummer);
    adreske.setPostcode(postcode);
    adreske.setStraat(straat);

    trainerke = new Trainer(naam, voornaam, adreske, 0);
    ploegske.setTrainer(trainerke);

    System.out.println("Geef volleybalspeler in: ");
    String input = "";
    do {
      System.out.print("Voornaam: ");
      voornaam = sc.nextLine();
      System.out.print("Naam: ");
      naam = sc.nextLine();
      System.out.print("straat");
      straat = sc.nextLine();
      System.out.print("nummer: ");
      nummer = Integer.parseInt(sc.nextLine());
      System.out.print("postcode: ");
      postcode = Integer.parseInt(sc.nextLine());
      System.out.print("gemeente: ");
      gemeente = sc.nextLine();

      adreske = new Adres();
      adreske.setGemeente(gemeente);
      adreske.setNummer(nummer);
      adreske.setPostcode(postcode);
      adreske.setStraat(straat);

      if (ploegske.getAantalSpelers() < 12) {
        ploegske.voegVolleybalSpelerToe(new VolleybalSpeler(naam, voornaam, adreske, 0));
      }

      System.out.print("Nog ne speler toevoegen (Y/n)");
      input = sc.nextLine();
    } while (input.charAt(0) == 'Y' || input.charAt(0) == 'y');

    System.out.println("Van welke speler wilt ge het rugnummer" + "kiezen? ( naam en voornaam )");

    String naamenvoornaam = sc.nextLine();

    spelerke = ploegske.getVolleybalSpeler(naamenvoornaam);

    if (spelerke != null) {
      System.out.println(spelerke);
      System.out.print("Geef het rugnummer in: ");
      spelerke.setLidNummer(sc.nextInt());
    } else {
      System.out.println("Niet gevonden");
    }

    System.out.println("Ploegvoorstelling: ");
    System.out.println(ploegske);

    for (VolleybalSpeler s : ploegske.getSpelers()) {
      if (s != null) {
        System.out.println(s.getNaam() + " " + s.getVoornaam());
        if (s.getLidNummer() != 0) {
          System.out.println("Rugnummer: " + s.getLidNummer());
        }
      }
    }
  }