一、项目结构
bean实体包,dao数据库操作包,webServlet包,utils工具包。
二、项目实现
1.实体:Ninja1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63package pers.zx.bean;
public class Ninja {
private Integer NinjaId;
private String NinjaName;
private String NinjaSex;
private Integer NinjaAge;
private String NinjaPassword;
private String NinjaLevel;
public Ninja() {
// TODO Auto-generated constructor stub
}
public Ninja(Integer ninjaId, String ninjaName, String ninjaSex, Integer ninjaAge,
String ninjaLevel) {
NinjaId = ninjaId;
NinjaName = ninjaName;
NinjaSex = ninjaSex;
NinjaAge = ninjaAge;
NinjaLevel = ninjaLevel;
}
public String getNinjaName() {
return NinjaName;
}
public void setNinjaName(String ninjaName) {
NinjaName = ninjaName;
}
public String getNinjaSex() {
return NinjaSex;
}
public void setNinjaSex(String ninjaSex) {
NinjaSex = ninjaSex;
}
public String getNinjaPassword() {
return NinjaPassword;
}
public void setNinjaPassword(String ninjaPassword) {
NinjaPassword = ninjaPassword;
}
public String getNinjaLevel() {
return NinjaLevel;
}
public void setNinjaLevel(String ninjaLevel) {
NinjaLevel = ninjaLevel;
}
public Integer getNinjaAge() {
return NinjaAge;
}
public void setNinjaAge(Integer ninjaAge) {
NinjaAge = ninjaAge;
}
public Integer getNinjaId() {
return NinjaId;
}
public void setNinjaId(Integer ninjaId) {
NinjaId = ninjaId;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return NinjaId+" "+NinjaName+" "+NinjaSex+" "+NinjaAge+" "+NinjaLevel;
}
}
2.Utils:数据库链接类以及关闭类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60package pers.zx.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcConnection {
// 获取数据库连接
public static Connection getConnection() {
String url="jdbc:mysql://localhost:3306/h5_sql?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8";
String user="****";
String password ="****";
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
#########################################################################
package pers.zx.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcClose {
protected Connection conn;
protected PreparedStatement ps;
protected ResultSet rs;
public void close(){
// 关闭资源
try {
if (rs!=null) {
rs.close();
}
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3.dao层:数据库相关操作1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93package pers.zx.dao;
import java.util.List;
import pers.zx.bean.Ninja;
public interface NinjaDao {
//根据每一页的条数 得到总页数
public int pages(int rows);
//根据所要查询的条数返回实体,从start开始查,查size条
public List<Ninja> ninja(int start,int size);
}
#########################################################################
package pers.zx.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import pers.zx.bean.Ninja;
import pers.zx.utils.JdbcClose;
import pers.zx.utils.JdbcConnection;
public class NinjaDaoImpl extends JdbcClose implements NinjaDao {
protected Connection conn;
protected PreparedStatement ps;
protected ResultSet rs;
@Override
public int pages(int rows) {
// TODO Auto-generated method stub
int num=0;
int page=0;//页数
conn=JdbcConnection.getConnection();
String sql="select COUNT(*) from ninja";
try {
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
num=rs.getInt(1);//获取总数
}
if(num%rows==0) {
page=num/rows;
}else {
page=num/rows+1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
super.close();//关闭资源
}
return page;
}
@Override
public List<Ninja> ninja(int start, int size) {
// TODO Auto-generated method stub
List<Ninja> list=new ArrayList<Ninja>();//创建集合
conn=JdbcConnection.getConnection();
String sql = "SELECT NinjaId,NinjaName,NinjaAge,NinjaSex,NinjaLevel FROM ninja LIMIT ?,?";//limit限制从start开始取size
try {
ps=conn.prepareStatement(sql);
//设置相关参数,在数据库中从start开始取size个
ps.setInt(1, start);
ps.setInt(2,size);
rs = ps.executeQuery();
while(rs.next()){
Ninja ninja = new Ninja();
ninja.setNinjaId(rs.getInt("NinjaId"));
ninja.setNinjaName(rs.getString("NinjaName"));
ninja.setNinjaAge(rs.getInt("NinjaAge"));
ninja.setNinjaSex(rs.getString("NinjaSex"));
ninja.setNinjaLevel(rs.getString("NinjaLevel"));
list.add(ninja);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
super.close();
}
return list;
}
}
4.web层Servlet实现1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65package pers.zx.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pers.zx.dao.NinjaDao;
import pers.zx.dao.NinjaDaoImpl;
/**
* Servlet implementation class PageServlet
*/
@WebServlet("/PageServlet")
public class PageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PageServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int rows=5;//一页显示条信息
int pageNos;//页号
int countPage;
NinjaDao list=new NinjaDaoImpl();
countPage=list.pages(rows);//总页数
HttpSession session = request.getSession(); // 将数据存到session中以便于在前台获取
session.setAttribute("countPage", countPage);
if (request.getParameter("pageNos") == null|| Integer.parseInt(request.getParameter("pageNos"))<1) {
pageNos = 1;
session.setAttribute("NinjaList", list.ninja(0, rows));
//当前页数
session.setAttribute("pageNos", pageNos);
} else {
pageNos = Integer.parseInt(request.getParameter("pageNos"));
session.setAttribute("NinjaList",list.ninja((pageNos-1)*rows, rows));
//当前页数
session.setAttribute("pageNos", pageNos);
}
request.getRequestDispatcher("page.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
5.page.jsp1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61<%@ 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 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>
<body>
<form action="PageServlet">
<center>
<table>
<tr>
<th>NinjaId</th>
<th>NinjaName</th>
<th>NinjaAge</th>
<th>NinjaSex</th>
<th>NinjaLevel</th>
</tr>
<c:forEach items="${NinjaList}" var="ninja" >
<tr>
<td>${ninja.ninjaId }</td>
<td>${ninja.ninjaName }</td>
<td>${ninja.ninjaAge}</td>
<td>${ninja.ninjaSex}</td>
<td>${ninja.ninjaLevel}</td>
</tr>
</c:forEach>
</table>
</center>
<center>
<a href="PageServlet?pageNos=1" >首页</a>
<c:choose>
<c:when test="${pageNos==1}">
<a href="PageServlet?pageNos=${pageNos}">上一页</a>
</c:when>
<c:otherwise>
<a href="PageServlet?pageNos=${pageNos-1}">上一页</a>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${pageNos==countPage}">
<a href="PageServlet?pageNos=${countPage }">下一页</a>
</c:when>
<c:otherwise>
<a href="PageServlet?pageNos=${pageNos+1 }">下一页</a>
</c:otherwise>
</c:choose>
<a href="PageServlet?pageNos=${countPage }">末页</a>
</center>
<h4 align="center">共${countPage}页
<input type="text" value="${pageNos}" name="pageNos" size="1">页
<input type="submit" value="go">
</h4>
</form>
</body>
</html>
6.index.jsp1
2
3
4
5
6
7
8
9
10
11
12<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% response.sendRedirect("PageServlet"); %>
</body>
</html>
7.web.xml1
2
3
4
5
6
7<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Page</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
三、结果显示
四、总结
算是初次尝试分页,通过网上的代码,自己稍作修改,实现了一个简单的分页web程序。还需很多改进,比如:
1.应该把分页操作专门做一个工具类而不是放在dao层
2.还有很多细节要处理,让其复用性更好
总之,通过这次尝试感觉还是很有收获的。
注:
- 如有不正确还请见谅。
- 如需代码请访问我的github
- 另外,我做了一些关于Spring、Struts、Hibernate的小应用可以帮助理解这些框架,如需代码请访问我的Github:https://github.com/Zxnaruto