Retrieve values associated with Checkbox Servlets

out.println("<th>");

out.println("<td width>Product Number</td>");

out.println("<td>Product Name</b></td>");

out.println("<td>Product Description</td>");

out.println("<td>Price</b></td>");

out.println("<td>Cost</td>");

out.println("<td>LineNumber</td>");

out.println("</th>");

out.println("</tr>");

out.println("<tr>");

out.println("<td><input type=checkbox name=items value=items1></td>");

out.println("<td width=42%>1</td>");

out.println("<td width=25%>TROY</td>");

out.println("<td width=25%>Action Movie</td>");

out.println("<td width=25%>10</td>");

out.println("<td width=25%>10</td>");

out.println("<td width=25%>1</td>");

out.println("</tr>");

<input type=submit value=ADD>

</form>

I have list of different items:

How to add those in AccessDB on next page

String items[]=req.getParameter(items);

pstmt=con.prepareStatement("Insert into Products (ProductNumber,ProductName,ProductDescription,Cost,LineNumber,Total) Values(?,?,?,?,?,?,?)");

How to get those values and insert into Database?

[1837 byte] By [haroon2006a] at [2007-10-2 5:57:50]
# 1

I assume that the product number is the key to the products table in your database and that when the user selects the checkbox, you want to do something with an existing product like put it into a shopping cart.

if your html looks like

<table>

<tr><td>Pick item</td><td>Item number</td><td>Name</td><td>Description</td></tr>

<tr><td><input type=checkbox name=items value=1101 /></td><td>1101</td><td>Troy</td><td>Historical Bomb</td></tr></tr>

<tr><td><input type=checkbox name=items value=1242 /></td><td>1242</td><td>Revenge of the Sith</td><td>Space opera</td></tr></tr>

<tr><td><input type=checkbox name=items value=1313 /></td><td>1313</td><td>The Pink Panther</td><td>Classic Comedy</td></tr></tr>

<tr><td><input type=checkbox name=items value=2828 /></td><td>2828</td><td>Citizen Kane</td><td>Overrated Masterpiece</td></tr></tr>

</table>

and your Java servlet code looks like

String [] items = request.getParameterValues("items");

for (String item : items) {

processItem(item);

}

your processItem method will be called for each selected item. Since the item is the key to the products table you can do something like

Statement stmt=con.prepareStatement("SELECT * FROM products WHERE ProductNumber=?");

And then you can add the product and its price to the user's cart.

If you are trying to add an item to your catalog, you will need to provide a form that doesn't have a product number, and asks the user to supply each of the paramters needed for the add, the database should generate the product number. Oracle uses sequences, Access uses autonumber.

pkwoostera at 2007-7-16 2:06:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...