1 pièce(s) jointe(s)
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:
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:
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 :
Pièce jointe 254262