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
| package com.test.dao;
import static com.test.dao.DAOUtilitaire.fermeturesSilencieuses;
import static com.test.dao.DAOUtilitaire.initialisationRequetePreparee;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.test.beans.Entreprise;
import com.test.beans.TypeEntreprise;
public class EntrepriseDaoImpl implements EntrepriseDao{
private static final String SQL_SELECT_PAR_EMAIL = "SELECT codeE, nomE, emailE, adresseE, loginE, mdpE FROM Entreprise WHERE email = ?";
private static final String SQL_INSERT = "INSERT INTO Entreprise (codee, codete, nome, emaile, adressee, logine, mdpe) VALUES (?, ?, ?, NOW())";
private DAOFactory daoFactory;
EntrepriseDaoImpl( DAOFactory daoFactory ) {
this.daoFactory = daoFactory;
}
/* Implémentation de la méthode définie dans l'interface EntrepriseDao */
@Override
public Entreprise trouver( String email ) throws DAOException {
return trouver( SQL_SELECT_PAR_EMAIL, email );
}
/* Implémentation de la méthode définie dans l'interface EntrepriseDao */
@Override
public void creer( Entreprise Entreprise ) throws DAOException {
Connection connexion = null;
PreparedStatement preparedStatement = null;
ResultSet valeursAutoGenerees = null;
try {
connexion = daoFactory.getConnection();
preparedStatement = initialisationRequetePreparee( connexion, SQL_INSERT, true, Entreprise.getCodeE(), Entreprise.getCodeTE(), Entreprise.getNomE(), Entreprise.getEmailE(), Entreprise.getAdresseE(), Entreprise.getLoginE(), Entreprise.getMdpE() );
int statut = preparedStatement.executeUpdate();
if ( statut == 0 ) {
throw new DAOException( "Échec de la création de l'Entreprise, aucune ligne ajoutée dans la table." );
}
valeursAutoGenerees = preparedStatement.getGeneratedKeys();
if ( valeursAutoGenerees.next() ) {
Entreprise.setCodeE( valeursAutoGenerees.getString( 1 ) );
} else {
throw new DAOException( "Échec de la création de l'Entreprise en base, aucun ID auto-généré retourné." );
}
} catch ( SQLException e ) {
throw new DAOException( e );
} finally {
fermeturesSilencieuses( valeursAutoGenerees, preparedStatement, connexion );
}
}
/*
* Méthode générique utilisée pour retourner un Entreprise depuis la base
* de données, correspondant à la requête SQL donnée prenant en paramètres
* les objets passés en argument.
*/
private Entreprise trouver( String sql, Object... objets ) throws DAOException {
Connection connexion = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Entreprise Entreprise = null;
try {
/* Récupération d'une connexion depuis la Factory */
connexion = daoFactory.getConnection();
/*
* Préparation de la requête avec les objets passés en arguments
* (ici, uniquement une adresse email) et exécution.
*/
preparedStatement = initialisationRequetePreparee( connexion, sql, false, objets );
resultSet = preparedStatement.executeQuery();
/* Parcours de la ligne de données retournée dans le ResultSet */
if ( resultSet.next() ) {
Entreprise = map( resultSet );
}
} catch ( SQLException e ) {
throw new DAOException( e );
} finally {
fermeturesSilencieuses( resultSet, preparedStatement, connexion );
}
return Entreprise;
}
/*
* Simple méthode utilitaire permettant de faire la correspondance (le
* mapping) entre une ligne issue de la table des Entreprises (un
* ResultSet) et un bean Entreprise.
*/
private static Entreprise map( ResultSet resultSet ) throws SQLException {
Entreprise Entreprise = new Entreprise();
TypeEntreprise typeEntreprise = new TypeEntreprise();
Entreprise.setCodeE( resultSet.getString( "code" ) );
Entreprise.setNomE( resultSet.getString( "nom" ) );
Entreprise.setEmailE( resultSet.getString( "email" ) );
Entreprise.setAdresseE( resultSet.getString( "adresse" ) );
Entreprise.setLoginE( resultSet.getString( "login" ) );
Entreprise.setMdpE( resultSet.getString( "Mot de passe" ) );
Entreprise.setCodeTE(typeEntreprise);
return Entreprise;
}
@Override
public String lister() {
return SQL_SELECT_PAR_EMAIL;
}
}
EntrepriseDao |
Partager