从数据库中检索数据到下拉列表的代码。
<%
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>
答案 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标记存在一些肯定无法帮助的问题。
请勿使用<font>
标记。它是not supported in
HTML5。
您的第二个<font>
需要结束</font>
。
结尾处有一个结束</td>
标记,没有开放<td>
。
<input>
tag is a void element,表示它不接受开始和结束标记之间的内容。将其写为<input type="text" name="Quantity" ...
/>
清理那些东西,你的问题可能会被解决,或者至少更明显。