/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Physique; import Metier.Bibliothecaire; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; public class BibliothecaireServiceJDBCImpl implements BibliothecaireServiceJDBC{ ConnectionService conn; protected BibliothecaireServiceJDBCImpl() { try { conn = ConnectionService.getInstance(PhysiqueFactory.getBase(), PhysiqueFactory.getHost(), PhysiqueFactory.getNomBase(), PhysiqueFactory.getDbDriver(), PhysiqueFactory.getNdc(), PhysiqueFactory.getMdp()); } catch (Exception ex) { System.err.println("Erreur BibliothecaireServiceJDBCImpl constructeur" + ex); } } @Override public ArrayList<Bibliothecaire> getAll() { ArrayList<Bibliothecaire> bibliothecaires = new ArrayList(); try { Statement st = conn.getStatement(); ResultSet rs = st.executeQuery("SELECT * FROM bibliothecaire"); while (rs.next()) { Bibliothecaire b = new Bibliothecaire(rs.getString("ndc"), rs.getString("mdp")); b.setId(rs.getInt("id")); bibliothecaires.add(b); } rs.close(); } catch (Exception ex) { System.err.println("Erreur bibliothecaire getall" + ex); } return bibliothecaires; } @Override public void add(Bibliothecaire b) { try { Statement st = conn.getStatement(); st.executeUpdate("INSERT INTO bibliothecaire VALUES ('" + b.getNdc()+ "', '" + b.getMdp()+"')"); ResultSet rs = st.executeQuery("SELECT * FROM bibliothecaire WHERE ndc ='" + b.getNdc() + "', and mdp ='" + b.getMdp() + "'"); while (rs.next()) { b.setId(rs.getInt("id")); } rs.close(); } catch (Exception ex) { System.err.println("Erreur bibliothecaire add" + ex); } } @Override public void remove(Bibliothecaire b) { try { Statement st = conn.getStatement(); st.executeUpdate("DELETE FROM bibliothecaire WHERE id ='" + b.getId() + "'"); st.close(); } catch (Exception ex) { System.err.println("Erreur bibliothecaire remove " + ex); } } @Override public void update(Bibliothecaire b) { try { Statement st = conn.getStatement(); st.executeUpdate("UPDATE bibliothecaire SET ('" + b.getNdc() + "', '" + b.getMdp() + "') WHERE id ='" + b.getId() + "'"); } catch (Exception ex) { System.err.println("Erreur bibliothecaire update" + ex); } } }