0

在下面的代码中,我希望用户选择至少 1 个复选框,然后将该输入与输入的 product_id 一起输入,并将它们插入两个不同的表中。所以我遇到的问题是从视图/jsp页面和servlet中的查询语句传递多个component_id

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

我的servlet是(就这一点而言)

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);

谢谢

4

1 回答 1

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 回答