今天来讲一讲新闻数据分页
首先,为什么要进行分页?
展示面板分页就是当你当前的内容放不下时,放到下一页继续进行展示,为了不让太多的信息累计在一起,影响页面美观,所以分页既方便用户查找相关信息,也可用更好的保持页面美观
下面进行分页实操
在实现分页功能之前,咱们可以先将主页(index.jsp)表单中的method=“post” 改为method=“get”,方便之后代码的操作,同时还要注释掉破碎重组的相关代码
优势:
能显示多条信息
减少一定的麻烦
分页操作:
page:当前页数 1
rows:当前显示条数 5
举例:
–【page:1,row:5】1-5
–【page:2,row:5】6-10
–【page:3,row:5】11-15
–【page:4,row:5】…
所以规律应该为:
开始位置 begin:1+(page-1)rows
结束位置 end:pagerows
下面看看分页代码吧
NewsDao.java:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.util.DBHelper;
import pojo.News;
public class NewsDao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
public List<News> queryByName(String newName,int page/*页数*/) {
//int page=1;//页数
int rows=5;//条数
int begin=1+((page-1)*rows);//开始位置
int end=page*rows;//结束位置
List<News> list=new ArrayList<News>();
try {
con=DBHelper.getCon();
ps=con.prepareStatement("select * from ("
+ "select a.*,ROWNUM myr from t_news02 a where news_title like ?"
+ ")b where myr between ? and ? ");
ps.setString(1, "%" + newName + "%");
ps.setInt(2, begin);
ps.setInt(3, end);
rs=ps.executeQuery();
while(rs.next()) {
News news=new News();
//给新闻对象赋值
news.setNewsId(rs.getInt(1));
news.setNewsTitle(rs.getString(2));
news.setNewsTopic(rs.getInt(3));
news.setNewsAuthor(rs.getString(4