IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JSF Java Discussion :

Obtenir un contenu datatable à partir de MySQL


Sujet :

JSF Java

  1. #1
    Invité
    Invité(e)
    Par défaut Obtenir un contenu datatable à partir de MySQL
    Bonjour,

    Je débute sur Primefaces et j'ai un problème pour afficher mes données dans ma datatable.
    Je n'ai pas de DAO.

    VOici mon Managed Bean, la partie qui ne fonctionne pas est la méthode get_liste() (Je précise ne pas vouloir faire de DAO pour l'instant), la partie insertion marche bien:

    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
    package jsf2;
     
    import java.sql.*;
    import java.util.*;
    import javax.faces.bean.ManagedBean;
     
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.faces.application.FacesMessage;
    import javax.faces.context.FacesContext;
     
    import org.primefaces.context.RequestContext;
    import org.primefaces.event.SelectEvent;
     
    @ManagedBean
     
    public class crud {
     
    	private String prenom;
    	private String nom;
    	private int age;
     
        public String getPrenom() {
            return prenom;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
     
        public String getNom() {
            return nom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public int getAge() {
            return age;
        }
     
        public void setAge(int age) {
            this.age = age;
        }
     
     
    	public void ajouter(){
    		PreparedStatement ps = null;
     
    		try
    	     {
    	         Connection con = getDBConnection(); 
    	         String sql = "INSERT INTO etudiants(prenom,nom,age) VALUES(?,?,?)";
    	         ps = con.prepareStatement(sql); 
    	         ps.setString(1, getPrenom());
    	         ps.setString(2, getNom());
    	         ps.setInt(3, getAge());
    	         ps.executeUpdate();
    	         System.out.println("Data Added Successfully");
     
    	     }  
    		catch(Exception ex)
    	     {
    	    	 System.out.println("Your query is not working");
    	    	 ex.printStackTrace();
    	     }
    	}
     
    	public void get_liste(){
    		PreparedStatement ps = null;
    		public static  ArrayList<Etudiant> etudiants = new ArrayList<Etudiant>();
    		try
    	     {
    			Class.forName("com.mysql.jdbc.Driver");
    	         Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
    	         String sql = "SELECT * FROM etudiants";
    	         ps = con.prepareStatement(sql); 
    	         ResultSet r=ps.executeQuery(sql);
    	         while(r.next())
                 {
    	        	 Etudiant e = new Etudiant();
     
                 }
     
    	     }  
    		catch(Exception ex)
    	     {
    	    	 System.out.println("Your query is not working");
    	    	 ex.printStackTrace();
    	     }
    	}
     
    	public Connection getDBConnection() {
     
            Connection dbConnection = null;
     
            String URL = "jdbc:mysql://localhost:3306/test";
            String USER = "root";        
            String PASSWORD = "root";
            String DRIVER = "com.mysql.jdbc.Driver";
     
            try {
     
                Class.forName(DRIVER);
                dbConnection= DriverManager.getConnection(URL, USER, PASSWORD);
                System.out.println("Connection completed.");
     
            } catch (SQLException e) { 
     
                System.out.println(e.getMessage()); 
     
            }catch(ClassNotFoundException cnfe){
     
               cnfe.printStackTrace();
               System.out.println(cnfe.getMessage());
               System.exit(-1);
     
           }
     
            return dbConnection; 
        }
     
     
    }

    Et voici ma vue index.xhtml :

    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
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:p="http://primefaces.org/ui"  >
        <h:head>
            <title>JSF 2.0</title>
        </h:head>
     
    <h:body>
     
        <div style="background-color:blue;color:white"> Mon Formulaire</div>
     
     
     
    	<h:form>
    	    <h:panelGrid columns="2" cellpadding="5" styleClass="ui-grid">
    	        <h:outputLabel  value="Prénom: " ></h:outputLabel>
    	        <p:inputText value="#{crud.prenom}" ></p:inputText>
     
    	        <h:outputLabel for="step" value="Nom: " ></h:outputLabel>
    	        <p:inputText  value="#{crud.nom}"  ></p:inputText>
     
    	   		<h:outputLabel for="step" value="Age: " ></h:outputLabel>
    	        <p:spinner  value="#{crud.age}"  ></p:spinner>
     
     
    	    </h:panelGrid>
     
    	</h:form>
     
    	<p:commandButton value="Insert" action="#{crud.ajouter}"/>
     
    	 <div style="background-color:blue;color:white"> Ma Liste</div>
     
     <p:dataTable var="etudiant" value="#{dtBasicView.etudiants}">
    	    <p:column headerText="Prenom">
    	        <h:outputText value="#{etudiant.prenom}" />
    	    </p:column>
     
    	    <p:column headerText="Nom">
    	        <h:outputText value="#{etudiant.nom}" />
    	    </p:column>
     
    	    <p:column headerText="Age">
    	        <h:outputText value="#{etudiant.age}" />
    	    </p:column>
     
     
    	</p:dataTable>
     
     
     
    </h:body>
    </html>


    Auriez vous une idée parce que c'est super dur ? Merci

    Voici ma table sql :

    Nom : table.jpg
Affichages : 117
Taille : 17,7 Ko

  2. #2
    Invité
    Invité(e)
    Par défaut
    J'ai essayé de modifier le code en ajoutant une classe etudiantsDAO.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
    package jsf2;
    import jsf2.crud;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
     
    public class etudiantsDAO {
     
    	public static ArrayList<crud> getEtudiants(){
     
    		try
    	     {
     
    	         Connection con = getDBConnection(); 
    	         String sql = "SELECT * FROM etudiants";
    	         PreparedStatement ps = con.prepareStatement(sql); 
    	         ArrayList<crud> al = new ArrayList<crud>();
    	         ResultSet r=ps.executeQuery(sql);
    	         boolean found = false;
    	         while(r.next())
                 {
    	        	 crud e = new crud();
    	        	 e.setPrenom(r.getString("prenom"));
    	        	 e.setNom(r.getString("nom"));
    	        	 e.setAge(r.getInt("age"));
    	        	 al.add(e);
                 }
    	         r.close();	
    	         if (found) {
                    return al;
                } else {
                    return null; // no entires found
                }
    	     }  
    		catch(Exception ex)
    	     {
    	    	 System.out.println("Your query is not working");
    	    	 ex.printStackTrace();
    	    	 return (null);
    	     }
    	};
     
    	public static Connection getDBConnection() {
     
            Connection dbConnection = null;
     
            String URL = "jdbc:mysql://localhost:3306/test";
            String USER = "root";        
            String PASSWORD = "root";
            String DRIVER = "com.mysql.jdbc.Driver";
     
            try {
     
                Class.forName(DRIVER);
                dbConnection= DriverManager.getConnection(URL, USER, PASSWORD);
                System.out.println("Connection completed.");
     
            } catch (SQLException e) { 
     
                System.out.println(e.getMessage()); 
     
            }catch(ClassNotFoundException cnfe){
     
               cnfe.printStackTrace();
               System.out.println(cnfe.getMessage());
               System.exit(-1);
     
           }
     
            return dbConnection; 
        }
    };
    Puis j'ai modifié ma classe crud.java comme cela :
    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
    package jsf2;
     
    import java.sql.*;
    import java.util.*;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.model.SelectItem;
     
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.faces.application.FacesMessage;
    import javax.faces.context.FacesContext;
     
    import org.primefaces.context.RequestContext;
    import org.primefaces.event.SelectEvent;
     
    @ManagedBean(name="etudiants")
    @SessionScoped
    public class crud {
     
    	private String prenom;
    	private String nom;
    	private int age;
     
        public String getPrenom() {
            return prenom;
        }
     
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
     
        public String getNom() {
            return nom;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public int getAge() {
            return age;
        }
     
        public void setAge(int age) {
            this.age = age;
        }
     
     
    	public void ajouter(){
    		PreparedStatement ps = null;
     
    		try
    	     {
    	         Connection con = getDBConnection(); 
    	         String sql = "INSERT INTO etudiants(prenom,nom,age) VALUES(?,?,?)";
    	         ps = con.prepareStatement(sql); 
    	         ps.setString(1, getPrenom());
    	         ps.setString(2, getNom());
    	         ps.setInt(3, getAge());
    	         ps.executeUpdate();
    	         System.out.println("Data Added Successfully");
     
    	     }  
    		catch(Exception ex)
    	     {
    	    	 System.out.println("Your query is not working");
    	    	 ex.printStackTrace();
    	     }
    	}
     
    	public ArrayList<crud> getEtudiants() {
            return etudiantsDAO.getEtudiants();
        }
     
    	public Connection getDBConnection() {
     
            Connection dbConnection = null;
     
            String URL = "jdbc:mysql://localhost:3306/test";
            String USER = "root";        
            String PASSWORD = "root";
            String DRIVER = "com.mysql.jdbc.Driver";
     
            try {
     
                Class.forName(DRIVER);
                dbConnection= DriverManager.getConnection(URL, USER, PASSWORD);
                System.out.println("Connection completed.");
     
            } catch (SQLException e) { 
     
                System.out.println(e.getMessage()); 
     
            }catch(ClassNotFoundException cnfe){
     
               cnfe.printStackTrace();
               System.out.println(cnfe.getMessage());
               System.exit(-1);
     
           }
     
            return dbConnection; 
        }
     
     
    }
    Et enfin ma vue comme cela :

    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
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:p="http://primefaces.org/ui"  >
        <h:head>
            <title>JSF 2.0</title>
        </h:head>
     
    <h:body>
     
        <div style="background-color:blue;color:white"> Mon Formulaire</div>
     
     
     
    	<h:form>
    	    <h:panelGrid columns="2" cellpadding="5" styleClass="ui-grid">
    	        <h:outputLabel  value="Prénom: " ></h:outputLabel>
    	        <p:inputText value="#{crud.prenom}" ></p:inputText>
     
    	        <h:outputLabel  value="Nom: " ></h:outputLabel>
    	        <p:inputText  value="#{crud.nom}"  ></p:inputText>
     
    	   		<h:outputLabel  value="Age: " ></h:outputLabel>
    	        <p:spinner  value="#{crud.age}"  ></p:spinner>
     
     
    	    </h:panelGrid>
     
    	</h:form>
     
    	<p:commandButton value="Insert" action="#{crud.ajouter}"/>
     
    	 <div style="background-color:blue;color:white"> Ma Liste</div>
     
    	 <p:dataTable var="etudiant" value="#{crud.crud}">
    	    <p:column headerText="Id">
    	        <h:outputText value="#{etudiant.prenom}" />
    	    </p:column>
     
    	    <p:column headerText="Year">
    	        <h:outputText value="#{etudiant.nom}" />
    	    </p:column>
     
    	    <p:column headerText="Brand">
    	        <h:outputText value="#{etudiant.age}" />
    	    </p:column>
     
     
    	</p:dataTable>
     
     
     
    </h:body>
    </html>
    Malheureusement, cela ne marche toujours pas mais j'ai l'impression d'être pas loin (pas d'erreurs reportées..)

    j'ai toujours no records found :

    Nom : norecords.jpg
Affichages : 116
Taille : 53,8 Ko

  3. #3
    Invité
    Invité(e)
    Par défaut
    Finalement, j'ai résolu mon problème avec un autre exemple, merci .

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 24
    Dernier message: 12/07/2012, 13h58
  2. Réponses: 4
    Dernier message: 11/08/2009, 10h21
  3. [GD] Créer un vignette à partir d'un image contenue dans un table MySQL
    Par khoudj dans le forum Bibliothèques et frameworks
    Réponses: 3
    Dernier message: 02/09/2007, 11h36
  4. [VB.NET][MySQL]Obtenir une string a partir d'un SELECT !
    Par Nofrag dans le forum Accès aux données
    Réponses: 3
    Dernier message: 14/01/2007, 20h54
  5. Réponses: 5
    Dernier message: 19/06/2006, 23h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo