web之新闻数据分页
如何实现分页操作?
page 当前页数 1
rows 当前显示条数 5
【page:1,rows:5】 1-5
【page:2,rows:5】 6-10
【page:3,rows:5】 11-15
开始位置 :1+((页数-1)*条数)
结束位置:条数*页数
分页语句怎么写呢?
错误语句:
因为是根据新闻id来做判断的,如果数据删除,界面就会没有数据
查第1~5条
select * from t_news where news_id between 1 and 5;
查第6~10条
select * t_news where news_id between 6 and 10;
正确语句:
根据列的顺序,伪列:rownum,如果删除一行会自动把序号补上
select a.*,rownum from t_news a;

查第1~5条
select a.* from _news a where rownum between 1 and 5;
但是查6~10条会发现没有数据,那我们得说一下伪列rownum的特性 :
不能用于大于1,
伪列是先搞好,再排序的
我们可以将伪列变成实列
select * from(select a.*,rownum myr from t_news a where news_title like '%%'
)b where myr between 6 and 10
在web中实现分页功能
第一步:编写dao包
需要接收一个名字(模糊查询)和序号(进行分页)
/*** 根据新闻名称进行模糊查询* @param newName 新闻的名字* @param page 让别人传入的页面数* @return*/public List queryByName(String newName,int page) {int rows=5;//条数int begin=1+((page-1)*rows);//开始位置int end=page*rows;//结束位置List list = new ArrayList();try {con = DBHelper.getCon();ps = con.prepareStatement("select * from (" +" select a.*,ROWNUM myr from t_news 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));news.setNewsPublisher(rs.getString(5));news.setNewsContent(rs.getString(6));news.setNewsCover(rs.getString(7));news.setNewsCount(rs.getInt(8));news.setNewsMarker(rs.getInt(9));
// 将新闻对象添加到集合中list.add(news);}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(con, ps, rs);}return list;}
第二步:在界面中调用
接收数据(页数)
//设定当前用户进行分页的名字叫page
String index = request.getParameter("page");
int n = 1;//默认第一页
if (index != null) {n = Integer.parseInt(index);//设置为携带的页数
}
记得在遍历数据的时候把页数传入
for (News news : new NewsDao().queryByName(newName, n)) {
第三步:按钮的实现

怎么知道当前有几页?
数据库的数量/条数
dao包:
/**
* 查询当前与传入名称相应的总页数
* @param newName
* @return
*/public int queryCount(String newName) {int rows=5;//条数int count=0;//数据库里面数据的数量try {con = DBHelper.getCon();ps = con.prepareStatement("select count(1) from t_news where news_title like ? ");ps.setString(1, "%" + newName + "%");rs = ps.executeQuery();if (rs.next()) {count= rs.getInt(1);//数据库的条数}//返回页数13.0/5=2.x->3.0 向上求整// return Integer.parseInt(Math.ceil(count*1.0/rows)+"");//求整// Double中有一个自动的拆箱,可以将Double类型变成int类型return new Double(Math.ceil(count*1.0/rows)).intValue();} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(con, ps, rs);}return 0;}
查询当前数据的对应页数(首页):整个数据最大页数
int maxPage = new NewsDao().queryCount(newName);
根据最大页码 动态生成相应个数的页面(首页)
众所周知一般查询查询的名字都会在输入框上的,那么我们该怎么实现呢?

在首页把输入框的数据据进行设置( value="<%=newName %>"),需要进行判断哦——默认为空字符串,在用户查询的时候显示相应的名字
<%
//点击了表单之后 跳转的是当前这个页面 同时携带一个newName(查询的关键字)过来
String newName = request.getParameter("newName");
if (newName == null) {newName = "";//查询所有
}
%>
选中的页数显示
如果当前遍历的数字与当前的页数是相等的class="active",否则为空字符串
"><%=i %>

今天的内容就讲到这里啦,下次再见咯,拜拜。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
