我想在文本框中显示下拉列表中所选项目的数量。数据存储在数据库中

时间:2014-03-17 07:45:13

标签: java jsp servlets

从数据库中检索数据到下拉列表的代码。

<%
    DataSource data = new MysqlDataSource();
    Connection con =  data.getConnection("root","system");
    System.out.println("Connected to MySQL");
    PreparedStatement pre = con.prepareStatement("select * from library.booklist");
    ResultSet result = pre.executeQuery();
%>
<tr>
<font color="black" size="5"> <td> Book List : </td> </font>
<td> <select name= "BookList" id="booklist" onchange="show()">
<option> - select - </option>
<%
while (result.next())
{
   String name = result.getString("Book");
   System.out.println("Output Done");
%>
<option value="<%=name%>"> <%=name%></option>
<%
}
%>
</select></td>
</tr>

<tr>
<%

/* my code for displaying quantity is below but it's not working. I am new to jsp, so don't know how to populate the textbox. */

String value = request.getParameter("BookList");
pre = con.prepareStatement("select Quantity from library.booklist where Book = ? ");
result = pre.executeQuery();
%>
<font color="black" size="5"><td> Quantity : </td>
<%
while (result.next())
{
    int quantity = result.getInt("Quantity");
    System.out.println("quantity dikha raha hai");
%>
<input name = "Quantity" type = "text" size = "25" readonly = "readonly" value = "<%=quantity%>"> <%=quantity%> </input>
<%
}
%>
</td>
</tr>

1 个答案:

答案 0 :(得分:0)

修改

您需要将BookList参数传递给准备好的声明:

String value = request.getParameter("BookList");
pre = con.prepareStatement("select Quantity from library.booklist where Book = ? ");

pre.setString(1, value); // <------

result = pre.executeQuery();

否则您的查询不知道要选择哪本书。

原始

您还没有说出实际问题是什么,但您的HTML标记存在一些肯定无法帮助的问题。

  1. 请勿使用<font>标记。它是not supported in HTML5

  2. 您的第二个<font>需要结束</font>

  3. 结尾处有一个结束</td>标记,没有开放<td>

  4. <input> tag is a void element,表示它不接受开始和结束标记之间的内容。将其写为<input type="text" name="Quantity" ... />

  5. 清理那些东西,你的问题可能会被解决,或者至少更明显。