1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| package DAO;
import static DAO.DAOUtilitaire.fermeturesSilencieuses;
import static DAO.DAOUtilitaire.initialisationRequetePreparee;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import DAO.Client;
public class ClientDaoImpl implements ClientDao{
private static final String selection = "call selectionClient()";
private static final String selectionParIdentifiant = "select * from client where id = ?";
private static final String insertion = "call AjoutClient('nomC','prenomC','adresseC','emailC','telephoneC')";
private static final String suppression = "delete from client where id = ?";
private DAOFactory daoFactory;
ClientDaoImpl(DAOFactory daoFactory) {
this.daoFactory = daoFactory;
}
public Client selection(Long id) throws DAOException {
return selection(selectionParIdentifiant,id);
}
public Client selection(String requete, Object... objets) throws DAOException {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resulta = null;
Client client = null;
try{
connection = daoFactory.getConntion();
preparedStatement = initialisationRequetePreparee(connection,requete,false,objets);
resulta = preparedStatement.executeQuery();
while(resulta.next()){
client = clientDansBeans(resulta);
}
}catch(SQLException e){
throw new DAOException("La selection a échoué.",e);
}finally {
fermeturesSilencieuses( resulta, preparedStatement,connection );
}
return client;
}
public void ajout(Client ajouter) throws DAOException {
Connection connection = null;
PreparedStatement preparedStatement = null;
try{
connection = daoFactory.getConntion();
preparedStatement = connection.prepareStatement(insertion);
int conf = preparedStatement.executeUpdate();
if(conf == 0){
throw new DAOException("Echec de l'enregistrement.");
}
}catch( SQLException e){
throw new DAOException(e);
}finally{
fermeturesSilencieuses(preparedStatement,connection);
}
}
public void supprimer(Client supClient) throws DAOException {
Connection connexion = null;
PreparedStatement preparedStatement = null;
try{
connexion = daoFactory.getConntion();
preparedStatement = initialisationRequetePreparee(connexion,suppression,true,supClient.getId());
int resultaSup = preparedStatement.executeUpdate();
if(resultaSup == 0){
throw new DAOException("Echec de suppression.Merci de reessayer!!!");
}
else{
supClient.setId(null);
}
}catch(SQLException e){
throw new DAOException(e);
} finally {
fermeturesSilencieuses( preparedStatement, connexion );
}
}
@Override
public List<Client> listerClient() throws DAOException {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resulta = null;
List<Client> clients = new ArrayList<Client>();
try {
connection = daoFactory.getConntion();
preparedStatement = connection.prepareStatement(selection);
resulta = preparedStatement.executeQuery();
while ( resulta.next() ) {
clients.add(clientDansBeans( resulta ) );
}
} catch ( SQLException e ) {
throw new DAOException( e );
} finally {
fermeturesSilencieuses( resulta, preparedStatement,connection );
}
return clients;
}
private static Client clientDansBeans(ResultSet resulta) throws SQLException {
Client client = new Client();
client.setId(resulta.getLong("id"));
client.setNom(resulta.getString("nom"));
client.setPrenom(resulta.getString("prenom"));
client.setAdresse(resulta.getString("adresse"));
client.setEmail(resulta.getString("email"));
client.setTelephone(resulta.getString("telephone"));
return client;
}
} |
Partager