Hi there,
I have a servlet which is meant to retrieve data from the database by compairing the number entered via a web form. The trouble is that, it only gives me the last record in the database and not any other record. I have 5 records in the database table and i want to be able to retrieve any one of them using the servlet. The code for the jsp and the servlet is below. Can anyone please point out where my mistake is and how I can resolve it.
Thanks in advance.
*the JSP Code*
<html>
<head>
<title>Check Your data</title>
<body>
<form name="check" method="post" action="checkAvailable">
<table>
<tr>
<td><input name="command" type="hidden" value=""></td>
</tr>
<tr>
<td>Product No: </td>
<td><input name="prodNum" id="prodNum" type="text"></td>
</tr>
<tr>
<td><input name="check" type="submit" id="search" value="Check"></td>
<td> </td>
</tr>
</table>
</form>
</body>
</head>
</html>
the servlet
public class checkAvailable extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String ProdID = new String("");
String ProdName = new String("");
String PAmt = new String("");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//the driver manager string
Connection con = DriverManager.getConnection("jdbc:odbc:Args","admin","");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery ("SELECT ProductID,PName,Stock FROM Product");
while(rs.next())
{
ProdID = rs.getString("ProductID");
ProdName = rs.getString("PName");
PAmt = rs.getString("Stock");
}
con.close();
rs.close ();
st.close ();
}
catch(Exception e)
{
System.out.print("Unable to find DB");
}
if(ProdID.equals(request.getParameter("prodNum")))
{
out.print("Prodcut ID : " +ProdID+ " Product Name : " +ProdName+ " Quantity"+PAmt);
}
else
{
out.print("Please Enter Correct Product Number");
}
out.close();
}
}