public String getGenre(Integer idCat) throws GenreException { try { Context ctx = new InitialContext(); DataSource source = (DataSource) ctx.lookup("jdbc/MusicStore"); connexion = source.getConnection(); String requeteSQL = "SELECT LABEL FROM GENRE WHERE GENRE.IDGENRE = ?"; PreparedStatement prepStat = connexion.prepareStatement(requeteSQL); prepStat.setInt(1, idCat); ResultSet donnees = prepStat.executeQuery(); String genre = ""; if (donnees.next()) { genre = donnees.getString(1); } else { throw new GenreException("errorGenre"); } return genre; } catch (SQLException e) { throw new GenreException("sqlConnexionError"); } catch (NamingException e) { throw new GenreException("errorNaming"); } catch (GenreException ex) { throw new GenreException(ex.toString()); } finally { try { connexion.close(); } catch (SQLException e) { throw new GenreException("sqlConnexionError"); } } }
public ArrayList<Categorie> getCategories() throws GenreException { try { ArrayList<Categorie> arrCat = new ArrayList<Categorie>(); Context ctx = new InitialContext(); DataSource source = (DataSource) ctx.lookup("jdbc/MusicStore"); connexion = source.getConnection(); String requeteSQL = "SELECT * FROM GENRE"; PreparedStatement prepStat = connexion.prepareStatement(requeteSQL); ResultSet donnees = prepStat.executeQuery(); while (donnees.next()) { Categorie newCat = new Categorie(); newCat.setIdCategorie(donnees.getInt(1)); newCat.setLibelle(donnees.getString(2)); arrCat.add(newCat); } if (arrCat.isEmpty()) { throw new GenreException("errorGenreNotExist"); } return arrCat; } catch (SQLException ex) { throw new GenreException("sqlException"); } catch (NamingException ex) { throw new GenreException("errorNaming"); } catch (GenreException ex) { throw new GenreException(ex.toString()); } finally { try { connexion.close(); } catch (SQLException ex) { throw new GenreException("sqlException"); } } }