Kindly go through the code below and the little explanation that follows thereafter...
The code for the web form
<form name="purchase" action="Purchase" method="POST">
<table border="1">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>Product Number</td>
<td><input type="text" name="ProdNum" value="" maxlength="7" /></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="quantity" value="" /></td>
</tr>
` <tr>
<td>Product Number</td>
<td><input type="text" name="ProdNum2" value="" maxlength="7" /></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="quantity2" value="" /></td>
</tr>
<tr>
<td>Product Number</td>
<td><input type="text" name="ProdNum3" value="" maxlength="7" /></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="quantity3" value="" /></td>
</tr>
<tr>
<td><input type="reset" value="Clear" name="clear" /></td>
<td><input type="submit" value="Purchase" name="purchase" /></td>
</tr>
</tbody>
</table>
</form>
The code for the purchase servlet.
String PID = request.getParameter("ProdNum");
String Num = request.getParameter("quantity");
float cost = 0;
float value = 0;
int amt,stocks;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Concept","admin","");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery ("SELECT * FROM Stock WHERE ProductID ="+PID+";");
while(rs.next())
{
//Assigning Database data to chosen variables
String ProdID = rs.getString(1);
String ProdName = rs.getString(2);
String Stock = rs.getString(3);
String Price1 = rs.getString(4);
//parsing Stock and Num to Integer values
amt = Integer.parseInt(Num);
stocks = Integer.parseInt(Stock);
if(stocks > amt)
{
//parsing Price1 contents to double
value = Float.parseFloat(Price1);
//calculation of the cost of purchases
cost = amt*value;
//Displaying the processed user input
out.print(ProdID +'\t');
out.print(ProdName + '\t');
out.print(Num + '\t');
out.print("$ "+ cost);
out.println('\t'+"<a href = purchase.jsp>Back</a>");
}
else
{
out.print("Stocks are below the ordered quantity");
out.println('\t' + "<a href = purchase.jsp>Back</a>");
}
}
//closing all connection processes to the database
con.close();
rs.close ();
st.close ();
}
catch(Exception e)
{
System.out.print("Unable to find DB");
}
Am trying to enhance the servlet so that i can simulate multiple purchases. Is there a way in which i can modify this servlet so that it can allow me to allow at least 3 products to be bought? Thanks for any help to be rendered.