I want to display the quantity of the selected item of a drop down list in a textbox. The data is stored in database

899 views Asked by At

The code for retrieving data from database into Drop Down List.

<%
    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

There are 1 answers

3
Alex Wittig On

Edit

You need to pass your BookList parameter to your prepared statement:

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

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

result = pre.executeQuery();

Otherwise your query does not know which Book to select.

Original

You haven't said what the actual problem is, but your HTML markup has problems that definitely aren't helping.

  1. Don't use the <font> tag. It is not supported in HTML5.

  2. Your second <font> needs a closing </font>.

  3. You have a closing </td> tag at the end with no opening <td>.

  4. The <input> tag is a void element, meaning it does not accept content between opening and closing tags. Write it as <input type="text" name="Quantity" .../>

Clean up those things and your problem might be fixed, or at least more obvious.