当前位置:编程学习 > JS >>

jsp实现读取数据库数据分页显示

[html] 
<%@ page language="java" pageEncoding="gb2312" %> 
<%@ page contentType="text/html;charset=gb2312" %> 
<%request.setCharacterEncoding("gb2312");%> 
<%@ page import="java.util.*,java.lang.*,java.sql.*,javax.naming.*,javax.sql.*" %> 
<%@ page errorPage="error.html" %> 
<html>  www.zzzyk.com
<head><meta http-equiv=Content-Type content="text/html;charset=gb2312"></head> 
<body> 
<%Connection con=null; 
  Statement stmt=null; 
  ResultSet rs=null; 
  int pageSize,rowCount,pageCount,curPage; 
  pageSize=5; 
  pageCount=1; 
  String strPage=request.getParameter("page"); 
  if(strPage==null) curPage=1; 
  else{ 
    curPage=Integer.parseInt(strPage);//通过参数来定当前页面 
    if(curPage<1) curPage=1; 
    } 
%> 
<%try{ 
  InitialContext ctx=new InitialContext(); 
  DataSource ds=(DataSource)ctx.lookup("java:comp/env/MySqlDS"); 
  con=ds.getConnection(); 
  stmt=con.createStatement(); 
  rs=stmt.executeQuery("select * from infos"); 
  rs.last(); 
  rowCount=rs.getRow(); 
  pageCount=(rowCount+pageSize-1)/pageSize; 
  if(curPage>pageCount) curPage=pageCount; 
  }catch(SQLException e){ 
    System.out.println(e.toString()); 
  } 
%> 
<table width="100%" ><tr><td> 
页次:<%=curPage %>/<%=pageCount %>页     <%=pageSize %>条/页</td></tr> 
</table> 
<table width="80%"><tr> 
<th>Number</th><th>Name</th><th>Activity</th><th>Score</th></tr> 
<%if(pageCount>0) rs.absolute((curPage-1)*pageSize+1); 
int i=0; 
while(i<pageSize&&!rs.isAfterLast()){%> 
<tr align="center"><td><%=rs.getString(1)%></td><td><%=rs.getString(2)%></td> 
<td><%=rs.getString(3)%></td><td><%=rs.getString(4)%></td></tr> 
<% 
rs.next(); 
i++; 
} %> 
</table> 
<%if(curPage>1){ %> 
<a href="MyJsp.jsp?page=<%=curPage-1%>">上一页</a><%} %> 
<%if(curPage<pageCount){ %> 
<a href="MyJsp.jsp?page=<%=curPage+1%>">下一页</a><%} %> 
</body></html> 
<%rs.close(); 
  stmt.close(); 
  con.close(); 
%> 
补充:Web开发 , Jsp ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,