我们需要的是实现分页效果和模糊查询的分页效果
我们需要要在Dao方法里面写一个执行代码代码如下
public List<News> getAll(int pageindex,int pageSize){
List<News> ls=new ArrayList<News>();
int a =(pageindex-1)*pageSize+1;
int b =pageindex*pageSize;
try {
con=DBHelper.getCon();//创建连接
String sql="select * from (\r\n" +
" select a.*, rownum as rid from(\r\n" +
" select nid,ntitle,nauthor from news279 order by tid desc\r\n" +
" ) a\r\n" +
") b where b.rid between ? and ?";//定义sql语句
ps=con.prepareStatement(sql);//获得执行对象
//给占位符赋值
ps.setInt(1, a);
ps.setInt(2, b);
rs=ps.executeQuery();//获得结果集
//遍历结果集
while(rs.next()) {
//实例化一个新闻对象
News n=new News();
//给对象赋值 set
n.setNid(rs.getInt(1));
n.setNtitle(rs.getString(2));
n.setNauthor(rs.getString(3));
//加到集合中 勿忘
ls.add(n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
而实现模糊查询的分页效果
public List<News> getAll(int pageindex,int pageSize,String title){
List<News> ls=new ArrayList<News>();
int a =(pageindex-1)*pageSize+1;
int b =pageindex*pageSize;
try {
con=DBHelper.getCon();//创建连接
String sql="select * from (\r\n" +
" select a.*, rownum as rid from(\r\n" +
" select nid,ntitle,nauthor from news279 where ntitle like '%"+title+"%' order by tid desc\r\n" +
" ) a\r\n" +
") b where b.rid between ? and ?";//定义sql语句
ps=con.prepareStatement(sql);//获得执行对象
//给占位符赋值
ps.setInt(1, a);
ps.setInt(2, b);
rs=ps.executeQuery();//获得结果集
//遍历结果集
while(rs.next()) {
//实例化一个新闻对象
News n=new News();
//给对象赋值 set
n.setNid(rs.getInt(1));
n.setNtitle(rs.getString(2));
n.setNauthor(rs.getString(3));
//加到集合中 勿忘
ls.add(n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
我们要在排序之前模糊查询