web分页的实现

1.首先,建立sql数据库实现表的基本信息

2.导入mysql-connector-java-5.1.16-bin.jar

 

3.建立一个web项目,在src目录下创建domain包,在其包内创建News.java类,根据数据库信息,定义实体对象

	private Integer id;
	private String title;
	private String content;

构建其有参、无参方法,并生成get与set方法

 

4.在src目录下创建dao包,创建接口NewsDao类定义如下方法:

	/*查询总记录数*/
	Integer getCountRecord();
	/*查询当前页信息*/
	List<News> findNowPageInfo(Integer nowpage);
	/*查询所有新闻信息*/
	List<News> findAll();
	
	Integer getCountPage();


5.在包内创建NewsDaoImpl实现类并实现NewsDao接口:

/* 封装数据库操作的接口 */
	private static Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;

	/* 封装每页显示的记录数 */
	private static final Integer PAGESIZE = 10;
	// 总记录数
	private Integer countRecord;
	// 总页数
	public Integer countPage;

	private static final String URL = "jdbc:mysql://localhost:3306/3gjava?user=root&password=123&useUnicode=true&characterEncoding=UTF8";
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(URL);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Integer getCountRecord() {
		String sql = "select count(*) from news";
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				this.countRecord = rs.getInt(1);
				this.countPage= this.countRecord%this.PAGESIZE==0?this.countRecord/this.PAGESIZE:this.countRecord/this.PAGESIZE+1;
			}
			release(rs, pstmt);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.countRecord;
	}

	public List<News> findNowPageInfo(Integer nowPage) {
		List<News> entities = new ArrayList<News>();
		String sql = "select id,title,content from news limit ?,?";
		try {
			pstmt = conn.prepareStatement(sql);
			int index = 1;
			pstmt.setObject(index++, (nowPage - 1) * this.PAGESIZE);
			pstmt.setObject(index++, this.PAGESIZE);

			rs = pstmt.executeQuery();
			while (rs.next()) {
				News entity = new News();
				entity.setId(rs.getInt("id"));
				entity.setTitle(rs.getString("title"));
				entity.setContent(rs.getString("content"));

				entities.add(entity);
			}
			release(rs, pstmt);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return entities;
	}

	private void release(ResultSet rs, PreparedStatement pstmt) {
		if (rs != null)
			try {
				rs.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		if (pstmt != null)
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public static void main(String[] args) {
		NewsDaoImpl ndi = new NewsDaoImpl();
		// System.out.println(ndi.getCountRecord());
		List<News> entities = ndi.findNowPageInfo(1);
		for (News entity : entities) {
			System.out.println(entity.toString());
		}		
	}

	public Integer getCountPage() {
		return countPage;
	}

	public List<News> findAll() {
		List<News> entities=new ArrayList<News>();
		String sql="select id,title,content from paging";
		try {
			pstmt=conn.prepareStatement(sql);
			while(rs.next()){
				News entity=new News();
				entity.setId(rs.getInt("id"));
				entity.setContent(rs.getString("content"));
				entity.setTitle(rs.getString("title"));
				entities.add(entity);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return entities;
	}

 

6.在src目录下创建service包,创建NewService接口且继承NewsDao:

public interface NewsService extends NewsDao{
}

7.在service包内创建NewService实现类且实现NewService接口:

private NewsDao newsDao = new NewsDaoImpl();

	public Integer getCountRecord() {
		return newsDao.getCountRecord();
	}

	public List<News> findNowPageInfo(Integer nowpage) {
		return newsDao.findNowPageInfo(nowpage);
	}

	public List<News> findAll() {
		return newsDao.findAll();
	}

	public Integer getCountPage() {
		return newsDao.getCountPage();
	}


8.在src目录下创建servlet包且在其内创建NewsListServiet类并继承HttpServlet:

在doPost方法中调用doGet方法:

this.doGet(request, response);

后再doGet方法中实现如下代码:

// 获取当前页
		String npage = request.getParameter("nowpage");
		int nowpage = 1;

		// 业务对象
		NewsService newsService = new NewsServiceImpl();
		// 获取总记录
		Integer countRecord = newsService.getCountRecord();
		// 总页数
		Integer countPage = newsService.getCountPage();
		if ("".equals(npage) || npage == null) {
			nowpage = 1;
		} else {
			nowpage = Integer.parseInt(npage);
		}

		// 当前页面的记录信息
		List<News> entities = newsService.findNowPageInfo(nowpage);
		
		request.setAttribute("countRecord", countRecord);
		request.setAttribute("countPage", countPage);
		request.setAttribute("nowpage", nowpage);
		request.setAttribute("entities", entities);
		// 转发
		request.getRequestDispatcher("./index.jsp").forward(request, response);


 9.建立jsp实现分页功能 

首先导入标准标签库

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"  %>


 

实现主体代码

<body>
    <div>      
    <a href="${pageContext.request.contextPath}/news.do">查看所有新闻信息</a>
    	<div>
    		<table border="1px" cellpadding="0" cellspacing="0">
    			<thead>
    				<tr>
    					<th>序号</th>
    					<th>标题</th>
    					<th>内容</th>
    					<th>操作</th>
    				</tr>
    			</thead>
    			
    			<tbody>
						<c:forEach var="entity" items="${entities}">
						<tr>
							<td>
								${entity.id }
							</td>
							<td>
								${entity.title }
							</td>
							<td>
								${entity.content }
							</td>
							<td>
								<a href="#">edit</a>
								<a href="#">delete</a>
							</td>
						</tr>
						</c:forEach>
					</tbody>
    		</table>    		
    	</div>
    	<div>
    		<a href="${pageContext.request.contextPath}/news.do?nowpage=1">首页</a>    	
    		<a href="${pageContext.request.contextPath}/news.do?nowpage=${nowpage-1<=1?1:nowpage-1}">上页</a>    	
    		<a href="${pageContext.request.contextPath}/news.do?nowpage=${nowpage+1>=countPage?countPage:nowpage+1}">下页</a>    	
    		<a href="${pageContext.request.contextPath}/news.do?nowpage=${countPage}">末页</a>    	
    	</div>
    	    	<DIV>当前是第${nowpage}页,一共${countRecord }条信息,一共${countPage}页</DIV>
    </div> 
  </body>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值