Advertisement
Guest User

xXx utilisateurs

a guest
Feb 20th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. package fr.eni.projet1.dal;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import fr.eni.projet1.bll.BusinessException;
  9. import fr.eni.projet1.bo.Utilisateur;
  10.  
  11. public class CompteDAOJdbcImpl implements CompteDAO{
  12.  
  13. private static final String INSERT_COMPTE = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  14. 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 = ?";
  15. private static final String DELETE_COMPTE = "delete from UTILISATEURS where no_utilisateur=?";
  16.  
  17.  
  18.  
  19. @Override
  20. public void insertCompte(Utilisateur compte) throws BusinessException {
  21. System.out.println("bonjour");
  22. if(compte==null)
  23. {
  24. BusinessException businessException = new BusinessException();
  25. businessException.ajouterErreur(CodesResultatDAL.INSERT_COMPTE_ECHEC);
  26. throw businessException;
  27. }
  28.  
  29. try(Connection cnx = ConnectionProvider.getConnection())
  30. {
  31. cnx.setAutoCommit(false);
  32. PreparedStatement pstmt;
  33. ResultSet rs;
  34. if(compte.getNoUtilisateur()==0)
  35. {
  36. System.out.println("testif");
  37. pstmt = cnx.prepareStatement(INSERT_COMPTE, PreparedStatement.RETURN_GENERATED_KEYS);
  38. pstmt.setString(1, compte.getPseudo());
  39. pstmt.setString(2, compte.getNom());
  40. pstmt.setString(3, compte.getPrenom());
  41. pstmt.setString(4, compte.getEmail());
  42. pstmt.setString(5, compte.getTelephone());
  43. pstmt.setString(6, compte.getAdresse().getRue());
  44. pstmt.setString(7, compte.getAdresse().getCodePostal());
  45. pstmt.setString(8, compte.getAdresse().getVille());
  46. pstmt.setString(9, compte.getMotDePasse());
  47. pstmt.setInt(10, compte.getCredit());
  48. pstmt.setBoolean(11, compte.isAdministrateur());
  49.  
  50. pstmt.execute();
  51. rs = pstmt.getGeneratedKeys();
  52. if(rs.next())
  53. {
  54. compte.setNoUtilisateur(rs.getInt(1));
  55. }
  56. rs.close();
  57. pstmt.close();
  58. }
  59. }
  60. catch(Exception e)
  61. {
  62. e.printStackTrace();
  63. BusinessException businessException = new BusinessException();
  64. businessException.ajouterErreur(CodesResultatDAL.INSERT_COMPTE_ECHEC);
  65. throw businessException;
  66. }
  67. }
  68.  
  69. @Override
  70. public void udpateCompte(int noUtilisateur) throws BusinessException {
  71. try(Connection cnx = ConnectionProvider.getConnection())
  72. {
  73. PreparedStatement pstmt = cnx.prepareStatement(UPDATE_COMPTE);
  74. pstmt.setInt(1, noUtilisateur);
  75. pstmt.executeUpdate();
  76. } catch (SQLException e) {
  77. e.printStackTrace();
  78. BusinessException businessException = new BusinessException();
  79. businessException.ajouterErreur(CodesResultatDAL.UPDATE_COMPTE_ERREUR);
  80. throw businessException;
  81. }
  82.  
  83. }
  84. @Override
  85. public void deleteCompte(int noUtilisateur) throws BusinessException {
  86. try(Connection cnx = ConnectionProvider.getConnection())
  87. {
  88. PreparedStatement pstmt = cnx.prepareStatement(DELETE_COMPTE);
  89. pstmt.setInt(1, noUtilisateur);
  90. pstmt.executeUpdate();
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. BusinessException businessException = new BusinessException();
  94. businessException.ajouterErreur(CodesResultatDAL.SUPPRESSION_COMPTE_ERREUR);
  95. throw businessException;
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement