
i have to retrieve data from the database and want to store in a variable using rs.getString and that variable i have to use in dropdown in jsp page.

1)Bean.java:
package form;
import java.sql.*;
import java.util.*;
public class Bean {
public List dataList(){
ArrayList list=new ArrayList();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from data");
while(rs.next()){
list.add(rs.getString("name"));
}
}
catch(Exception e){}
return list;
}
}
2)BeanInServlet.java:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BeanInServlet extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
form.Bean p = new form.Bean();
List list=p.dataList();
req.setAttribute("data", list);
RequestDispatcher rd = req.getRequestDispatcher("/jsp/beandata.jsp");
rd.forward(req, res);
}
}
3)beandata.jsp:
<%@page language="java" import="java.util.*" %>
<html>
<body>
<select name="name">
<option value="Select">Select</option>
<%Iterator itr;%>
<% List data= (List)request.getAttribute("data");
for (itr=data.iterator(); itr.hasNext(); ){
%>
<option value="<%=itr.next()%>"><%=itr.next()%></option>
<%}%>
</select>
</body>
</html>
For more information, visit the following links:
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.