How to populate a drop down menu in JSP from database

Hi,

I need to design a JSP page where i am required to populate the drop down menu from the database. Please let me know how it can be done. Your help would be sincerely appreciated.

Thanks
Gautam

Well first you need to get the data from the database using some kind of datasource connection. Then you can write some html to the browser to populate the combo box with a for loop.


// connect to database
Connection conn = ...// get conncetion, don't remember, sorry, you'll have to look that up
Statement stmt = new Statement(conn);
 
ResultSet rs = conn.executeQuery("SELECT * FROM someTable");
 
out.print("<select name=\\"combo1\\">\
");
 
while (rs.hasElements())
	  out.print("\	<option>" + rs.getString("column1") + "</option>\
"); // where column1 is the column in the database table
 
out.print("</option>\
");
 
rs.close();
stmt.close();
conn.close();

This has to be with an existing html form. I’m assuming you know how to deal with that, if not, just ask and I can give you some info on that.

Well the thing is simple.

sdms.CustomerBO cus=customer.selectCustomer();

<select size=“1” name=“cName” >
<%
for(int i=0;i<cus.length;i++)
out.println(“<option value=”+“\”“+cus[i].getCus_id()+”\“”+“>”+cus[i].getCus_name()+“</option>”);
%>
</select>

I am pasting this code from my own jsp page.
selectCustomer(); ==> Select data from my DB.
For loop doing your job.
Hope it help.

Hi,

I want to know how to fill my combo box with values from the database for a field “name”.

I have in my action class the following line.
Using dao, I have got values for field “name” and filled it in List list.
request.setAttribute(“UserCollection”,list);

How do I write in jsp the select and options tags…
what should be the name field in the select tag ?
Should I use a new getter and setters class?

Thanks,
Vidya