尝试从同一表单中的选择选项中获取值

时间:2014-12-16 15:50:14

标签: java sql database jsp options

我正试图从select选项中获取价值。但它似乎无法获得,因为它是在形式上。我该怎么做 ?我的意思是从选项选择中取出值。

<strong>Select a product :</strong>
    <select name="stockid">
     <c:forEach var="row" items="${STOCKS1.rowsByIndex}">
      <option selected><c:out value="${row[0]}"/></option>
     </c:forEach>
    </select>
    </p>
<%!int query = 0; %>
 <%   
        if(request.getParameter("stockid") != null){
            Connection connection = DriverManager.getConnection(
                "jdbc:derby://localhost:1527/healthdb", "nbuser", "nbuser");

            PreparedStatement statement = connection.prepareStatement("select stockqty from stocks where stockid = ?");


            statement.setString(1,request.getParameter("stockid"));
            ResultSet resultset = 
                statement.executeQuery( ) ; 
        if(resultset.next()){
            out.print(resultset.getString("stockqty"));
            query = resultset.getInt("stockqty");
        }
 else
 out.print("lol");}%> 

1 个答案:

答案 0 :(得分:0)

1。)您无法使用selected选择所有项目!没有multiple="multiple"

 <c:forEach var="row" items="${STOCKS1.rowsByIndex}">
  <option selected><c:out value="${row[0]}"/></option>
 </c:forEach>

E.g。 topFr[]

<select name = "topFr[]" multiple="multiple">
    <option selected="selected">Apple</option>
    <option>Pear</option>
    <option selected="selected">Peach</option>
</select>

如果您可以选择多个,则name必须是此处的数组stockid[]

<select name="stockid[]">

如果您来自<Form>数组,则必须将request.getParameter("stockid")视为array

<强> 2)。 OR stockid = INT 然后

您无法将字符串传递给field = INT

如果stockid是INT!

statement.setString(1,request.getParameter("stockid"));

将为查询设置一个String,字符串将引用

select stockqty from stocks where stockid = "1" 

你需要

select stockqty from stocks where stockid = 1 

尝试

statement.setInt(1,Integer.parseInt(request.getParameter("stockid"));

你也应该用空测试!

String res = request.getParameter("stockid");
if(res != null && !res.isEmpty()){