I have a database with tables and values. I want to fetch that data and display it in jsp page. Don’t want to write query in jsp page, separate java class,creating java object values and then displaying it in select field in jsp. please help me…
here is my java class
p
ublic class EmpBean {
public java.util.List dataList(){
ArrayList list=new ArrayList();
try{
Class.forName("driver");
Connection con = DriverManager.getConnection("url", "user", "pwd");
Statement st=con.createStatement();
System.out.println("hiiiii");
ResultSet rs=st.executeQuery("select * from employee");
while(rs.next()){
list.add(rs.getString("name"));
list.add(rs.getString("address"));
list.add(rs.getString("contactNo"));
list.add(rs.getString("email"));
}
System.out.println(rs.getString("contactNo"));
}
catch(Exception e){}
return list;
}
}
//and my jsp file
<%@page language="java" import="java.util.*" %>
<html>
<body>
<table border="1" width="303">
<tr>
<td width="119"><b>Name</b></td>
</tr>
<%Iterator itr;%>
<%EmpBean p = new EmpBean();
List list= (List) p.dataList(); %>
for (itr=list.iterator(); itr.hasNext(); ){
%>
<tr>
<select name="" id="" style="width: 150px;"">
<option value="-1"><%=itr.next()%></option>
</select>
</tr>
<%}%>
</table>
</body>
</html>
i think you should try this
Assuming this class working fine and it is returning list with some value
now on your jsp page
just check this code…it might happen that values fetched may be wrong here just check the values by changing list indexing and also if you can surround the whole scriptlets thing with try catch so that if exception occurs it will be easy to find bug…good luck and don’t worry if it doesn’t works i will give you sample code for sure i am using this code my project