/** * 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"); } }