Dynamically fetch values from database and display in combo box in jsp

Rits rishi

Rits rishi

@rits-rishi-VsVG1W Oct 26, 2024
I am making a form in jsp where i have to filter 4 combo boxes one after the other .. the values of the combo box have to be fetched from the database dynamically..
in table there are 5 fields:-
id category subcategory brand price
1 garments jacket adidas 4000
2 garments shirt peter England 1000
3 sports bat BDM 800

my form contains combo boxes namely:
category:
subcategory:
brand:
price:

firstly, category should display only those values in list(on front end) that are present in database.[I have done this with sql query and resultset](e.g- garments,sports)
Now on selecting the category(e.g- garments) only the respective subcategory should be displayed i.e next combo box should contain only- jacket, shirt.
similarly other lists should be filtered..
Please tell me statements should be used.
  <tr><td>Product Category</td> <td><select name="cat1" id="category1" style='width:141px;' action="add_offer2.jsp" onchange="this.form.submit()"> 
      <%
      int act=Integer.parseInt( request.getParameter("act11").toString());
//if(act==1)         
      con=s1.get_my_con();
            try
            {
              Statement st=con.createStatement();
              String str1="select distinct  category from product_detail where id='"+id+"'";
              ResultSet myre = st.executeQuery(str1);
             
              while(myre.next())
                    {
                      String s =myre.getString("category");
                     
                     
                        // out.println(s);
                    %>
                    <option name="pcat" onclick="this.form.submit()" value="<%=s%>"><%=s%></option>
                    <%
                 
                    }   
            }catch(Exception ex){
                out.println(ex.getMessage());
                }
      %>
      </select></td></tr>  

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Nov 28, 2013

    A simple jsp needs to be refreshed. Try using Ajax and servlets.

    Try this. This is not in English but I think, it's understandable.