【问题标题】:delete a row from table and database on clicking checkbox using jsp使用jsp单击复选框时从表和数据库中删除一行
【发布时间】:2013-10-24 09:31:51
【问题描述】:

这里的表id是从删除按钮传递过来的。但我无法从数据库和表中删除记录。代码如下:

function deleteRow(tableID) {        

        try {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;

        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null !== chkbox && true === chkbox.checked) {
                table.deleteRow(i);

                var did;

                id=request.getParameter("check"+i);
                out.println(did);
                }

                rowCount--;
                i--;
            }


        }
        catch(e) {
            alert(e);
        }
    }


  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:mysql://localhost/jspdatabase","root", "kca@fnpl#12");
 st=con.createStatement();

  st.executeUpdate("delete from table1 where id ='"+did+"'");

【问题讨论】:

  • 您遇到的错误是什么?
  • 控件没有传递给函数function deleteRow(tableID)
  • 虽然我没有在这里提供你的解决方案,但要指出一件事,我相信你必须使用 JDBC 4.0 或更高版本,所以尽量不要显式编写 Class.forName("com.mysql.jdbc 。司机”);另一件事使用 try-with-resource。

标签: jsp


【解决方案1】:

查看您的代码,在我看来您从未将变量“did”设置为任何值。

var did;
id=request.getParameter("check"+i);

应该是

var did;
did=request.getParameter("check"+i);

此外,您应该在函数外部(在最顶部)声明 var did,以便它仍然存在于函数外部。

【讨论】:

    猜你喜欢
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    相关资源
    最近更新 更多