salut tout le monde,
j'ai un problème avec l'accès à ma base de données,j'utilise le modèle MVC avec jsf,
et mon problème c'est que à l'exécution ma datatable doit etre remplie à partir des données de la base,
mon idée c'est de mettre le code de la selection de la base à l'interieur de la classe "association" dans
laquelle j'ai déclaré mon "Arraylist":
voila le code:
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 package beans; import java.util.ArrayList; import java.sql.*; import beans.Personne; public class Association { private ArrayList adherents = null; public Association(String a,String b) { this.adherents = new ArrayList(); this.adherents.add(new Personne(a,b)); } public Association() { //} //public Association(ArrayList adher ) { try { Connection conn= connexion.connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select nom,prenom from personne"); while(rs.next()){ this.adherents.add(new Personne(rs.getString(1),rs.getString(2))); //this.nom= rs.getString(1); //this.prenom= rs.getString(2); System.out.println(rs.getString(1)+" "+rs.getString(2)); } rs.close(); conn.close(); } catch(SQLException e2){ System.out.println("ERREUR de CONNEXION"); e2.printStackTrace(); } } public ArrayList getAdherents() { return this.adherents; } public void setAdherents(ArrayList adherents) { this.adherents = adherents; } public void removeAdherent(Personne adhe){ this.adherents.remove(adhe); } public void addAdherent(Personne adhe){ this.adherents.add(adhe); } }
et voila le code de l'interface "model":
et puis celui de la classe qui va implémenter "model":
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package beans; public interface Model { public Object getDatas(); public void setDatas(Object object); }
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 package beans; import java.util.ArrayList; import java.util.List; import java.sql.*; public class SimpleModel implements Model { private Object datas; public SimpleModel() { } public Object getDatas(){ return this.datas; /* try { Connection conn= connexion.connect(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select nom,prenom from personne"); while(rs.next()){ this.datas.adherents.add(new Personne(rs.getString(1),rs.getString(2))); //this.nom= rs.getString(1); //this.prenom= rs.getString(2); System.out.println(rs.getString(1)+" "+rs.getString(2)); } rs.close(); conn.close(); } catch(SQLException e2){ System.out.println("ERREUR de CONNEXION"); e2.printStackTrace(); } */ } public void setDatas(Object object){ this.datas = object; } }
et enfin la page jsp :
s'il vous plait aidez moi
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 <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@ page contentType="text/html" %> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <f:view> <h3>Association des footballeurs</h3> <h:form> <h:dataTable binding="#{assocCtrl.view.dataTable}" value="#{assocCtrl.model.datas.adherents}" var="personne" border="1"> <h:column> <h:selectBooleanCheckbox binding="#{assocCtrl.view.checkbox}"/> </h:column> <h:column> <f:facet name="header"> <f:verbatim>Nom</f:verbatim> </f:facet> <h:outputText value="#{personne.nom}"/> </h:column> <h:column> <f:facet name="header"> <f:verbatim>Prénom</f:verbatim> </f:facet> <h:outputText value="#{personne.prenom}"/> </h:column> </h:dataTable> <br> <h:commandButton value="Supprimer adhérent" action="#{assocCtrl.removeSelectedPersonnes}"/> <h:commandButton value="Ajouter adhérent" action="Ajout"/> </h:form> </f:view>
et merci d'avance.
Partager