Servlets / JSP以及对数据库的多次插入

时间:2012-10-07 06:25:03

标签: java database jsp servlets

我有一个页面可以让学生参加。它打印出一个学生列表,每个学生旁边都有一个复选框。然后,该人员单击提交并将所选学生的ID和实验室ID发送到数据库。

我遇到的问题是它将最后一个勾选的框发送到数据库而其余的都没有。我假设它覆盖了之前的那些,但我不确定如何解决这个问题。

这是我的doPost方法:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


            int user_id = Integer.parseInt((String)request.getParameter("user_id"));
            int lab_id =  Integer.parseInt((String)request.getParameter("lab_id"));


              System.out.println("I got a blow job");

              String message = null; 
              try {
                  Class.forName("com.mysql.jdbc.Driver");
                  Connection con = 
                    DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
                System.out.println("got connection"); 

                Statement s = con.createStatement(); 

                String sql = "INSERT INTO user_lab" + 
                          " (user_id, lab_id)" + 
                          " VALUES" + 
                          " ('" +  user_id + "'," + 
                             " '"  +  lab_id + "')"; 

                  System.out.println(sql);
                  int i = s.executeUpdate(sql); 
                  if (i==1) {
                    message = "Successful attendance."; 
                    response.sendRedirect("Tutor_labs");
                  } 

                  s.close(); 
                  con.close(); 
                } 
                catch (SQLException e) {
                  message = "Error." + e.toString(); 
                  boolean error = true; 
                } 
                catch (Exception e) {
                  message = "Error." + e.toString(); 
                  boolean error = true; 
                } 
                if (message!=null) {
                  PrintWriter out = response.getWriter(); 
                  out.println("<B>" + message + "</B><BR>"); 
                  out.println("<HR><BR>"); 
                } 

              } 

    }

这是我的JSP页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Mars University Lab System</title>
    <link rel="stylesheet" href="style.css" type="text/css" media="screen">
</head>

<body>
<jsp:include page="headerTutor.jsp"/>

<div id = "centrecontent">
<h3>Students In Tutorial</h3>
<h2>Attendance List</h2>

<%
    String[] list1 = (String[])request.getAttribute("res1");
    String[] list2 = (String[])request.getAttribute("res2");
    String[] list3 = (String[])request.getAttribute("res3");
    String[] list4 = (String[])request.getAttribute("res4");

         if(null == list1){%>

<th>Uni Id</th><th>Name</th><th>Email</th>      
<% 
         }else{ %>

        <form name="ViewStudentsTutor" ACTION="http://www.crackman.net.au/ICE/test.php\"  method="post">
<%      for(int i=0; i<list1.length; i++)
        {   
            %>                  <input type="hidden" name="lab_id" value=<%=request.getParameter("labid")%>>
                                <%out.println(list2[i]);%>
                                <%out.println(list3[i]);%>
                                <%out.println(list4[i]); %>
                                <input type="checkbox" name="user_id" value=<%out.println(list1[i]);%>><br>


    <%  }}
    %>  
     <input type="submit" value="Submit" name="Submit"/>
        </form> 


</div>
<jsp:include page="footer.jsp"/>

</body>
</html>

1 个答案:

答案 0 :(得分:3)

您只使用user_id一个request.getParameter("user_id") 相反,您应该使用request.getParameterValues("user_id")来返回您想要的所有String user_id数组,而不是一个user_id当前正在获取的数据

类似于lab_id