有mysql数据库里有两条数据id:1,name:张三,money:0;id:2,name:李四,money:0;用java写一个User类里面有id,name,money,是标准javabeen;index.jsp:发送请求到UserListSerlet类;UserListSerlet类:调用UserService类的方法查询所有用户,将查询到的用户集合储存到request,跳转到list.jsp进行展示;UserDao类:能根据mysql数据库语句查询所有用户的信息创建一个 QueryRunner 对象,并将DruidUtil.getDataSource() 方法返回的 javax.sql.DataSource 对象作为参数传入 QueryRunner 的构造方法中,有getAllUsers()方法,里面调用queryRunner的query()方法,;UserService类:里面创建UserDao对象,有查询所有用户的getAllUsers()方法,getAllUsers()方法里调用UserDao的getAllUsers()方法,;list.jsp:使用el和jstl遍历展示;
时间: 2024-04-14 17:31:28 浏览: 135
sort-by-name.rar_site:www.pudn.com_sortbyname
5星 · 资源好评率100%
这是一个使用Java编写的简单的用户管理系统,其中涉及到了数据库操作、Servlet的使用以及JSP页面的展示。下面是相应的代码示例:
User.java(User类):
```java
public class User {
private int id;
private String name;
private int money;
// 构造方法、getters和setters省略
}
```
UserDao.java(数据访问层):
```java
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
public class UserDao {
private QueryRunner queryRunner;
public UserDao(DataSource dataSource) {
this.queryRunner = new QueryRunner(dataSource);
}
public List<User> getAllUsers() throws SQLException {
String sql = "SELECT * FROM user";
return queryRunner.query(sql, new BeanListHandler<>(User.class));
}
}
```
UserService.java(业务逻辑层):
```java
import java.sql.SQLException;
import java.util.List;
public class UserService {
private UserDao userDao;
public UserService() {
this.userDao = new UserDao(DruidUtil.getDataSource());
}
public List<User> getAllUsers() throws SQLException {
return userDao.getAllUsers();
}
}
```
UserListServlet.java(Servlet类):
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
public class UserListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserService userService = new UserService();
try {
List<User> userList = userService.getAllUsers();
request.setAttribute("userList", userList);
request.getRequestDispatcher("list.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
// 错误处理
}
}
}
```
index.jsp(请求入口):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Management</title>
</head>
<body>
<form action="UserListServlet" method="GET">
<input type="submit" value="Show User List">
</form>
</body>
</html>
```
list.jsp(用户列表展示页面):
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Money</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.money}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
以上是一个简单的用户管理系统的示例代码,通过访问index.jsp页面可以发送请求到UserListServlet类,该类会调用UserService类的方法查询所有用户,并将查询到的用户集合存储到request中,然后跳转到list.jsp页面进行展示。
阅读全文