j'ai besoin d'aide , je veux savoir si mon code est correcte et comment effectuer des affichages de clients , modification et suppression avec accès à la base de données sans jdbc et merci d'avance
entity from database: Client.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
135
136
137
138
139
140
141
142
143
144
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entities;
 
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
 
/**
 *
 * @author Wanwana
 */
@Entity
@Table(name = "Client")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c"),
    @NamedQuery(name = "Client.findByIdcl", query = "SELECT c FROM Client c WHERE c.idcl = :idcl"),
    @NamedQuery(name = "Client.findByNom", query = "SELECT c FROM Client c WHERE c.nom = :nom"),
    @NamedQuery(name = "Client.findByPrenom", query = "SELECT c FROM Client c WHERE c.prenom = :prenom"),
    @NamedQuery(name = "Client.findByAdresse", query = "SELECT c FROM Client c WHERE c.adresse = :adresse"),
    @NamedQuery(name = "Client.findByEmail", query = "SELECT c FROM Client c WHERE c.email = :email"),
    @NamedQuery(name = "Client.findByTelephone", query = "SELECT c FROM Client c WHERE c.telephone = :telephone")})
public class Client implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "idcl")
    private String idcl;
    @Size(max = 20)
    @Column(name = "nom")
    private String nom;
    @Size(max = 20)
    @Column(name = "prenom")
    private String prenom;
    @Size(max = 20)
    @Column(name = "adresse")
    private String adresse;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Size(max = 20)
    @Column(name = "email")
    private String email;
    @Size(max = 20)
    @Column(name = "telephone")
    private String telephone;
 
    public Client() {
 
    }
    public Client(String nom, String prenom) {
        this.nom = nom;
        this.prenom = prenom;
    }
  public Client(String nom, String prenom, String adresse, String email, String telephone) {
        this.nom = nom;
        this.prenom = prenom;
        this.adresse = adresse;
        this.email = email;
        this.telephone = telephone;
    }
    public Client(String idcl) {
        this.idcl = idcl;
    }
 
    public String getIdcl() {
        return idcl;
    }
 
    public void setIdcl(String idcl) {
        this.idcl = idcl;
    }
 
    public String getNom() {
        return nom;
    }
 
    public void setNom(String nom) {
        this.nom = nom;
    }
 
    public String getPrenom() {
        return prenom;
    }
 
    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }
 
    public String getAdresse() {
        return adresse;
    }
 
    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public String getTelephone() {
        return telephone;
    }
 
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
 
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idcl != null ? idcl.hashCode() : 0);
        return hash;
    }
 
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Client)) {
            return false;
        }
        Client other = (Client) object;
        if ((this.idcl == null && other.idcl != null) || (this.idcl != null && !this.idcl.equals(other.idcl))) {
            return false;
        }
        return true;
    }
 
    @Override
    public String toString() {
        return "Entities.Client[ idcl=" + idcl + " ]";
    }
 
}
et ma stateless
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Stateless;
 
import Entities.Client;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.*;
 
 
/**
 *
 * @author Wanwana
 */
@Stateless
public class GestionnaireClientBean implements GestionnaireClientBeanLocal {
    @PersistenceContext(unitName = "project-ejbPU")
    private EntityManager em;
    private String message;
    private Object telephone;
 
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
     public void persist(Object object) {
        em.persist(object);
    }
@Override
    public void ajouterClient(Client clt) {
    em.persist(clt);
    }
  @Override
    public Client findClientById(Long idcl) {
         Query q = em.createQuery("SELECT clt FROM Client clt WHERE clt.id = :id_client");
        q.setParameter("id_client", idcl);
        return (Client) q.getSingleResult();
    }
    @Override
    public Client findClientByName(String nom) {
        Query q = em.createQuery("SELECT clt FROM Client clt WHERE clt.nom = :nom_client");
        q.setParameter("nom_client", nom);
        try {
            return (Client) q.getSingleResult();
        } catch (NoResultException e) {
            return null;
        } catch (NonUniqueResultException nu) {
            List<Client> c = (List<Client>) q.getResultList();
 
            return c.get(0);
        }
    }
 
@Override
    public Collection<Client> findAllClient() {
        Query q = em.createQuery("SELECT clt FROM Client clt");
        return q.getResultList();
    }
 
@Override
    public Client findClientByEmail(String email) {
        Query q = em.createQuery("SELECT clt FROM Client clt WHERE clt.email = :email_client");
        q.setParameter("email_client", email);
        try {
            return (Client) q.getSingleResult();
        } catch (NoResultException e) {
            return null;
        } catch (NonUniqueResultException nu) {
            List<Client> c = (List<Client>) q.getResultList();
            return c.get(0);
        }
    }
  @Override
    public Client findClientByTelephone(String telephone) {
    Query q = em.createQuery("SELECT clt FROM Client clt WHERE clt.telephone = :telephone_client");
        q.setParameter("telephone_client", telephone);
        return (Client) q.getSingleResult();
    }  
 
   @Override
    public void creerClient(String nom, String prenom, String adresse, String email, String telephone) {
        Client c = findClientByName(nom);
       if(c!=null){
           message="Le client existe déjà";
       }
          Client clt = new Client(nom, prenom, adresse, email, telephone);
        em.persist(clt);
    }
 @Override
    public void creerClientTest() {
     creerClient("Baba", "Ali", "rue de Bagdad", "alibaba@gmail.com", "0604444");
        creerClient("Baba", "Ali", "rue de Bagdad", "alibaba@gmail.com", "0604444");
        creerClient("Potter", "Harry", "ecole de magie", "harrypotter@gmail.com", "0604004");
        creerClient("Potter", "Harry", "ecole de magie", "harrypotter@gmail.com", "0604004");
        creerClient("Botter", "Chat", "dans la foret", "lechatbotter@gmail.com", "555678");
        creerClient("Botter", "Chat", "dans la foret", "lechatbotter@gmail.com", "555678");
    }
 @Override
    public void supprimerClient(Long id) {
      Client c = findClientById(id);
        em.remove(c);
        /*  Query q = em.createQuery("delete from Client c where c.id=:id ").
        setParameter("id", id);
        q.executeUpdate();*/
    }
   @Override
    public void modifierClientById(Long id, String nom, String prenom, String adresse, String email, String telepone) {
     Query q = em.createQuery("update Client clt set clt.nom=:nom, clt.prenom=:prenom, clt.adresse=:adresse, clt.email=:email, clt.telephone=:telephone where clt.idcl=:id ").
                setParameter("id", id).
                setParameter("nom", nom).
                setParameter("prenom", prenom).
                setParameter("adresse", adresse).
                setParameter("email", email).
                setParameter("telephone",telephone);
        q.executeUpdate();
   }
  }
et l'interface locale
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Stateless;
 
import Entities.Client;
import java.util.Collection;
import javax.ejb.Local;
 
/**
 *
 * @author Wanwana
 */
@Local
public interface GestionnaireClientBeanLocal {
 
    void ajouterClient(Client clt);
 
    Client findClientById(Long idcl);
 
    Client findClientByName(String nom);
 
    Collection<Client> findAllClient();
 
    Client findClientByEmail(String email);
 
    Client findClientByTelephone(String telephone);
 
    void creerClient(String nom, String prenom, String adresse, String email, String telephone);
 
    void creerClientTest();
 
    void supprimerClient(Long id);
 
 
 
    void modifierClientById(Long id, String nom, String prenom, String adresse, String email, String telepone);
 
}