/**
   * deze methode koppelt de opgegeven trainer aan de opgegeven ploeg
   *
   * @param naam de naam van de trainer
   * @param voornaam de voornaam van de trainer
   * @param ploegnaam de ploegnaam van de ploeg
   * @throws DBException
   * @throws ApplicationException
   */
  public void toevoegenTrainerPloeg(String naam, String voornaam, String ploegnaam)
      throws DBException, ApplicationException {

    Persoon persoon = persoonDB.zoekPersoon(naam, voornaam);
    Ploeg ploeg = zoekPloeg(ploegnaam);
    toevoegenTrainerPloeg(persoon.getId(), ploeg.getId());
  }
  /**
   * deze methode geeft een ArrayList terug waarin alle spelers van de opgegeven ploeg zitten
   *
   * @param ploegnaam de naam van de ploeg waarvan je alle spelers wilt opvragen
   * @return
   * @throws DBException
   * @throws ApplicationException
   */
  public ArrayList<Persoon> zoekSpelersPloeg(String ploegnaam)
      throws DBException, ApplicationException {

    Ploeg p = zoekPloeg(ploegnaam);
    ArrayList<Persoon> a = zoekSpelersPloeg(p.getId());
    return a;
  }
  /**
   * ploeg toevoegen aan database
   *
   * @param p de ploeg die je wilt toevoegen
   * @throws DBException
   */
  public void toevoegenPloeg(Ploeg p) throws DBException {

    // connectie tot stand brengen (en automatisch sluiten)
    try (Connection conn = ConnectionManager.getConnection(); ) {
      // preparedStatement opstellen (en automtisch sluiten)
      if (p.getTrainer() != null) {

        try (PreparedStatement stmt =
            conn.prepareStatement(
                "INSERT INTO ploeg (`naam`, `niveau`, `trainer_id`) VALUES (?,?,?)"); ) {
          stmt.setString(1, p.getNaam());
          stmt.setString(2, p.getCategorie().getTekst());
          stmt.setInt(3, p.getTrainer());

          stmt.execute();
        } catch (SQLException sqlEx) {
          throw new DBException("SQL-exception in toevoegenPloeg(PloegBag p) - statement" + sqlEx);
        }
      } else {
        try (PreparedStatement stmt =
            conn.prepareStatement("INSERT INTO ploeg (`naam`, `niveau`) VALUES (?,?)"); ) {
          stmt.setString(1, p.getNaam());
          stmt.setString(2, p.getCategorie().getTekst());

          stmt.execute();
        } catch (SQLException sqlEx) {
          throw new DBException("SQL-exception in toevoegenPloeg(PloegBag p) - statement" + sqlEx);
        }
      }

    } catch (SQLException sqlEx) {
      throw new DBException("SQL-exception in toevoegenPloeg(PloegBag p) - connection" + sqlEx);
    }
  }
  /**
   * deze methode geeft een ArrayList terug met alle ploegen die de opgegeven persoon als trainer
   * hebben
   *
   * @param trainer_id het id van de opgegeven persoon
   * @return
   * @throws DBException
   * @throws ApplicationException
   */
  public ArrayList<Ploeg> zoekPloegenTrainer(int trainer_id)
      throws DBException, ApplicationException {
    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 trainer_id=?"); ) {
        stmt.setInt(1, trainer_id);
        // 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"));
            k.setTrainer(r.getInt("trainer_id"));

            kl.add(k);
          }
          return kl;
        } catch (SQLException sqlEx) {
          throw new DBException(
              "SQL-exception in zoekPloegenTrainer(int trainer_id) - resultset" + sqlEx);
        }
      } catch (SQLException sqlEx) {
        throw new DBException(
            "SQL-exception in zoekPloegenTrainer(int trainer_id) - statement" + sqlEx);
      }
    } catch (SQLException sqlEx) {
      throw new DBException(
          "SQL-exception in zoekPloegenTrainer(int trainer_id) - connection" + sqlEx);
    }
  }
  /**
   * @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");
    }
  }
  /**
   * @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");
    }
  }
Exemple #7
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());
        }
      }
    }
  }
  /**
   * deze methode geeft een ArrayList terug waarin alle spelers van de opgegeven ploeg zitten
   *
   * @param p de ploeg waarvan je alle spelers wilt opvragen
   * @return
   * @throws DBException
   * @throws ApplicationException
   */
  public ArrayList<Persoon> zoekSpelersPloeg(Ploeg p) throws DBException, ApplicationException {

    ArrayList<Persoon> kl = zoekSpelersPloeg(p.getNaam());
    return kl;
  }
 /**
  * deze methode ontkoppelt de trainer van de opgegeven ploeg
  *
  * @param p de opgegeven ploeg
  * @throws DBException
  * @throws ApplicationException
  */
 public void verwijderTrainerPloeg(Ploeg p) throws DBException, ApplicationException {
   verwijderTrainerPloeg(p.getNaam());
 }
  /**
   * deze methode koppelt de opgegeven trainer aan de opgegeven ploeg
   *
   * @param persoon Het persoon object van de trainer die je wilt toevoegen.
   * @param ploeg Het ploeg object waraan je een trainer wilt toevoegen.
   * @throws DBException
   * @throws ApplicationException
   */
  public void toevoegenTrainerPloeg(Persoon persoon, Ploeg ploeg)
      throws DBException, ApplicationException {

    toevoegenTrainerPloeg(persoon.getNaam(), persoon.getVoornaam(), ploeg.getNaam());
  }
  /**
   * deze methode koppelt de opgegeven speler van de ploegen waarin hij zit
   *
   * @param ploeg De ploeg waaraan je een speler wilt koppelen.
   * @param speler De persoon die je aan een ploeg wilt koppelen.
   * @throws DBException
   * @throws exception.ApplicationException
   */
  public void toevoegenSpelerPloeg(Ploeg ploeg, Persoon speler)
      throws DBException, ApplicationException {

    toevoegenSpelerPloeg(ploeg.getNaam(), speler);
  }
  /**
   * ploeg verwijderen van database
   *
   * @param p de ploeg die je wilt verwijderen
   * @throws DBException
   */
  public void verwijderPloeg(Ploeg p) throws DBException {

    verwijderPloeg(p.getNaam());
  }
 /**
  * deze methode zoekt de trainer op van de opgegeven ploeg
  *
  * @param p de ploeg waarvan je de trainer wilt zoeken
  * @return Geeft de trainer weer van een ploeg
  * @throws DBException
  * @throws ApplicationException
  */
 public Persoon getTrainer(Ploeg p) throws DBException, ApplicationException {
   return getTrainer(p.getNaam());
 }
 public Ploeg zoekPloeg(Ploeg p) throws DBException, ApplicationException {
   Ploeg returnPloeg = zoekPloeg(p.getNaam());
   return returnPloeg;
 }