将检查复选框值附近的文本框的值插入数据库

时间:2016-06-02 07:14:12

标签: mysql jsp servlets jdbc

我需要帮助将显示在复选复选框值附近的文本框的值作为单行插入数据库

产品名称是一个复选框,描述是一个文本框,它出现在同一行的复选框附近。如果选中复选框值,则需要将其相应的文本框值与检查的复选框值一起插入数据库。

对我来说,已检查的复选框值已插入到数据库中,但未插入其相应的文本框值,而是将第一个文本框值插入到数据库中

这是我的代码

Productlist.jsp

<%@page import="java.util.List"%>
    <%@page import="web.Products"%>
    <%@page import="java.util.ArrayList"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
     <form name="sform" method="post" action="Save_Products"> 
      <table style="width:40%">
          <tr> <th> Brand Name</th>
              <th> Product Name</th>
              <th> Description</th>
          </tr>
          <tr>
               <td><%
  List<Products> pdts = (List<Products>) request.getAttribute("productlist");
  if(pdts!=null){
    for(Products prod: pdts){
  out.println("<input type=\"hidden\" name=\"brand_name\" value=\""  + prod.getBrandname() + "\">");
      out.println("<br/>"+prod.getBrandname());

        } %> 
       <td><%
  if(pdts!=null){
    for(Products prod: pdts){
       out.println("<input type=\"checkbox\" name=\"prod\" checked=\"checked\" value=\""  + prod.getProductname() + "\">"  + prod.getProductname()+"<br>");
        } } %> </td>
       <td><%
       if(pdts!=null){
       for(Products prod: pdts){
        out.println("<input type=\"text\" name=\"desc\" style=\"width:50px; height:22px\" value=\""+prod.getDesc()+"\"/><br/>");
    } 
   }  
   } %> </td>  
   </tr>
    <br/>
    <br/>
    <tr>
        <td></td>
        <td align="center">  <input type="submit" value="Save" name="save"/> </td></tr> 

      </table>
         </form>
    </html>

Servlet代码

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;


public class Save_Products extends HttpServlet {
 static final String dbURL = "jdbc:mysql://localhost:3306/pdt";
     static final String dbUser = "root";
     static final String dbPass = "root";
    @Override
     public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         response.setContentType("text/html;charset=UTF-8");    
        PrintWriter out = response.getWriter();
            ResultSet rs=null;
            Connection connection = null;  
            try{
      HttpSession session = request.getSession();
    String[] brand_name =request.getParameterValues("brand_name");
String [] prod_list = request.getParameterValues("prod");
String [] desc_list = request.getParameterValues("desc");
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection (dbURL,dbUser,dbPass);

String sql="insert into pdt_list(brand_name,product_name,desc)values(?,?,?)";
PreparedStatement prep = connection.prepareStatement(sql); 
int num_values = Math.min(prod_list.size(), desc_list.size());

int count_updated = 0;
for(int i = 0; i < num_values; i++){
    prep.setString(1, brand_name[i]);
    prep.setString(2, prod_list[i]);
    prep.setString(3,desc_list[i]);
    count_updated += prep.executeUpdate();
}
if(count_updated>0)
{    
   out.print("Products Saved Successfully...");
 RequestDispatcher rd=request.getRequestDispatcher("Save_Success.jsp");    
            rd.forward(request,response);    
}
else{
    RequestDispatcher rd=request.getRequestDispatcher("Save_Failure.jsp");    
            rd.forward(request,response);   
}

prep.close();
       }
     catch(Exception E){
//Any Exceptions will be caught here
System.out.println("The error is"+E.getMessage());

    }  

        finally {
            try {
                connection.close();
            } 
        catch (Exception ex) {
                System.out.println("The error is"+ex.getMessage());
            }
                }

}

}

0 个答案:

没有答案