如何从db中获取一条记录到select呢?
How to get a record from db to select it?
我的数据库中有一个 table 产品。 table 字段之一是状态(良好、维护...)。
我正在使用 servlet 和 jsp。
在产品JSP中我静态填充了一些这样的状态:
<select id="selecStatus">
<option value="Good">Good</option>
<option value="Maintenance">Maintenance</option>
</select>
为了插入,我在 servlet 中做了一个请求,它起作用了。
String s = request.getParameter("selecStatus");
我的问题是更改已注册的产品。如果是输入文本我会这样做:
<jsp:useBean id="prod" class="entidade.Produto" scope="request"/>
...
<input type="text" name="selecStatus" value="${prod.status}">
但是因为它是 select 我不知道如何填写选项和 select 数据库中的选项。
有没有不使用javascript和php的解决方案?我对这些编程语言不熟悉。
也许您想像下面那样做:
<%@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>
并使用 String s = request.getParameter("selecStatus");
在 servlet
中获得以上值。
我的数据库中有一个 table 产品。 table 字段之一是状态(良好、维护...)。
我正在使用 servlet 和 jsp。
在产品JSP中我静态填充了一些这样的状态:
<select id="selecStatus">
<option value="Good">Good</option>
<option value="Maintenance">Maintenance</option>
</select>
为了插入,我在 servlet 中做了一个请求,它起作用了。
String s = request.getParameter("selecStatus");
我的问题是更改已注册的产品。如果是输入文本我会这样做:
<jsp:useBean id="prod" class="entidade.Produto" scope="request"/>
...
<input type="text" name="selecStatus" value="${prod.status}">
但是因为它是 select 我不知道如何填写选项和 select 数据库中的选项。
有没有不使用javascript和php的解决方案?我对这些编程语言不熟悉。
也许您想像下面那样做:
<%@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>
并使用 String s = request.getParameter("selecStatus");
在 servlet
中获得以上值。