Liste déroulante avec jsp
annitta
Messages postés
1
Date d'inscription
Statut
Membre
Dernière intervention
-
annitta -
annitta -
Bonjour,
SVp,j'ai besoin de votre aide,j ss en train de développer une application j2EE avec hibernate et struts,
et j voulais charger une liste déroulante à partir d'une table d ma base de donnée,et sa marche ps pr moi:-(
voila mon code jsp:list.jsp
sachant que ville_clt c'est le champ k j veux charger à partir de la table client:
Merci d'avance:-)
<%@ page language="java" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Charger la liste</title>
</head>
<body>
<form name="form1" action="">
<html:select name="select" property="ville">
<option>ville</option>
</html:select>
</form>
<%
String url = "jdbc:mysql://localhost:3306/gestion";
Class.forName("com.mysql.jdbc.Driver");
Connection con;
con = DriverManager.getConnection(url,"root","");
Statement instruction = con.createStatement();
%>
<%@ page import="java.sql.*"%>
<%
ResultSet resultat = instruction.executeQuery("SELECT ville_clt FROM client ");
while(resultat.next()){
//int nom=resultat.getInt("id_clt");
String ville=resultat.getString("ville_clt");
out.println("<tr>");
out.println("<option>"+ville+"</option>");
} %>
</body>
</html:html>
SVp,j'ai besoin de votre aide,j ss en train de développer une application j2EE avec hibernate et struts,
et j voulais charger une liste déroulante à partir d'une table d ma base de donnée,et sa marche ps pr moi:-(
voila mon code jsp:list.jsp
sachant que ville_clt c'est le champ k j veux charger à partir de la table client:
Merci d'avance:-)
<%@ page language="java" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Charger la liste</title>
</head>
<body>
<form name="form1" action="">
<html:select name="select" property="ville">
<option>ville</option>
</html:select>
</form>
<%
String url = "jdbc:mysql://localhost:3306/gestion";
Class.forName("com.mysql.jdbc.Driver");
Connection con;
con = DriverManager.getConnection(url,"root","");
Statement instruction = con.createStatement();
%>
<%@ page import="java.sql.*"%>
<%
ResultSet resultat = instruction.executeQuery("SELECT ville_clt FROM client ");
while(resultat.next()){
//int nom=resultat.getInt("id_clt");
String ville=resultat.getString("ville_clt");
out.println("<tr>");
out.println("<option>"+ville+"</option>");
} %>
</body>
</html:html>
A voir également:
- Liste déroulante avec jsp
- Liste déroulante excel - Guide
- Liste déroulante en cascade - Guide
- Liste déroulante google sheet - Accueil - Guide bureautique
- Liste code ascii - Guide
- Site dangereux liste - Guide
2 réponses
tu n'as pas besoin de faire ceci si tu utilise hibernate:
<%
String url = "jdbc:mysql://localhost:3306/gestion";
Class.forName("com.mysql.jdbc.Driver");
Connection con;
con = DriverManager.getConnection(url,"root","");
Statement instruction = con.createStatement();
%>
apres avoir mapper ta table ville avec hibernate tu fais ceci:
tu cree une classe getList.java ou tu mis cette methode:
public static List getVilleListt()
{
Session session = HibernateSessionFactory.getSession();
List list = null;
try{
Query query=session.createQuery("from Ville u");
list =query.list();
}catch(Exception ex){
ex.printStackTrace();
}
return list;
}
aprés il suffit de mettre dans la jsp:
<select>
<% Iterator it = GetList.getVilleList().iterator();
String v=null;
while(it.hasNext()){
Ville ville =(Ville)it.next();
v=ville.getNomVillev();
%>
<option value="<%=ville.getNomVillev()%>"> <%=v%></option>
<%}%> </select>
<%
String url = "jdbc:mysql://localhost:3306/gestion";
Class.forName("com.mysql.jdbc.Driver");
Connection con;
con = DriverManager.getConnection(url,"root","");
Statement instruction = con.createStatement();
%>
apres avoir mapper ta table ville avec hibernate tu fais ceci:
tu cree une classe getList.java ou tu mis cette methode:
public static List getVilleListt()
{
Session session = HibernateSessionFactory.getSession();
List list = null;
try{
Query query=session.createQuery("from Ville u");
list =query.list();
}catch(Exception ex){
ex.printStackTrace();
}
return list;
}
aprés il suffit de mettre dans la jsp:
<select>
<% Iterator it = GetList.getVilleList().iterator();
String v=null;
while(it.hasNext()){
Ville ville =(Ville)it.next();
v=ville.getNomVillev();
%>
<option value="<%=ville.getNomVillev()%>"> <%=v%></option>
<%}%> </select>