0

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?

4
Contributors
3
Replies
11
Views
6 Years
Discussion Span
Last Post by javaAddict
-1
<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..

Edited by __avd: Added [code]..[/code] tags.

Votes + Comments
Completeley wrong suggestion! Don't gibe bad advices
1
<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.

Votes + Comments
words :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.