0

I have in my db one table, products. One of the table fields is the status(Good, Maintenance...).

I'm working with servlet and jsp.

In the product JSP I statically fill some statuses like this:

<select id="selecStatus"> 
      <option value="Good">Good</option> 
      <option value="Maintenance">Maintenance</option> 
</select>

To do an insert I do an request in the servlet and it works.

String s = request.getParameter("selecStatus");

My problem is to change a product already registered. If it was an input text I would do:

<jsp:useBean id="prod" class="entidade.Produto" scope="request"/>
...    
<input type="text" name="selecStatus"  value="${prod.status}">

But as it is a select I do not know how to fill in the options and select the one that is in the db.

Is there any solution without using javascript and php?I'm not familiar with these programming languages.

Joelend
  • 175
  • 10
  • Possible duplicate of [Selected value for JSP drop down using JSTL](https://stackoverflow.com/questions/15657367/selected-value-for-jsp-drop-down-using-jstl) – Jozef Chocholacek May 08 '19 at 14:04

1 Answers1

0

Maybe you want to do like below :

 <%@page import= "java.sql.*" %>
 <%@page import= "java.io.*" %>
    <% 
    //connection setup 
     Class.forName("com.mysql.jdbc.Driver");
    Connection 
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","","");
    Statement statement = con.createStatement() ;
   //your query to select status ,change column name and table name according to your requirement
    ResultSet rs= statement.executeQuery("select status from products") ; 
      %>

       <select name="selecStatus" required>

            <option value="select" >Select</option>
             <%while(rs.next()){ %>
           <!--printing out option from table i.e column status -->
            <option value="<%=rs.getString("status")%>"> <%=rs.getString("status")%> 
            </option>
             <%}%> 

        </select>

And get above value in servlet using String s = request.getParameter("selecStatus");.

Swati
  • 28,069
  • 4
  • 21
  • 41
  • Well, what if s/he has 1000 products in the DB? ;-) I'd say `select distinct status from products` would be better then. And the best would be to have a separate table (or a Java enum) with possible values of the status field, and list these. – Jozef Chocholacek May 08 '19 at 14:07
  • @JozefChocholacek Yes maybe,but he didn't specify such things in question.but,yes i agree with you . – Swati May 08 '19 at 14:10