bonjour
je débute en jEE et vraiment je me plante dés le début
en fait avant d'entamer le projet demandé,j'ai essayé d'inserer une ligne dans une base de donnée a partir d'une page jsp.
Version imprimable
bonjour
je débute en jEE et vraiment je me plante dés le début
en fait avant d'entamer le projet demandé,j'ai essayé d'inserer une ligne dans une base de donnée a partir d'une page jsp.
Code:
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.ensi.bechir; import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import javax.xml.bind.annotation.XmlRootElement; /** * * @author bechir */ @Entity @Table(name = "client", catalog = "nettrading", schema = "") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Client.findAll", query = "SELECT c FROM Client c"), @NamedQuery(name = "Client.findById", query = "SELECT c FROM Client c WHERE c.id = :id"), @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")}) public class Client implements Serializable { private static final long serialVersionUID = 1L; @Id @Basic(optional = false) @NotNull @Column(name = "id", nullable = false) private Integer id; @Basic(optional = false) @NotNull @Size(min = 1, max = 20) @Column(name = "nom", nullable = false, length = 20) private String nom; @Basic(optional = false) @NotNull @Size(min = 1, max = 20) @Column(name = "prenom", nullable = false, length = 20) private String prenom; public Client() { } public Client(Integer id) { this.id = id; } public Client(Integer id, String nom, String prenom) { this.id = id; this.nom = nom; this.prenom = prenom; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } 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; } @Override public int hashCode() { int hash = 0; hash += (id != null ? id.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.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @Override public String toString() { return "com.ensi.bechir.Client[ id=" + id + " ]"; } }
Code:
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.ensi.bechir; import javax.ejb.Stateless; import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagementType; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import javax.persistence.PersistenceContext; /** * * @author bechir */ @TransactionManagement(TransactionManagementType.CONTAINER) @Stateless public class sessionbechir implements sessionbechirLocal { @Override public void creerclient(Client c) { em.persist(c); } @PersistenceContext(unitName = "bechir-ejbPU") private EntityManager em; @Override public void persist_1(Object object) { em.persist(object); } @Override public void creerclient(int id, String nom, String prenom) { EntityManagerFactory emf=Persistence.createEntityManagerFactory("bechir-ejbPU"); EntityManager em1=emf.createEntityManager(); //EntityTransaction entr = em1.getTransaction(); em1.getTransaction().begin(); //entr.begin(); Client c = new Client(id,nom,prenom); em1.persist(c); em1.getTransaction().commit(); //entr.commit(); //em1.getTransaction().commit(); } // @Override /*private void print(String string) { throw new UnsupportedOperationException("Not yet implemented"); }*/ @Override public void businessMethod(Client c) { throw new UnsupportedOperationException("Not supported yet."); } @Override public String test() { throw new UnsupportedOperationException("Not supported yet."); } }
Code:
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 <%-- Document : index Created on : 20 déc. 2011, 19:31:31 Author : bechir --%> <%@page import="com.ensi.bechir.Client"%> <%@page import="com.ensi.bechir.sessionbechir"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> <% sessionbechir objs = new sessionbechir(); //out.print(sess.test()); objs.creerclient(50, "nom", "prenom"); Client c = new Client(3,"b","l"); try{ out.print(c.getId()); out.print(c.getNom()); out.print(c.getPrenom()); out.print("<br>"); // sess.creerclient(c); // sess.creerclient_C(5, "nom", "prenom"); }catch (Exception e) {out.print(e);} %> </body> </html>
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="bechir-ejbPU" transaction-type="JTA"> <jta-data-source>bechir</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> </properties> </persistence-unit> <persistence-unit name="bechir-ejbPU2" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <jta-data-source>bechir</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="eclipselink.ddl-generation" value="create-tables"/> </properties> </persistence-unit> </persistence>
Code:
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.ensi.bechir; import javax.ejb.Local; /** * * @author bechir */ @Local public interface sessionbechirLocal { void businessMethod(Client c); public String test(); public void creerclient(com.ensi.bechir.Client c); public void persist_1(java.lang.Object object); public void creerclient(int id, String nom,String prenom); }