用Java实现用户页面的增、删、改,完成3种提交操作,增,删,改,把握好数据流,从jsp到servlet,再到mysql,体验MVC的整体架构, 2.挑战任务:完成用户页面的查询操作
时间: 2023-06-18 18:07:45 浏览: 96
基于jsp+sevlet+mysql实现用户登陆和增删改查功能
5星 · 资源好评率100%
好的,对于用户页面的增删改查操作,我们可以采用MVC架构,具体实现流程如下:
1. 数据库设计
首先,我们需要设计数据库,这里以用户表为例,设计如下:
```
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
2. 实体类设计
接着,我们需要设计实体类,这里我们创建一个User类,包含id、name、age、gender四个属性,以及对应的get/set方法。
```
public class User {
private int id;
private String name;
private int age;
private String gender;
// 省略get/set方法
}
```
3. DAO层设计
接下来,我们需要设计DAO层,这里我们创建一个UserDao类,包含增删改查四个方法。
```
public class UserDao {
// 数据库连接
private Connection conn;
// 构造方法,获取数据库连接
public UserDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
} catch (Exception e) {
e.printStackTrace();
}
}
// 增加用户
public void addUser(User user) {
try {
String sql = "INSERT INTO user(name, age, gender) VALUES(?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setInt(2, user.getAge());
pstmt.setString(3, user.getGender());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
// 删除用户
public void deleteUser(int id) {
try {
String sql = "DELETE FROM user WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
// 修改用户
public void updateUser(User user) {
try {
String sql = "UPDATE user SET name=?, age=?, gender=? WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setInt(2, user.getAge());
pstmt.setString(3, user.getGender());
pstmt.setInt(4, user.getId());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
// 查询用户
public List<User> queryUser() {
List<User> userList = new ArrayList<User>();
try {
String sql = "SELECT * FROM user";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setGender(rs.getString("gender"));
userList.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return userList;
}
}
```
4. 控制器设计
接下来,我们需要设计控制器,这里我们创建一个UserServlet类,处理用户请求,并调用DAO层的方法进行增删改查操作。
```
public class UserServlet extends HttpServlet {
private UserDao userDao = new UserDao();
// 处理GET请求
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equals("add")) {
addUser(request, response);
} else if (action.equals("delete")) {
deleteUser(request, response);
} else if (action.equals("update")) {
updateUser(request, response);
} else if (action.equals("query")) {
queryUser(request, response);
}
}
// 处理POST请求
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
// 增加用户
private void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
User user = new User();
user.setName(name);
user.setAge(age);
user.setGender(gender);
userDao.addUser(user);
response.sendRedirect("user.jsp");
}
// 删除用户
private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
userDao.deleteUser(id);
response.sendRedirect("user.jsp");
}
// 修改用户
private void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
user.setGender(gender);
userDao.updateUser(user);
response.sendRedirect("user.jsp");
}
// 查询用户
private void queryUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> userList = userDao.queryUser();
request.setAttribute("userList", userList);
request.getRequestDispatcher("user.jsp").forward(request, response);
}
}
```
5. 视图设计
最后,我们需要设计用户界面,这里我们创建一个user.jsp页面,包含增删改查四个操作。
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户管理</title>
</head>
<body>
<h1>用户管理</h1>
<form action="UserServlet" method="post">
<input type="hidden" name="action" value="add">
<p>姓名:<input type="text" name="name"></p>
<p>年龄:<input type="text" name="age"></p>
<p>性别:<input type="text" name="gender"></p>
<p><input type="submit" value="增加"></p>
</form>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
<c:forEach var="user" items="${userList}">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.gender}</td>
<td>
<a href="UserServlet?action=delete&id=${user.id}">删除</a>
<a href="update.jsp?id=${user.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
以上就是用户页面的增删改查操作的实现流程。如果要实现查询操作,只需要在user.jsp页面中添加查询表单和查询按钮,并在UserServlet类中添加queryUser()方法即可。
阅读全文