将表数据列表保存到jsp中的数据库中

时间:2018-11-26 10:30:29

标签: java mysql jsp

我正在尝试保存一个如下所示的表。 问题是,当我保存表的第一条记录时,仅将其插入数据库中。

请建议我如何将所有行数据保存到数据库中。

查看页面中的表格与

<table>
    <thead>
        <tr>
            <th><input type="checkbox" name="check" id="flowcheckall" value="1"></th>
            <th>Level of education</th>
            <th>college name</th>
            <th>university name</th>
            <th>year of passing</th>                
        </tr>
    </thead>
    <tbody id="dataTable">
        <%  
            int i = 0;
            for(i=0;i<eduDetails.size();i++) { 
            HashMap EduDet = new HashMap();
            EduDet = (HashMap)eduDetails.get(i);
        %>      
        <tr>
            <td><input type="checkbox" name="check_list" class="check_list" id="<% out.print(EduDet.get("s.no")); %>" value="<% out.print(EduDet.get("s.no")); %>" style="width:10px"></td>
            <td> <input type="text" id="levelofeducation" name="levelofeducation" value="<% out.print(EduDet.get("Level_of_Education")); %>" /></td>
            <td> <input type="text" id="college_name"  name="college_name" value="<% out.print(EduDet.get("College_Name_OR_School_Name")); %>" /></td>
            <td> <input type="text" id="university_name" name="university_name" value="<% out.print(EduDet.get("University_Name_OR_Board_Name")); %>" /></td>
            <td> <input type="text" id="yearofpassing" name="yearofpassing" value="<% out.print(EduDet.get("Year_of_Passing")); %>" /></td>
        </tr>
        <%} %>  
    </tbody>
</table> 

我的模型 JSP 类似于

employee_data1.put("level_of_education",request.getParameterValues("levelofeducation"));
employee_data1.put("college_name" ,request.getParameterValues("college_name"));
employee_data1.put("university_name",request.getParameterValues("university_name"));
employee_data1.put("year_of_passing",request.getParameterValues("yearofpassing"));

最后,我的控制器

try 
{
    SQL = "INSERT INTO education_details(Level_of_Education,College_Name_OR_School_Name,University_Name_OR_Board_Name,Year_of_Passing) VALUES ( ?, ?, ?, ? );";

    pstmt = connection.prepareStatement(SQL);
    String[] level_of_education = (String[]) employeeData1.get("level_of_education");
    for(String s : level_of_education) 
    {
        pstmt.setString(1, s);
    }

    System.out.println(employeeData1.get("level_of_education"));
    String[] college_name = (String[]) employeeData1.get("college_name");
    for(String c : level_of_education) 
    {
        pstmt.setString(2, c);
    }   

    String[] university_name = (String[]) employeeData1.get("university_name");
    for(String u : university_name) 
    {
        pstmt.setString(3, u);
    }

    String[] year_of_passing = (String[]) employeeData1.get("year_of_passing");
    for(String y : year_of_passing) 
    {
        pstmt.setString(4, y);
    }

    record = pstmt.executeUpdate();
    pstmt.close();
    connection.close();
}

1 个答案:

答案 0 :(得分:0)

您需要在循环内包含executeUpdate语句,也可以使用类似这样的批处理更新。

stm = db.prepareStatement("INSERT INTO ITEM (ID, TYPE, TITEL, UITGELEEND) VALUES (?, ?, ?, ?)"); 
db.setAutoCommit(false); 
for (int n = 0; n < ItemLijst.getItems().size(); n++) 
{ 
    Item huidigItem = ItemLijst.getItemObvIdx(n); 
    stm.setString(1, huidigItem.getID().toString()); 
    stm.setString(2, huidigItem.getType().toString()); 
    stm.setString(3, huidigItem.getTitel()); 
    stm.setString(4,String.valueOf(huidigItem.isUitgeleend())); 
    stm.addBatch(); 
} 
stm.executeBatch(); 
db.commit();