Javascript 需要从数据库中删除记录,并使用jsp中的复选框提醒已删除的ID
我试图使用jsp页面中的复选框删除数据库中的记录。我不熟悉JSP和前端,因此无法正确地调试并找出实际应该如何进行调试Javascript 需要从数据库中删除记录,并使用jsp中的复选框提醒已删除的ID,javascript,jsp,dom-events,Javascript,Jsp,Dom Events,我试图使用jsp页面中的复选框删除数据库中的记录。我不熟悉JSP和前端,因此无法正确地调试并找出实际应该如何进行调试 <%@page import="java.sql.*"%> <form name=myname > <table border="1"> <tr><td></td><a href="#" onclick='deleteElement();'&
<%@page import="java.sql.*"%>
<form name=myname >
<table border="1">
<tr><td></td><a href="#" onclick='deleteElement();'">Delete</a>
<td><b><u>bookid</u></b></td>
<td><b><u>Author</u></b></td>
<td><b><u>title</u></b></td>
</tr>
<%try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
ResultSet rs = null;
Statement st=null;
st=conn.createStatement();
rs = st.executeQuery("select * from book");
int i=0; while(rs.next()){ %>
<tr><td><input type="checkbox" name="check<%=i%>" value=<%= rs.getString("bookId") %>></td>
<td><%= rs.getString("bookId") %></td>
<td><%= rs.getString("author") %></td>
<td><%= rs.getString("title") %></td>
</tr><%
i++;
}
}catch(SQLException e){ System.out.println(e.getMessage()); } %>
</table>
</form>
<script LANGUAGE="JAVASCRIPT">
function deleteElement(){
alert("hello");
<%String id[]= new String[10];
for(int i=0;i<10;i++){
id[i]=request.getParameter("check"+i);
}
%>
<%try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
Statement st=null;
st=conn.createStatement();
for(int a=0;a<10;a++){
st.executeUpdate("delete from book where bookid='"+id[a]+"'");
}
}catch(SQLException e){
System.out.println(e.getMessage());
}
%>
}
</script>
书号
作者
标题
函数deleteElement(){
警惕(“你好”);
JSP中有scriptlet代码,这是一个非常糟糕的主意
我建议学习JSTL,让JSP与servlet通信。servlet将绑定和验证输入参数,并与为您执行数据库操作的对象交互
这种安排的好处在于,它自然地将您的问题分解为多个部分。可以对数据库进行编码、调试、测试和搁置