Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.eni.projet1.dal;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import fr.eni.projet1.bll.BusinessException;
- import fr.eni.projet1.bo.Utilisateur;
- public class CompteDAOJdbcImpl implements CompteDAO{
- private static final String INSERT_COMPTE = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- private static final String UPDATE_COMPTE = "UPDATE UTILISATEURS set (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur) where no_utilisateur = ?";
- private static final String DELETE_COMPTE = "delete from UTILISATEURS where no_utilisateur=?";
- @Override
- public void insertCompte(Utilisateur compte) throws BusinessException {
- System.out.println("bonjour");
- if(compte==null)
- {
- BusinessException businessException = new BusinessException();
- businessException.ajouterErreur(CodesResultatDAL.INSERT_COMPTE_ECHEC);
- throw businessException;
- }
- try(Connection cnx = ConnectionProvider.getConnection())
- {
- cnx.setAutoCommit(false);
- PreparedStatement pstmt;
- ResultSet rs;
- if(compte.getNoUtilisateur()==0)
- {
- System.out.println("testif");
- pstmt = cnx.prepareStatement(INSERT_COMPTE, PreparedStatement.RETURN_GENERATED_KEYS);
- pstmt.setString(1, compte.getPseudo());
- pstmt.setString(2, compte.getNom());
- pstmt.setString(3, compte.getPrenom());
- pstmt.setString(4, compte.getEmail());
- pstmt.setString(5, compte.getTelephone());
- pstmt.setString(6, compte.getAdresse().getRue());
- pstmt.setString(7, compte.getAdresse().getCodePostal());
- pstmt.setString(8, compte.getAdresse().getVille());
- pstmt.setString(9, compte.getMotDePasse());
- pstmt.setInt(10, compte.getCredit());
- pstmt.setBoolean(11, compte.isAdministrateur());
- pstmt.execute();
- rs = pstmt.getGeneratedKeys();
- if(rs.next())
- {
- compte.setNoUtilisateur(rs.getInt(1));
- }
- rs.close();
- pstmt.close();
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- BusinessException businessException = new BusinessException();
- businessException.ajouterErreur(CodesResultatDAL.INSERT_COMPTE_ECHEC);
- throw businessException;
- }
- }
- @Override
- public void udpateCompte(int noUtilisateur) throws BusinessException {
- try(Connection cnx = ConnectionProvider.getConnection())
- {
- PreparedStatement pstmt = cnx.prepareStatement(UPDATE_COMPTE);
- pstmt.setInt(1, noUtilisateur);
- pstmt.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- BusinessException businessException = new BusinessException();
- businessException.ajouterErreur(CodesResultatDAL.UPDATE_COMPTE_ERREUR);
- throw businessException;
- }
- }
- @Override
- public void deleteCompte(int noUtilisateur) throws BusinessException {
- try(Connection cnx = ConnectionProvider.getConnection())
- {
- PreparedStatement pstmt = cnx.prepareStatement(DELETE_COMPTE);
- pstmt.setInt(1, noUtilisateur);
- pstmt.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- BusinessException businessException = new BusinessException();
- businessException.ajouterErreur(CodesResultatDAL.SUPPRESSION_COMPTE_ERREUR);
- throw businessException;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement