Issue
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.
Solution
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");
.
Answered By - Swati