1.建立页面
2 DbUtil.class
package com;
import java.sql.*;
public class DbUtil {
private String driver="com.mysql.cj.jdbc.Driver";
private String url="jdbc:mysql://localhost:3306:3306/mm?useSSL=false&serverTimezone=PRC";
private String uid="root";
private String pwd="123456";
private String dbname="mm";
private String host="lcalhost";
private int port=3306;
private Connection con=null;
// 数据库连接
public Connection connection(){
Connection con=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/mm?user=root&password=123456&useSSL=false&serverTimezone=PRC");
this.con=con;
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
}
3 table.css
table {
text-align: center;
background-color: rgba(0, 0, 0, 0.05);
margin: 30px auto;
}
table,th,td {
border-collapse: collapse;
border: 1px solid #d5d5d5;
width: 600px;
height: 40px;
}
table tr th {
background-color: #e2e2e2;
}
table tr:hover {
background-color: #f8fffe;
}
a{
text-decoration: none;
color: #121212;
font-size: 16px;
}
4 css.css
table {
width: 350px;
height: 300px;
text-align: center;
font-size: 18px;
border: 1px solid #c5c5c5;
position: absolute;
top: 40%;
left: 40%;
padding-top: 10px;
padding-left: 10px;
background-color: rgba(0, 0, 0, 0.2);
border-radius: 5px;
box-shadow: 1px 1px 1px 1px;
}
.ad {
width: 100px;
height: 40px;
font-size: 16px;
border-radius: 5px;
box-shadow: 2px 2px 2px #898989;
line-height: 30px;
}
input {
width: 200px;
height: 30px;
border-radius: 5px;
}
5 index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
<link rel="stylesheet" href="css.css">
</head>
<body>
<form method="post" name="form1">
<table>
<tr>
<td>用户:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td colspan="2">
<input class="ad" type="submit" name="register" value="注册" formaction="register.html">
<input class="ad" type="submit" name="login" value="登录" formaction="login.jsp">
</tr>
</table>
</form>
</body>
</html>
6 register.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
<link rel="stylesheet" href="css.css">
</head>
<body>
<form action="saveReg.jsp" method="post">
<table>
<tr>
<td colspan="2" valign="left"><h3>用户注册</h3> </td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="pwd" ></td>
</tr>
<tr>
<td colspan="2">
<input class="ad" type="submit" name="save" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
7 savaReg.jsp
<%@ page import="com.DbUtil" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
DbUtil db=new DbUtil();
PreparedStatement pst=db.connection().prepareStatement("insert into users values (null,?,?)");
pst.setString(1,request.getParameter("name"));
pst.setString(2,request.getParameter("pwd"));
pst.executeUpdate();
db.connection().close();
pst.close();
out.print("注册成功!");
response.sendRedirect("./");
%>
</body>
</html>
8 login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
<link rel="stylesheet" href="css.css">
</head>
<body>
<form method="post" name="form1">
<table>
<tr>
<td>用户:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td colspan="2">
<input class="ad" type="submit" name="register" value="注册" formaction="register.html">
<input class="ad" type="submit" name="login" value="登录" formaction="login.jsp">
</tr>
</table>
</form>
</body>
</html>
9 show.jsp
<%@ page import="com.DbUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="table.css">
</head>
<body>
<table >
<tr>
<th>编码</th>
<th>姓名</th>
<th>密码</th>
<th>基本操作</th>
</tr>
<%
request.setCharacterEncoding("utf-8");
DbUtil db=new DbUtil();
Connection con=db.connection();
if(request.getParameter("id")!=null){
PreparedStatement dpt=con.prepareStatement("delete from users where id=?");
dpt.setInt(1,Integer.parseInt(request.getParameter("id")));
dpt.executeUpdate();
dpt.close();
}
PreparedStatement pst=con.prepareStatement("select * from users");
ResultSet rs=pst.executeQuery();
while(rs.next()){ %>
<tr>
<td><%=rs.getInt("id")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getString("pwd")%></td>
<td >
<%-- id 传值? 将数据库中id的值传入到本页面,然后通过request 获取id ,从而删除id这条信息--%>
<a href="?id=<%=rs.getInt("id")%>"> <button>删除</button></a>
<%--将id 传入update.jsp页面,通过id找到需要编辑的信息--%>
<a href="update.jsp?id=<%=rs.getInt("id")%>"><button>编辑</button></a>
</td>
</tr>
<%}%>
</table>
</body>
</html>
10 update.jsp
<%@ page import="com.DbUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改</title>
<link rel="stylesheet" href="css.css">
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
DbUtil db=new DbUtil();
Connection con= db.connection();
PreparedStatement pst=con.prepareStatement("select * from users where id=?");
pst.setInt(1,Integer.parseInt(request.getParameter("id")));
out.print(request.getParameter("id"));
ResultSet rs=pst.executeQuery();
rs.next();
%>
<form action="save.jsp" method="post">
<table>
<tr>
<td></td>
<td><input type="hidden" name="id" value="<%=rs.getInt("id")%>"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="pwd" value="<%=rs.getString("pwd")%>"></td>
</tr>
<tr>
<td colspan="2">
<input class="ad" type="submit" name="save" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
11 save.jsp
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="com.DbUtil" %>
<%@ page import="java.sql.Connection" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改后的数据保存到数据库中</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
int id=Integer.parseInt(request.getParameter("id"));
DbUtil db=new DbUtil();
Connection con=db.connection();
PreparedStatement pst =con.prepareStatement("update users set name=?,pwd=?where id=?");
pst.setString(1,name);
pst.setString(2,pwd);
pst.setInt(3,id);
pst.executeUpdate();
con.close();
pst.close();
response.sendRedirect("index.jsp");
%>
</body>
</html>