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<News> findPage(int cp, int ls) throws Exception {
List<News> list=new ArrayList<News>();
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:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.accp.jsppage.dao.NewsDao"%>
<%@page import="org.accp.jsppage.dao.Impl.NewsDaoImpl"%>
<%@page import="org.accp.jsppage.entity.News"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%
// 获取当前信息---隐藏域中存储
String cp=request.getParameter("cp");
int currentPage=1;//当前页
if(cp!=null&&!("".equals(cp))){
currentPage=Integer.parseInt(cp);
}
int ls=3;
%>
<base href="<%=basePath%>">
<title>My JSP 'list_news.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function changePag(currentPage){
document.myform.cp.value=currentPage;//修改当前页面******
document.myform.submit();//提交事件 修改后触发提交
}
</script>
</head>
<body>
<form action="list_news.jsp" method="post" name="myform">
<input type="hidden" name="cp"/>
<table border="1px">
<tr>
<td>新闻ID</td>
<td>新闻标题</td>
<td>新闻作者</td>
<td>新闻介绍</td>
<td>新闻发布时间</td>
</tr>
<%
int allpage=0;
List<News> list=new ArrayList<News>();
NewsDao ndao=new NewsDaoImpl();
list=ndao.findPage(currentPage,ls);
allpage=ndao.getAllPage(ls);
if(list.size()>0){
for(int i=0;i<list.size();i++){
News n=new News();
n=list.get(i);%>
<tr>
<td><%=n.getNid() %></td>
<td><%=n.getTitle() %></td>
<td><%=n.getCreator() %></td>
<td><%=n.getContent() %></td>
<td><%=n.getPubTime() %></td>
</tr>
<%
}
}%>
<tr>
<td colspan="6">
共有:<%=allpage %>页 当前页:<%=currentPage %>/<%=allpage %>页
<input type="button" value="首页" onClick="changePag(1)" <%=currentPage==1?"disabled":"" %>/>
<input type="button" value="上页" onClick="changePag(<%=currentPage-1%>)" <%=currentPage==1?"disabled":"" %>/>
<input type="button" value="下页" onClick="changePag(<%=currentPage+1 %>)" <%=currentPage==allpage?"disabled":"" %>/>
<input type="button" value="尾页" onClick="changePag(<%=allpage %>)" <%=currentPage==allpage?"disabled":"" %>/>
跳转到<select onChange="changePag(this.value)">
<option >请选择页数</option>
<%
if(allpage>0){
for(int i=1;i<=allpage;i++){
%>
<option value="<%=i%>" <%=currentPage==i?"selected":"" %>><%=i%></option>
<%
}
}
%>
</select>页
</td>
</tr>
</table>
</form>
</body>
</html>
转载于:https://blog.51cto.com/6160337/1045238