0

In the below code I wish to ask the user to select at least 1 of the check boxes and then take that input along with the entered product_id and insert them into two different tables. so the problem am having is with passing multiple component_id's from the view/jsp page and the query statement in the servlet

<%
  List<Component> components = new ArrayList<Component>();
  components = ComponentDb.selectComponents();
  for (Component component : components) {
   %>
  <tr> 
   <td></td>
   <td><input type="checkbox" name="component_id" value="    
   <%=component.getId()%>" required="required"><%=component.getName()%></td>
   //here i have the end of form and stuff i doubt its necessary

and my servlet is(as far as this is concerned)

String query = "insert into Product         
(product_id,product_name,product_description,product_price) VALUES  
('"+product_id+"','"+product_name+"','"+product_description+"','"+product_price+"');"+
            "insert into PC(product_id,component_id) VALUES 
("+product_id+","+component_id+")";
  //kinda puzzled here on how to pass the values seperately
    try {
        ps = connection.prepareStatement(query);
        int a = ps.executeUpdate(query);

Thank you

4

1 に答える 1

1

2 つのステートメントを作成して、ステートメントを 1 つずつ実行しますINSERT

String query = "insert into Product (product_id,product_name,product_description,product_price) VALUES (?,?,?,?)";
ps=connection.prepareStatement(query);
ps.setInt(1,product_id);
...
ps.executeUpdate();

query="insert into PC(product_id,component_id) VALUES (?,?)";
ps=connection.prepareStatement(query);
String []component_ids=request.getParameterValues("component_id");
for(String componentID: components_ids){
   ps.setString(1,product_id);
   ps.setString(2,componentID);
   ps.executeUpdate();
}
于 2012-09-22T03:08:05.633 に答える