<%--
Created by IntelliJ IDEA.
User: luffy
Date: 2021/8/3
Time: 23:49
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<!-- 连接数据库必须将“java.sql”导入 -->
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询所有的系统</title>
</head>
<script type="text/javascript">
function deleteRow(r)
{
var i=r.parentNode.parentNode.rowIndex;
document.getElementById('myTable').deleteRow(i);
}
</script>
<body>
<center>
<a href=>添加</a>
<table id='myTable' border="1" align="center" width="50%">
<tr>
<th>id</th>
<th>账号</th>
<th>密码</th>
<th>功能</th>
</tr>
<!-- 注意:<% %>中的是脚本语言 -->
<%
Class.forName("com.mysql.cj.jdbc.Driver");
//加载驱动
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbc?characterEncoding=utf-8","root","123456");
//上面两句是与数据库建立连接
Statement stmt=conn.createStatement();
//stmt拥有执行sql语句的方法
String sql="select * from student";
ResultSet rs=stmt.executeQuery(sql);
//执行s语句,得到的rs指向所查数据的开头
//下述语句是将查到的数据展示到页面,用到html内容要用
//out.println("<table align='center' border='1px' cellpadding='20' cellspacing='0' class='a'>"+"<tr><th> id:</th> <th> 账号:</th> <th> 密码:</th> <th>功能: </th> </tr> </table> ");
Map map = new HashMap();
while(rs.next()) {
int a =Integer.valueOf( rs.getInt(1));
String b = rs.getString(2);
String c = rs.getString(3);
map.put("in",a);
map.put("name",b);
map.put("pass",c);
for(int j=0;j<=0; j++){
out.print("<tr>"+
"<td>"+ map.get("in")+"</td> " +
"<td>"+ map.get("name")+"</td> " +
"<td>"+ map.get("pass")+"</td> " +
"<td><input type='button' value='Delete' onclick='deleteRow(this)'> </td>"+
"</tr>");
}
out.print(" </tr> "+"<br>");
//数据库中存的id不需要展示,但在其修改和删除的链接中要用
}
out.print("</table>");
//由内向外一层层关闭
rs.close();
stmt.close();
conn.close();
%>
</table>
</center>
</body>
</html>
```sql
//数据库
CREATE TABLE `student` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`password` varchar(40) NOT NULL,
PRIMARY KEY (`id`)
) ;
insert into student()VALUES(1,'123','123');
select *from student;
```
js删除只是把页面渲染的数据删除了,也就是把展示的数据删除了,并没有给数据库发送删除原始数据请求,所以数据库中的数据没有删除,如果需要删除数据库中的数据,你需要再写一个删除数据库数据的脚本,这样就可以删除页面数据的同时也删除数据库中的数据了
那怎么通过js拿到删除的那个id
```java
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="test3.Student" %><%--
Created by IntelliJ IDEA.
User: luffy
Date: 2021/8/6
Time: 16:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>js删除jsp</title>
</head>
<body>
<%
Student s = new Student();
Class.forName("com.mysql.cj.jdbc.Driver");
//加载驱动
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/dbc?characterEncoding=utf-8","root","123456");
//上面两句是与数据库建立连接
String sql="delect from student where id= ?";
//放入数据
PreparedStatement prepared = null;
prepared.setInt(1,s.getId());
int count = prepared.executeUpdate();
// System.out.println("影响的函数有" +count);
if(count >0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
prepared.close();
conn.close();
// int id = Integer.valueOf(request.getParameter("id"));
%>
</body>
</html>
```