我正在尝试使用 jsp 页面中的复选框删除数据库中的记录。我是 JSP 和前端的新手,所以无法正确调试并找出我应该如何实际操作。
<%@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>