title: How to insert the dynamic added row values into the database using JDBC Servlets
body: <p>Here is my jsp code, am adding and deleting the rows dynamically using javascript,but while inserting the details into the database mysql,it will insert only 1 row..How to add the dynamically added row values into the database using servlets..
  Here is my code</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt; Timeline Sheet&lt;/title&gt;
   &lt;SCRIPT language="javascript"&gt;
   $('form').change(function(){
      $('form').submit();
      });
      function validLogin(){
        if (document.form.eid.value == ""){
               alert ( "Please enter Employee Id." );
                document.form.eid.focus();
                return false;
       }
      if (document.form.ename.value == ""){
               alert ( "Please enter Employee Name." );
               document.form.ename.focus();
              return false;
       }
       alert ( "Inserted Successfully" );
       return true;
  }
  
      function addRow(tableID) {
  
          var table = document.getElementById(tableID);
  
         // rowCount = table.rows.length;
          var row = table.insertRow(1);
  
          var colCount = table.rows[2].cells.length;
  
          for(var i=0; i&lt;colCount; i++) {
  
              var newcell = row.insertCell(i);
  
              newcell.innerHTML = table.rows[2].cells[i].innerHTML;
              //alert(newcell.childNodes);
              switch(newcell.childNodes[0].type) {
                  case "text":
                          newcell.childNodes[0].value = 0;
                          break;
                  case "checkbox":
                          newcell.childNodes[0].checked = false;
                          break;
                  case "select-one":
                          newcell.childNodes[0].selectedIndex = 0;
                          break;
                  case "value":
                      newcell.childNodes[0].value=0;
              }
          }
      }
  
      function deleteRow(tableID) {
          try {
          var table = document.getElementById(tableID);
          var rowCount = table.rows.length;
  
          for(var i=1; i&lt;rowCount; i++) {
              var row = table.rows[i];
              var chkbox = row.cells[0].childNodes[0];
              if(null != chkbox &amp;&amp; true == chkbox.checked) {
                  if(rowCount &lt;= 3) {
                      alert("Cannot delete all the rows.");
                      break;
                  }
                  table.deleteRow(i);
                  rowCount--;
                  i--;
              }
  
  
          }
          }catch(e) {
              alert(e);
          }
      }
  
      function getColumnCount()
      {
          return document.getElementById('dataTable').getElementsByTagName('tr')       [1].getElementsByTagName('td').length;         
      }
  
      function getRowCount()
      {
          return document.getElementById('dataTable').rows.length;
      }
  
      function doAdd(ths)
      {
          //alert(ths.parentNode.cellIndex);
          //alert(getColumnCount());
          var lastCol = getColumnCount()-1;
          var lastRow = getRowCount()-1;
          //for Column Sum
           var table = document.getElementById("dataTable");
          var row = table.rows[ths.parentNode.parentNode.rowIndex];
          var colSum=0;
          for(var i=4;i&lt;lastCol;i++)
              colSum=eval(colSum) + eval(row.cells[i].childNodes[0].value);
          row.cells[lastCol].innerHTML = colSum;
  
          //for Row Sum
          var cIndex = ths.parentNode.cellIndex;
          //alert(cIndex);
          var rowSum = 0;
          for(var i=1;i&lt;lastRow;i++)
              rowSum = eval(rowSum) +   parseInt(table.rows[i].cells[cIndex].childNodes[0].value);
          table.rows[lastRow].cells[cIndex].innerHTML = rowSum;
          //grand Total
          var finSum = 0;
  
          for(var i=1;i&lt;lastRow;i++)
              finSum = eval(finSum) + parseInt(table.rows[i].cells[lastCol].innerHTML);
  
          table.rows[lastRow].cells[lastCol].innerHTML = finSum;
  
  
      }
  
  
  &lt;/SCRIPT&gt;
  
   &lt;/head&gt;
   &lt;body&gt;
  
  &lt;center&gt; &lt;b&gt; &lt;i&gt;SAMPLE TIMELINE SHEET &lt;/b&gt; &lt;/i&gt; &lt;/center&gt;
  
  &lt;br&gt;
   &lt;br&gt;
  
   &lt;form name="form" action="LoginServlet" method="post" onsubmit="return validLogin();"&gt;
   Employee ID:&lt;input type="text" name="eid"&gt;&lt;br&gt;&lt;br&gt;
   Employee Name:&lt;input type="text" name="ename"&gt;&lt;br&gt;&lt;br&gt;
   From Date&lt;input type="date"  name="fromDate" /&gt;
   To Date:&lt;input type="date" name="todate"&gt;
   &lt;br&gt;
  &lt;br&gt;
   &lt;br&gt;
  
   &lt;table id="dataTable" border="2"  cell padding="1" cell spacing="1"&gt;
  
  
  &lt;tr&gt;
  &lt;th style="background-color:white"&gt;&lt;/th&gt;
      &lt;th&gt;Project&lt;/th&gt;
       &lt;th&gt;Reports&lt;/th&gt;
  &lt;th&gt;Activity&lt;/th&gt;
  &lt;th &gt; Monday &lt;/th&gt;
  &lt;th&gt; Tuesday &lt;/th&gt;
  &lt;th&gt; Wednesday&lt;/th&gt;
  &lt;th&gt; Thursday &lt;/th&gt;
  &lt;th&gt; Friday &lt;/th&gt;
  &lt;th&gt; Saturday &lt;/th&gt;
  &lt;th&gt; Sunday &lt;/th&gt;
  &lt;th&gt;No. Of Hrs&lt;/th&gt;
  
    &lt;/tr&gt;
  
     &lt;tr&gt;
  &lt;td&gt;&lt;INPUT type="checkbox" name="chk"/&gt;&lt;/td&gt;
       &lt;td&gt;&lt;select name="project"&gt;
      &lt;option value="A"&gt;A&lt;/option&gt;
      &lt;option value="B"&gt;B&lt;/option&gt;
  
      &lt;/select&gt;&lt;/td&gt;
  &lt;td&gt;&lt;select name="reports"&gt;
      &lt;option value="Resource Pyramid"&gt;Resource Pyramid&lt;/option&gt;
      &lt;option value="Migration"&gt;Migration&lt;/option&gt;
      &lt;option value="Timesheet Form"&gt;Timesheet Form&lt;/option&gt;
      &lt;option value="Timesheet Report"&gt;Timesheet Report&lt;/option&gt;
      &lt;/select&gt;&lt;/td&gt;
  &lt;td&gt; &lt;select name="activity"&gt;
      &lt;option value="Development"&gt;Development&lt;/option&gt;
      &lt;option value="Testing"&gt;Testing&lt;/option&gt;
      &lt;option value="Research"&gt;Research&lt;/option&gt;
      &lt;option value="Data Upload"&gt;Data Upload&lt;/option&gt;
      &lt;option value="Design Review"&gt;Design Review&lt;/option&gt;
      &lt;option value="Documentation"&gt;Documentation&lt;/option&gt;
      &lt;option value="Leave"&gt;Leave&lt;/option&gt;
      &lt;option value="Holiday"&gt;Holiday&lt;/option&gt;
      &lt;option value="Pre Sales"&gt;Pre Sales&lt;/option&gt;
      &lt;option value="Installation"&gt;Installation&lt;/option&gt;
      &lt;option value="Miscellaneous"&gt;Miscellaneous&lt;/option&gt;
      &lt;option value="Back Up"&gt;Back Up&lt;/option&gt;
      &lt;option value="Migration"&gt;Migration&lt;/option&gt;
      &lt;option value="Defect Fixing"&gt;Defect Fixing&lt;/option&gt;
      &lt;/select&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input type="number"  name="mon" value="0" onchange="doAdd(this)"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="number" name="tue" value="0" onchange="doAdd(this)"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="number" name="wed" value="0" onchange="doAdd(this)"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="number" name="thur" value="0" onchange="doAdd(this)"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="number" name="fri" value="0" onchange="doAdd(this)"&gt;&lt;/td&gt;
       &lt;td&gt;&lt;input type="number" name="sat" value="0" onchange="doAdd(this)"&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input type="number" name="sun" value="0" onchange="doAdd(this)"&gt;&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
  &lt;/tr&gt;
       &lt;/table&gt;
  
     &lt;INPUT type="button" value="Add Row" onclick="addRow('dataTable')" name="addrow" /&gt;
     &lt;INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" /&gt;
     &lt;input type="button" name="save" value="Save"&gt;
     &lt;input type="Submit" name="submit" value="Submit" &gt;
  
     &lt;/form&gt;
     &lt;/body&gt;
     &lt;/html&gt;
  </code></pre>
  
  <p>And My Servlet Code is:</p>
  
  <pre><code> public class LoginServlet extends HttpServlet {
  
     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
  
        if (request.getParameter("submit") != null) {
  
  
  
          // Invoke FirstServlet's job here.
  
          response.setContentType("text/html");
          String id=request.getParameter("eid");
          String name=request.getParameter("ename");
  
          String fromdate=request.getParameter("fromdate");
          String todate=request.getParameter("todate");
          String project=request.getParameter("project");
          String reports=request.getParameter("reports");
          String activity=request.getParameter("activity");
          String mon=request.getParameter("mon");
          String tue=request.getParameter("tue");
          String wed=request.getParameter("wed");
          String thur=request.getParameter("thur");
          String fri=request.getParameter("fri");
          String sat=request.getParameter("sat");
          String sun=request.getParameter("sun"); 
  
          try
          {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con =    DriverManager.getConnection("jdbc:mysql://localhost:3306/timeline","root", "root");
              String stringDate ="01-01-2012 ";
              String stringDate1 ="02-01-2012 ";
              SimpleDateFormat sdf= new SimpleDateFormat("mm-dd-yyyy");
              SimpleDateFormat sdf1= new SimpleDateFormat("mm-dd-yyyy");
              java.util.Date uDate = sdf.parse(stringDate);
              java.util.Date uDate1 = sdf1.parse(stringDate1);
              java.sql.Date sqlDate = new java.sql.Date(uDate.getDate());
              java.sql.Date sqlDate1 = new java.sql.Date(uDate1.getDate());
              String sql="insert into employeeform(id,name,fromdate,todate,project,reports,activity,mon,tue,wed,thur,fri,sat,sun)value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
              PreparedStatement stmt=con.prepareStatement(sql);
  
              stmt.setString(1, id);
              stmt.setString(2,name);
              stmt.setString(3,fromdate);
              stmt.setString(4,todate);
              stmt.setString(5,project);
              stmt.setString(6,reports);
              stmt.setString(7, activity);
              stmt.setString(8,mon);
              stmt.setString(9,tue);
              stmt.setString(10,wed);
              stmt.setString(11, thur);
              stmt.setString(12, fri);
              stmt.setString(13,sat);
              stmt.setString(14,sun);
              stmt.executeUpdate();
              System.out.println("inserted successfully");
  
              response.sendRedirect("success.jsp"); 
  
          }
          catch(Exception e)
          {
              System.out.println(e);
              response.sendRedirect("error.jsp"); 
  
  
  
          }
  
  
      } else if (request.getParameter("save") != null) {
          // Invoke SecondServlet's job here.
          response.setContentType("text/html");
          String id=request.getParameter("eid");
          String name=request.getParameter("ename");
          String fromdate=request.getParameter("fromdate");
          String todate=request.getParameter("todate");
          String project=request.getParameter("project");
          String reports=request.getParameter("reports");
          String activity=request.getParameter("activity");
          String mon=request.getParameter("mon");
          String tue=request.getParameter("tue");
          String wed=request.getParameter("wed");
          String thur=request.getParameter("thur");
          String fri=request.getParameter("fri");
          String sat=request.getParameter("sat");
          String sun=request.getParameter("sun");
          String msg=null;
          try
          {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/timeline","root", "root");
              String sql="insert into partialentered(id,name,fromdate,todate,project,reports,activity,mon,tue,wed,thur,fri,sat,sun)value('"+id+"','"+name+"','"+fromdate+"','"+todate+"','"+project+"','"+reports+"','"+activity+"','"+mon+"','"+tue+"','"+wed+"','"+thur+"','"+fri+"','"+sat+"','"+sun+"')";
              Statement stmt=con.createStatement();
              int rs=stmt.executeUpdate(sql);
              System.out.println("inserted successfully");
  
  
          }
          catch(Exception e)
          {
              System.out.println(e);
          }
  
  
  
      }
  }
  
  }
  </code></pre>
  
  <p>While submitting the form it tells inserted successfully,but it stores only 1 row details.Please help me how can I change this servlet code such that i can insert the dynamic add row values also into the database.</p>
  
  <p>Thanks in advance.</p>
  
  <p>U Can view the output with the jfiddle: <a href="http://jsfiddle.net/UnuaX/" rel="nofollow">http://jsfiddle.net/UnuaX/</a>
  Where when I Use Add row Button and insert the values to the newly added row,it will not insert those details to the database.</p>