Hey guys, I'm new to JSP and I want to make an order form with a dropdown list allowing the user to pick which product he/she wants to buy. And the products would come from the database i made. How can i retrieve the data from the database and place it in the dropdown list box?

Recommended Answers

All 3 Replies

Welcome. Head First Servlets and JSP is a great book. I suggest you buy it.

<td colspan="3"><input name="text4" id="vid" type="text"/></td>
                    <td colspan="1">
                        <select name="combo1" onchange="document.getElementById('vid').value=this.option[this.selectedIndex].text">
                            <option>Select One</option>
                   <%
                   stmt=con.prepareStatement("Select id from student order by id");
                   ResultSet rst=stmt.executeQuery();
                   while(rst.next())
                       {
                    %>
                            <option><%=rst.getString("id")%></option>
                     <%
                   }
                   }
                  catch (Exception e)
                   {
                       System.out.println("error in program:-"+e);
                   }
                   %>
                        </select></td>

hope it might work for u..

commented: Completeley wrong suggestion! Don't gibe bad advices -3
<td colspan="3"><input name="text4" id="vid" type="text"/></td>
                    <td colspan="1">
                        <select name="combo1" onchange="document.getElementById('vid').value=this.option[this.selectedIndex].text">
                            <option>Select One</option>
                   <%
                   stmt=con.prepareStatement("Select id from student order by id");
                   ResultSet rst=stmt.executeQuery();
                   while(rst.next())
                       {
                    %>
                            <option><%=rst.getString("id")%></option>
                     <%
                   }
                   }
                  catch (Exception e)
                   {
                       System.out.println("error in program:-"+e);
                   }
                   %>
                        </select></td>

hope it might work for u..

That code is completely wrong. Don't give advices if you don't know what you are doing. For starters you don't close anything. Zero re-usability. You don't set the value attribute of the options tag so what's the point of having a select box and the user doesn't gets notified if an exception occurs!

Everything needs to be in its own class with a method that returns the values of the select box in a list or an array.
Then in the jsp you call the method and loop the list. Also inside the list/array, there should be an object with attributes for the value of the option as well as what would be displayed.


--- Create a method that retrieves the data from the database and returns them. Call that method from a main method and test it. Once you are done, you can call it in the jsp. You can post the method if you have any problems with the code. Get it done and afterward you will deal with the jsp/presentation part.

commented: words :) +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.