Bonjour a tout , j’ai un petit problème avec les javabean , j’ai créé une jsp «BeanTest.jsp » dans laquelle un formulaire envoi les données saisie vers une autre page « BeanTest_action.jsp » cette dernière utilise un bean pour récupérer les données saisie et elle les insère dans une base de données acces puis elle fait une redirection vers la première page «BeanTest.jsp » ou j’affiche l’ensemble de données insérer dans la table , mais chaque fois que j’ajoute une nouvelle ligne elle ne s’affiche pas qu’après que j’actualise la page manuellement , voila le code source et merci pour toute aide.
BeanTest.jsp
BeanTest_action.jsp
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 <HTML> <BODY> <CENTER> <TABLE BORDER=5> <TR><TH > Formulaire Personne</TABLE> </CENTER> <P> <form action="BeanTest_action.jsp"> <table> <tr><td>ID</td><td><input type="text" name="id"></td></tr> <tr><td>Nom</td><td><input type="text" name="nom"></td></tr> <tr><td>Prénom</td><td><input type="text" name="prenom"></td></tr> <tr><td>Login</td><td><input type="text" name="login"></td></tr> <tr><td>Password</td><td><input type="text" name="password"></td></tr> <tr><td>Profil</td><td><input type="text" name="profil"></td></tr> </table> <input type="submit" value="OK"> </form> <br> <% java.util.Vector vect = (new bean.SimpleBean()).getPers(); %> <table width="80%" border="1"> <tr> <td>ID</td><td>Nom</td><td>Prenom</td><td>Login</td><td>Password</td><td>Profil</td> </tr> <% String[] tab; for(int i=0;i<vect.size();i++) { tab = (String[])vect.elementAt(i); %> <tr> <td><%=tab[0]%></td> <td><%=tab[1]%></td> <td><%=tab[2]%></td> <td><%=tab[3]%></td> <td><%=tab[4]%></td> <td><%=tab[5]%></td> </tr> <%}%> </table> </BODY> </HTML>
SimpleBean.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <HTML> <BODY> <jsp:useBean id="test" class="bean.SimpleBean" scope="session"/> <jsp:setProperty name="test" property="*" /> <% test.ajoutPers(); response.sendRedirect("BeanTest.jsp"); %> </BODY> </HTML>
Conection.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 package bean; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; import dataaccess.Conection; public class SimpleBean { private String id = ""; private String nom = ""; private String prenom = ""; private String login = ""; private String password = ""; private String profil = ""; public String getId() { return (id); } public void setId(String id) { this.id = id; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public String getProfil() { return profil; } public void setProfil(String profil) { this.profil = profil; } public void ajoutPers() throws ClassNotFoundException, SQLException { Connection connexion = Conection.getConn(); Statement requete = connexion.createStatement(); int i = requete.executeUpdate("insert into personne values(" + this.id + ",'" + this.nom + "','" + this.prenom + "','" + this.profil + "','" + this.login + "','" + this.password + "')"); } public static Vector getPers() throws ClassNotFoundException, SQLException { Vector vect = new Vector(); Connection connexion = Conection.getConn(); Statement requete = connexion.createStatement(); ResultSet resultat = requete.executeQuery("SELECT * FROM personne order by id;"); boolean encore = resultat.next (); while (encore) { String[] tab = new String[6]; tab[0]= resultat.getInt(1)+""; tab[1]= resultat.getString(2); tab[2]= resultat.getString(3); tab[3]= resultat.getString(4); tab[4]= resultat.getString(5); tab[5]= resultat.getString(6); vect.addElement(tab); encore = resultat.next (); } return vect; } }
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 package dataaccess; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Conection { public static Connection getConn() throws ClassNotFoundException, SQLException { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:db1"; Connection connexion = DriverManager.getConnection(url, "test", "test"); return connexion; } public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:db1"; Connection connexion = DriverManager.getConnection(url, "test", "test"); Statement requete = connexion.createStatement(); int i = requete.executeUpdate("insert into personne values(4,'bouslimi','atef','admin','fghfg','hfg')"); System.out.println("i = "+i); ResultSet resultat = requete.executeQuery("SELECT DISTINCT nom FROM personne order by nom;"); boolean encore = resultat.next (); while (encore) { /* Pour lire et afficher toutes les colonnes en tabulant */ System.out.print(resultat.getString(1)); encore = resultat.next (); } } }
Modéré par zekey: Ajout des balises code, s'il vous plait pensez-y
Partager