Partage
  • Partager sur Facebook
  • Partager sur Twitter

Impossible d'afficher les données la base MySQL

    25 juillet 2023 à 10:27:00

    Bonsoir,

    Je suis débutant et pour mon exercice je travaille sur un projet de gestion de produits. Mais voilà, je n'arrive pas à afficher les données de la base MySQL. J'ai comme serveur Tomcat 10.1.

    La page s'affiche mais aucune donnée.


    Fichier JSP

    %@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<h1>LISTE DES PRODUITS</h1>
    	<table border="1">
    		<tr>
    			<th>DESIGNATION</th>
    			<th>PRIX</th>
    			<th>POIDS</th>
    			<th>DATE PEREMPTION</th>
    		</tr>
    		<c:forEach var="produit" items="${produits}">
    		<tr>
    			<td><c:out value="${produit.designation}" /></td>
    			<td><c:out value="${produit.prix}" /></td>
    			<td><c:out value="${produit.poids}" /></td>
    			<td><c:out value="${produit.date}" /></td>
    		</tr>	
    		</c:forEach>
    	</table>
    </body>
    </html>

    Code de connexion à la base

    public class Singleton {
    	private static Connection connection;
     
    	static {
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
     
    			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/gestionproduit", "root","");
    		}
    		catch(SQLException | ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
     
    	public static Connection getConnection() {
    		return connection;
    	}
     
    }

    Mon fichier bean Produit

    public class ProduitBean {
    	private int id;
    	private String designation ;
    	private Double prix;
    	private Double poids;
    	private String datePeremption;
     
    	public ProduitBean() {
    		super();
    	}
     
    	public ProduitBean(String designation, Double prix, Double poids, String dateperemption) {
    		this.designation = designation;
    		this.prix = prix;
    		this.poids = poids;
    		this.datePeremption = dateperemption;
    	}
     
    	public int getId() {
    		return id;
    	}
     
    	public void setId(int id) {
    		this.id = id;
    	}
     
    	public String getDesignation() {
    		return designation;
    	}
     
    	public void setDesignation(String designation) {
    		this.designation = designation;
    	}
     
    	public Double getPrix() {
    		return prix;
    	}
     
    	public void setPrix(Double prix) {
    		this.prix = prix;
    	}
     
    	public Double getPoids() {
    		return poids;
    	}
     
    	public void setPoids(Double poids) {
    		this.poids = (double) poids;
    	}
     
    	public String getDatePeremption() {
    		return datePeremption;
    	}
     
    	public void setDatePeremption(String date) {
    		this.datePeremption = date;
    	}
    }

    Fichier Produit qui implémente l'interface Produit

    public class C_Produit implements I_Produit{
     
    	Connection cn = null;
    	PreparedStatement ps = null;
    	ResultSet rs = null;
     
    	public List<ProduitBean> getListeProduit() {
    		List<ProduitBean> produits = new ArrayList<ProduitBean>();
    		try {
     
    			cn = Singleton.getConnection();
    			ps = cn.prepareStatement("SELECT * FROM produit");
    			rs = ps.executeQuery();
     
    			while(rs.next()) {
     
    				String designation = rs.getString("designation");
    				Double prix = rs.getDouble("prix");
    				Double poids = rs.getDouble("poids");
    				String date = rs.getString("date");
     
    				ProduitBean ListProduit = new ProduitBean();
    				ListProduit.setDesignation(designation);
    				ListProduit.setPrix(prix);
    				ListProduit.setPoids(poids);
    				ListProduit.setDatePeremption(date);
     
    				produits.add(ListProduit);
    			}
    		}
    		catch(SQLException e) {
    			e.printStackTrace();
    		}
     
    		return produits;
    	}
    }

    Ma Servlet

    @WebServlet("/ProduitServlet")
    public class ProduitServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
     
        public ProduitServlet() {
        	super();
        }
     
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        	C_Produit listProduit = new C_Produit();
        	request.setAttribute("produits", listProduit.getListeProduit());
     
            this.getServletContext().getRequestDispatcher( "/liste.jsp" ).forward( request, response );
        }
     
     
        protected void doPost(HttpServletRequest request, ServletResponse response) throws ServletException, IOException {
       }
    }

    Fix-chier web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>ProduitServlet</display-name>
    
    <servlet>
       <servlet-name>ProduitServlet</servlet-name>
       <servlet-class>com.controler.ProduitServlet</servlet-class>
    </servlet>
    <servlet-mapping> 
       <servlet-name>ProduitServlet</servlet-name>
       <url-pattern>/liste</url-pattern>
    </servlet-mapping>
    </web-app>










    • Partager sur Facebook
    • Partager sur Twitter

    Impossible d'afficher les données la base MySQL

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown