java - 在jsp中将表数据列表保存到数据库中

标签 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"));

最后我的 Controller

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();
}

最佳答案

您需要在循环中包含 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();

关于java - 在jsp中将表数据列表保存到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53479176/

相关文章:

java - IBM i (AS400) 关于新 Java 版本/Java 7 的更新

mysql - Matlab 数据库工具箱 - 警告 : com. mysql.jdbc.Connection@6e544a45 不可序列化

php - 如何在第三个表中使用两个表的返回字段连接?

java - 具有多个运算符的 JSTL 条件

Java Spring 下拉填充

java - Byte Buddy : Annotation and Class<? >[] 具有字节伙伴类的属性

java - 我的选项卡中的自定​​义 PNG 背景,Android 应用程序,(无法理解 logcat)

java - 位置标签(表单)无效

java - 处理 ExecutionException 的原因

mysql - 您应该如何删除具有唯一值的行?