save a list of table data into database in jsp












1















I am trying to save a table which looks like below.
The problem is, when i am saving the first record of the table only inserting into the database.



Please suggest me how to save all the rows data into database.



The table in view page is as



<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>


My model JSP is looks like



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


Finally my controller is as



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









share|improve this question

























  • It looks like you're overwriting the parameters for the statement but execute it only once. Instead execute the statement for each employee (or better add a batch - see JDBC docs for that) and before that set the parameters for that single employee.

    – Thomas
    Nov 26 '18 at 10:37











  • Each employee should have a list of education details. For same employee i have to save all his rows data on his employee_id. I am able to save only the last row data by the above code.

    – user1111
    Nov 26 '18 at 10:43











  • That's because you execute the statement only once but overwrite the parameters first - so the last set parameter will be used. Instead, execute the statement for each set of parameters. You're basically doing foreach( param1 ) { stmt.setParameter(...); } foreach( param2 ) { stmt.setParameter(...); } stmt.execute(); but you'd need forEach( paramSet) { stmt.setParameter(param1); stmt.setParameter(param2); stmt.execute(); } (note: this is pseudo code, the exact syntax is left for you).

    – Thomas
    Nov 26 '18 at 10:51


















1















I am trying to save a table which looks like below.
The problem is, when i am saving the first record of the table only inserting into the database.



Please suggest me how to save all the rows data into database.



The table in view page is as



<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>


My model JSP is looks like



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


Finally my controller is as



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









share|improve this question

























  • It looks like you're overwriting the parameters for the statement but execute it only once. Instead execute the statement for each employee (or better add a batch - see JDBC docs for that) and before that set the parameters for that single employee.

    – Thomas
    Nov 26 '18 at 10:37











  • Each employee should have a list of education details. For same employee i have to save all his rows data on his employee_id. I am able to save only the last row data by the above code.

    – user1111
    Nov 26 '18 at 10:43











  • That's because you execute the statement only once but overwrite the parameters first - so the last set parameter will be used. Instead, execute the statement for each set of parameters. You're basically doing foreach( param1 ) { stmt.setParameter(...); } foreach( param2 ) { stmt.setParameter(...); } stmt.execute(); but you'd need forEach( paramSet) { stmt.setParameter(param1); stmt.setParameter(param2); stmt.execute(); } (note: this is pseudo code, the exact syntax is left for you).

    – Thomas
    Nov 26 '18 at 10:51
















1












1








1








I am trying to save a table which looks like below.
The problem is, when i am saving the first record of the table only inserting into the database.



Please suggest me how to save all the rows data into database.



The table in view page is as



<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>


My model JSP is looks like



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


Finally my controller is as



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









share|improve this question
















I am trying to save a table which looks like below.
The problem is, when i am saving the first record of the table only inserting into the database.



Please suggest me how to save all the rows data into database.



The table in view page is as



<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>


My model JSP is looks like



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


Finally my controller is as



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






java mysql jsp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 12:22









dhilmathy

1,98321120




1,98321120










asked Nov 26 '18 at 10:30









user1111user1111

63




63













  • It looks like you're overwriting the parameters for the statement but execute it only once. Instead execute the statement for each employee (or better add a batch - see JDBC docs for that) and before that set the parameters for that single employee.

    – Thomas
    Nov 26 '18 at 10:37











  • Each employee should have a list of education details. For same employee i have to save all his rows data on his employee_id. I am able to save only the last row data by the above code.

    – user1111
    Nov 26 '18 at 10:43











  • That's because you execute the statement only once but overwrite the parameters first - so the last set parameter will be used. Instead, execute the statement for each set of parameters. You're basically doing foreach( param1 ) { stmt.setParameter(...); } foreach( param2 ) { stmt.setParameter(...); } stmt.execute(); but you'd need forEach( paramSet) { stmt.setParameter(param1); stmt.setParameter(param2); stmt.execute(); } (note: this is pseudo code, the exact syntax is left for you).

    – Thomas
    Nov 26 '18 at 10:51





















  • It looks like you're overwriting the parameters for the statement but execute it only once. Instead execute the statement for each employee (or better add a batch - see JDBC docs for that) and before that set the parameters for that single employee.

    – Thomas
    Nov 26 '18 at 10:37











  • Each employee should have a list of education details. For same employee i have to save all his rows data on his employee_id. I am able to save only the last row data by the above code.

    – user1111
    Nov 26 '18 at 10:43











  • That's because you execute the statement only once but overwrite the parameters first - so the last set parameter will be used. Instead, execute the statement for each set of parameters. You're basically doing foreach( param1 ) { stmt.setParameter(...); } foreach( param2 ) { stmt.setParameter(...); } stmt.execute(); but you'd need forEach( paramSet) { stmt.setParameter(param1); stmt.setParameter(param2); stmt.execute(); } (note: this is pseudo code, the exact syntax is left for you).

    – Thomas
    Nov 26 '18 at 10:51



















It looks like you're overwriting the parameters for the statement but execute it only once. Instead execute the statement for each employee (or better add a batch - see JDBC docs for that) and before that set the parameters for that single employee.

– Thomas
Nov 26 '18 at 10:37





It looks like you're overwriting the parameters for the statement but execute it only once. Instead execute the statement for each employee (or better add a batch - see JDBC docs for that) and before that set the parameters for that single employee.

– Thomas
Nov 26 '18 at 10:37













Each employee should have a list of education details. For same employee i have to save all his rows data on his employee_id. I am able to save only the last row data by the above code.

– user1111
Nov 26 '18 at 10:43





Each employee should have a list of education details. For same employee i have to save all his rows data on his employee_id. I am able to save only the last row data by the above code.

– user1111
Nov 26 '18 at 10:43













That's because you execute the statement only once but overwrite the parameters first - so the last set parameter will be used. Instead, execute the statement for each set of parameters. You're basically doing foreach( param1 ) { stmt.setParameter(...); } foreach( param2 ) { stmt.setParameter(...); } stmt.execute(); but you'd need forEach( paramSet) { stmt.setParameter(param1); stmt.setParameter(param2); stmt.execute(); } (note: this is pseudo code, the exact syntax is left for you).

– Thomas
Nov 26 '18 at 10:51







That's because you execute the statement only once but overwrite the parameters first - so the last set parameter will be used. Instead, execute the statement for each set of parameters. You're basically doing foreach( param1 ) { stmt.setParameter(...); } foreach( param2 ) { stmt.setParameter(...); } stmt.execute(); but you'd need forEach( paramSet) { stmt.setParameter(param1); stmt.setParameter(param2); stmt.execute(); } (note: this is pseudo code, the exact syntax is left for you).

– Thomas
Nov 26 '18 at 10:51














1 Answer
1






active

oldest

votes


















0














You need to include executeUpdate statement within loop or you can use batch update something like this..



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





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479176%2fsave-a-list-of-table-data-into-database-in-jsp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You need to include executeUpdate statement within loop or you can use batch update something like this..



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





    share|improve this answer






























      0














      You need to include executeUpdate statement within loop or you can use batch update something like this..



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





      share|improve this answer




























        0












        0








        0







        You need to include executeUpdate statement within loop or you can use batch update something like this..



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





        share|improve this answer















        You need to include executeUpdate statement within loop or you can use batch update something like this..



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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 14:23









        dhilmathy

        1,98321120




        1,98321120










        answered Nov 26 '18 at 11:07









        amaanullahkhanamaanullahkhan

        204




        204
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479176%2fsave-a-list-of-table-data-into-database-in-jsp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin