我需要帮助将多个复选框值插入mysql数据库中的单独行。
工作流程
1.用户选中产品复选框,并在特定复选框旁边的文本框中输入有关产品的说明。
2.选中复选框后,用户点击保存按钮。
3.单击“保存”,应将产品详细信息插入数据库。 注意:用户可以选择多个复选框值。
问题:
我使用了一般方法来插入值,但只有一个复选框值被插入到数据库中。我需要在单击后将多个复选框值及其文本框值插入到数据库中作为单独的行保存按钮。
这是我的代码
products.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 method="post" action="Save_Products">
<b>
Brand Name:<font color="green">
<% String brand_name=(String)session.getAttribute("brand_name");
out.print(brand_name);%>
<c:set var="brand_name" value="brand_name" scope="session" />
</font></b>
<table>
<tr>
<th> Products</th>
<th> Description </th>
</tr>
<tr>
<td> <b><%
List<Products> pdts = (List<Products>) request.getAttribute("list");
if(pdts!=null){
for(Products prod: pdts){
out.println("<input type=\"checkbox\" name=\"prod\" value=\"" + prod.getProductname() + "\">" + prod.getProductname()+"<br>");
} %> </b></td>
<td><%for(Products prod: pdts){
out.println("<input type=\"text\" name=\"desc\" style=\"width:50px; height:22px\"/><br/>");
}
}
%> </td>
</tr>
<br/>
<br/>
<tr><td align="center"> <input type="submit" value="Save" name="save"/> </td></tr>
</table>
</form>
</body>
</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 =(String) session.getAttribute("brand_name");
String prod=request.getParameter("prod");
String desc=request.getParameter("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);
prep.setString(1, brand_name);
prep.setString(2, prod);
prep.setString(3,desc);
int i=prep.executeUpdate();
if(i>0)
{
out.print("Product Details 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 :(得分:1)
在您的servlet中使用request.getParameterValues("prod")
来获取您收到的所有输入的数组。 request.getParameter("prod")
只会给你一个。您的desc
值也是如此。
然后遍历这些值并将它们全部插入到您的数据库中
String [] prod_list = request.getParameterValues("prod");
String [] desc_list = request.getParameterValues("desc");
// set up your query
PreparedStatement prep = connection.prepareStatement(sql);
// just a precaution here getting min so we do not get outofbounds exception on one of the arrays
// both arrays should be of same size - if not, something is wrong on your jsp logic
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);
prep.setString(2, prod_list[i]);
prep.setString(3,desc_list[i]);
count_updated += prep.executeUpdate();
}
// Rest of your logic
这是使用PreparedStatement
的更简单方法。检查如何使用PreparedStatement
进行批量插入。 This应该让你开始。