SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2004
    Location
    dc
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code:
    // 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\">\n");
     
    while (rs.hasElements())
    	  out.print("\t<option>" + rs.getString("column1") + "</option>\n"); // where column1 is the column in the database table
     
    out.print("</option>\n");
     
    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.

  3. #3
    SitePoint Enthusiast Nawaz Ijaz's Avatar
    Join Date
    Feb 2005
    Location
    Lahore, Pakistan
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Nawaz Ijaz [Lahore, Pakistan]
    Boy: He is big..i wont like to fight him...
    Achilles: Thatz Y no one wud remember u after thousand years...!!

  4. #4
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to populate a drop down menu in JSP from database

    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •