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

<%@ 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" %>



<%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());
  }
%>
页次:<%=curPage %>/<%=pageCount %>页 <%=pageSize %>条/页
<%if(pageCount>0) rs.absolute((curPage-1)*pageSize+1); int i=0; while(i <% rs.next(); i++; } %>
NumberNameActivityScore
<%=rs.getString(1)%><%=rs.getString(2)%> <%=rs.getString(3)%><%=rs.getString(4)%>
<%if(curPage>1){ %> 上一页<%} %> <%if(curPage 下一页<%} %> <%rs.close(); stmt.close(); con.close(); %>


------------------------------------------------------------------------


你可能感兴趣的:(J2EE)