如何在数据库中保存表数据

时间:2019-09-08 01:14:39

标签: mysql jsp servlets

我正在用jsp在我的表中输入数据,但是数据库中只保存了一行。当我在表中添加另一行时,它确实保存了,但是只保存了第一行。请帮助我如何解决我的问题。我是新来的。

这是我的jsp代码:

 <TABLE>
<tr>
    <th>Subject Code </th>
    <th>Description </th>
    <th>Lecture </th>
    <th>Lab </th>
    <th>Units </th>
    <th>Schedule </th>
    </tr>

    <TR>
        <TD><INPUT type="checkbox" name="chk"/></TD>
        <TD></TD>
        <TD> <INPUT type="text" name="subjCode" /> </TD>
        <TD> <INPUT type="text" name="description" /> </TD>
        <TD> <INPUT type="text" name="lec" /> </TD>
        <TD> <INPUT type="text" name="lab" /> </TD>
        <TD> <INPUT type="text" name="units" /> </TD>
        <TD> <INPUT type="text" name="sched" /> </TD>
    </TR>
</TABLE>

这是我为控制器编写的代码:

 try (PrintWriter out = response.getWriter()) {

        String subjCode = request.getParameter("subjCode");
        String description = request.getParameter("description");
        String lec = request.getParameter("lec");
        String lab = request.getParameter("lab");
        String units = request.getParameter("units");
        String sched = request.getParameter("sched");


        try
        {

                Class.forName("com.mysql.jdbc.Driver").newInstance();
                   Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","");
                Statement st= conn.createStatement();
                st.executeUpdate("insert into student (subjCode, description, lec, lab, units, sched) values('"+subjCode+"', '"+description+"', '"+lec+"', '"+lab+"', '"+units+"', '"+sched+"')");

        out.println("Data is inserted");    

        }

    }

当您要添加另一行时,这是我的js代码:

 <SCRIPT language="javascript">
        function addRow(tableID) {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "checkbox";
        element1.name="chkbox[]";
        cell1.appendChild(element1);

        var cell2 = row.insertCell(1);


        var cell3 = row.insertCell(2);
        var element2 = document.createElement("input");
        element2.type = "text";
        element2.name = "txtbox[]";
        cell3.appendChild(element2);

        var cell4 = row.insertCell(3);
        var element3 = document.createElement("input");
        element3.type = "text";
        element3.name = "txtbox[]";
        cell4.appendChild(element3);

        var cell5 = row.insertCell(4);
        var element4 = document.createElement("input");
        element4.type = "text";
        element4.name = "txtbox[]";
        cell5.appendChild(element4);


        var cell6 = row.insertCell(5);
        var element5 = document.createElement("input");
        element5.type = "text";
        element5.name = "txtbox[]";
        cell6.appendChild(element5);


        var cell7 = row.insertCell(6);
        var element6 = document.createElement("input");
        element6.type = "text";
        element6.name = "txtbox[]";
        cell7.appendChild(element6);

        var cell8 = row.insertCell(7);
        var element7 = document.createElement("input");
        element7.type = "text";
        element7.name = "txtbox[]";
        cell8.appendChild(element7);



    }

我希望所有行的数据都保存在数据库中。请在这件事上给予我帮助!我是新来的。

0 个答案:

没有答案