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 130 131 132 133 134
|
package com.dao;
import static com.dao.DAOUtilitaire.fermeturesSilencieuses;
import static com.dao.DAOUtilitaire.initialisationRequetePreparee;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.beans.Entreprise;
public class EntrepriseDaoImpl implements EntrepriseDao {
private static final String SQL_SELECT_PAR_ID = "SELECT * FROM Entreprise where ident=?";
private static final String SQL_SELECT = "SELECT * FROM Entreprise";
private static final String SQL_INSERT = "INSERT INTO Entreprise (ident,noment,adresseent,telent) VALUES (?, ?, ?, ?)";
private DAOFactory daoFactory;
EntrepriseDaoImpl( DAOFactory daoFactory ) {
this.daoFactory = daoFactory;
}
@Override
public Entreprise trouver( String id ) throws DAOException {
return trouver( SQL_SELECT_PAR_ID, id );
}
@Override
public ArrayList<Entreprise> trouverTous() throws DAOException {
return trouverTous( SQL_SELECT);
}
@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.getident(), entreprise.getnoment(), entreprise.getadresseent(),entreprise.gettelent());
int statut = preparedStatement.executeUpdate();
if ( statut == 0 ) {
throw new DAOException( "Échec de la création de la station, aucune ligne ajoutée dans la table." );
}
valeursAutoGenerees = preparedStatement.getGeneratedKeys();
if ( valeursAutoGenerees.next() ) {
entreprise.setident( valeursAutoGenerees.getString( 1 ) );
} else {
throw new DAOException( "Échec de la création de la station en base, aucun ID auto-généré retourné." );
}
} catch ( SQLException e ) {
throw new DAOException( e );
} finally {
fermeturesSilencieuses( valeursAutoGenerees, preparedStatement, connexion );
}
}
private Entreprise trouver( String sql, Object... objets ) throws DAOException {
Connection connexion = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Entreprise entreprise = null;
try {
connexion = daoFactory.getConnection();
preparedStatement = initialisationRequetePreparee( connexion, sql, false, objets );
resultSet = preparedStatement.executeQuery();
if ( resultSet.next() ) {
entreprise = map( resultSet );
}
} catch ( SQLException e ) {
throw new DAOException( e );
} finally {
fermeturesSilencieuses( resultSet, preparedStatement, connexion );
}
return entreprise;
}
private ArrayList<Entreprise> trouverTous( String sql, Object... objets ) throws DAOException {
Connection connexion = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Entreprise loueur = null;
ArrayList<Entreprise> listestation=new ArrayList<Entreprise>();
try {
connexion = daoFactory.getConnection();
preparedStatement = initialisationRequetePreparee( connexion, sql, false, objets );
resultSet = preparedStatement.executeQuery();
while ( resultSet.next() ) {
loueur = map( resultSet );
System.out.println("nom:"+ loueur.getnoment());
listestation.add(loueur);
}
} catch ( SQLException e ) {
throw new DAOException( e );
} finally {
fermeturesSilencieuses( resultSet, preparedStatement, connexion );
}
return listestation;
}
private static Entreprise map( ResultSet resultSet ) throws SQLException {
Entreprise entreprise = new Entreprise();
entreprise.setident(resultSet.getString( "ident" ) );
entreprise.setadresseent(resultSet.getString("adresseent"));
entreprise.setnoment( resultSet.getString( "noment" ) );
entreprise.settelent( resultSet.getString( "telent" ) );
return entreprise;
}
} |
Partager