2012-10-07 116 views
0

我有一個需要學生出席的頁面。它用每個學生旁邊的複選框打印出學生名單。然後該人員點擊提交併將選定的學生的ID和實驗室ID發送到數據庫。Servlets/JSP和多個數據庫插入

我遇到的問題是它發送最後一個打勾的框到數據庫,其餘的都沒有。我假設它覆蓋之前的那個,但我不確定我能如何解決這個問題。

這裏是我的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> 

回答

3

你只是一個user_id使用request.getParameter("user_id") 相反,你應該使用request.getParameterValues("user_id")將返回數組服用所有user_idString你想要的而不是單一的user_id你正在獲得哪些資金

類似的用戶lab_id

+0

在jsp或servlet中我應該做出這些更改嗎? – user1393064

+0

在servlet中,因爲當你使用scriptlet製作chekbox時,那麼許多複選框被創建爲'user_id',所以如果你想要所有的你需要用上面提到的方法在servlet中改變它。 – Abubakkar

+0

當我這樣做時,我得到的錯誤不能從字符串[]轉換爲字符串 – user1393064