JAVA接口实现类:
package org.accp.jsppage.dao.Impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.accp.jsppage.dao.NewsDao;
import org.accp.jsppage.entity.News;
import org.accp.jsppage.util.BaseDao;
public class NewsDaoImpl implements NewsDao {
Connection con;
ResultSet rs;
PreparedStatement ps;
/*
* 查询当前页在x-t条
* cp第X页
*/
public List findPage(int cp, int ls) throws Exception {
List list=new ArrayList();
con=BaseDao.getcon();
try {
String sql="select * from news limit ?,?";
ps=con.prepareStatement(sql);
//表示当页所在的索引即从那开始
ps.setInt(1, (cp-1)*ls);
ps.setInt(2,ls);
rs=ps.executeQuery();
while(rs.next()){
News n=new News();
n.setContent(rs.getString("content"));
n.setCreator(rs.getString("creator"));
n.setNid(rs.getInt("nid"));
n.setPubTime(rs.getString("pub_time"));
n.setTitle(rs.getString("title"));
n.setTid(rs.getInt("tid"));
list.add(n);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/*
* 总页数
* */
//ls每页条数
public int getAllPage(int ls) throws Exception {
int allCount=0;//总条数
int allPage=0;//总页数
allCount=this.getCount();
allPage=(allCount%ls==0)?(allCount/ls):(allCount/ls)+1;
return allPage;
}
/*
* 总条数
*/
public int getCount() throws Exception {
int count=0;
String sql="select count(nid) from news";
con=BaseDao.getcon();
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()){
count=rs.getInt(1);
}
return count;
}
}
JSP:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
// 获取当前信息---隐藏域中存储
String cp=request.getParameter("cp");
int currentPage=1;//当前页
if(cp!=null&&!("".equals(cp))){
currentPage=Integer.parseInt(cp);
}
int ls=3;
%>
My JSP 'list_news.jsp' starting pagefunction changePag(currentPage){
document.myform.cp.value=currentPage;//修改当前页面******
document.myform.submit();//提交事件 修改后触发提交
}
新闻ID | 新闻标题 | 新闻作者 | 新闻介绍 | 新闻发布时间 |
int allpage=0;
List list=new ArrayList();
NewsDao ndao=new NewsDaoImpl();
list=ndao.findPage(currentPage,ls);
allpage=ndao.getAllPage(ls);
if(list.size()>0){
for(int i=0;i
News n=new News();
n=list.get(i);%>
}
}%>
共有:页 当前页:/页
/>
/>
/>
/>
跳转到
请选择页数
if(allpage>0){
for(int i=1;i<=allpage;i++){
%>
>
}
}
%>
页