创建一个简单的javaweb项目能实现分页功能,在jsp页面中有一个表格显示studentID,sNum,name,gender,age,tel这六种数据,这六种数据是从数据库student中的students表获取的数据,需要创建Students类,在同一个jsp页面下方要显示页码,每页显示8条数据
时间: 2024-01-22 22:17:32 浏览: 88
用JSP进行数据分页显示的一个实现.rar_java 分页_java 分页显示_jsp 显示数据_jsp数据分页_分页显示
首先,需要在项目中引入JDBC驱动包,以便于连接数据库。可以在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
```
接着,创建一个Students类,用来存储从数据库中获取的学生信息:
```java
public class Students {
private String studentID;
private String sNum;
private String name;
private String gender;
private int age;
private String tel;
// getter and setter methods
}
```
然后,在JSP页面中,先通过JDBC连接数据库,并查询出总共的数据条数。根据每页显示8条数据,可以计算出总共需要显示多少页。
```jsp
<%
// JDBC连接数据库
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false";
String username = "root";
String password = "root";
conn = DriverManager.getConnection(url, username, password);
// 查询总共的数据条数
String countSql = "SELECT COUNT(*) FROM students";
pstmt = conn.prepareStatement(countSql);
rs = pstmt.executeQuery();
int totalCount = 0;
if (rs.next()) {
totalCount = rs.getInt(1);
}
int pageCount = (totalCount + 7) / 8; // 计算总共需要显示多少页
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭JDBC连接
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
```
接着,根据当前页数和每页显示的数据条数,从数据库中查询出需要显示的数据,并将它们存储在一个List中。
```jsp
<%
int currentPage = 1;
if (request.getParameter("page") != null) {
currentPage = Integer.parseInt(request.getParameter("page"));
}
int start = (currentPage - 1) * 8; // 计算从数据库中哪个位置开始获取数据
// 查询需要显示的数据
String querySql = "SELECT * FROM students LIMIT ?, 8";
List<Students> studentsList = new ArrayList<>();
try {
conn = DriverManager.getConnection(url, username, password);
pstmt = conn.prepareStatement(querySql);
pstmt.setInt(1, start);
rs = pstmt.executeQuery();
while (rs.next()) {
Students students = new Students();
students.setStudentID(rs.getString("studentID"));
students.setsNum(rs.getString("sNum"));
students.setName(rs.getString("name"));
students.setGender(rs.getString("gender"));
students.setAge(rs.getInt("age"));
students.setTel(rs.getString("tel"));
studentsList.add(students);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭JDBC连接
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
```
最后,在JSP页面中,可以使用JSTL标签库来方便地显示数据和页码。以下是完整的JSP代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>分页功能示例</title>
</head>
<body>
<table border="1">
<tr>
<th>学号</th>
<th>学籍号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>电话</th>
</tr>
<c:forEach items="${studentsList}" var="students">
<tr>
<td><c:out value="${students.studentID}" /></td>
<td><c:out value="${students.sNum}" /></td>
<td><c:out value="${students.name}" /></td>
<td><c:out value="${students.gender}" /></td>
<td><c:out value="${students.age}" /></td>
<td><c:out value="${students.tel}" /></td>
</tr>
</c:forEach>
</table>
<c:if test="${pageCount > 1}">
<div>
<%-- 显示页码 --%>
<c:choose>
<c:when test="${currentPage == 1}">
首页 上一页
</c:when>
<c:otherwise>
<a href="?page=1">首页</a> <a href="?page=${currentPage - 1}">上一页</a>
</c:otherwise>
</c:choose>
<c:forEach begin="1" end="${pageCount}" var="i">
<c:choose>
<c:when test="${i == currentPage}">
${i}
</c:when>
<c:otherwise>
<a href="?page=${i}">${i}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${currentPage == pageCount}">
下一页 尾页
</c:when>
<c:otherwise>
<a href="?page=${currentPage + 1}">下一页</a> <a href="?page=${pageCount}">尾页</a>
</c:otherwise>
</c:choose>
</div>
</c:if>
</body>
</html>
```
阅读全文