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

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'fenyan.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">
	-->

  </head>
  
  <body>
    <%!
       int pageSize=5;
       int pageCount=0;   
    
     %>
     <form action="" method="get">
        <p>请输入页数:<input type="text" name="page"/></p>
        <input type="submit" value="提交"/>
     </form>
     <%
         String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   //加载JDBC驱动   
		 String dbURL = "jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=mmiEMSDB";   //连接服务器和数据库msdb   
		 Connection dbConn=null; 
		 Statement s;
		 ResultSet rs=null;
		 int showPage;  
		   
		 try {   
		        Class.forName(driverName);   
		        dbConn = DriverManager.getConnection(dbURL);   
		        //System.out.println("Connection Successful!");   //如果连接成功 控制台输出Connection Successful!   
		        s=dbConn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
		        String sql="select ID,OrgFullName,GroupID from dbo.SBJ_ORG";
		        rs=s.executeQuery(sql);
		        
		        //将游标移到最后一行
		        rs.last();
		        //获取最后一行的行号
		        int lastRow=rs.getRow();
		        
		        pageCount=(lastRow%pageSize==0)?(lastRow/pageSize):(lastRow/pageSize+1);
		        
		        //当前显示的页数
		        showPage=1;
		        
		        
		        
		     } 
		     catch (Exception e) {   
		            }   
      %>
      
      <p>共有<%=pageCount %>页</p><br/>
      <p>每页显示:<%=pageSize %></p>
      
      <%//获取当前想要显示的页数
          String pagenum=request.getParameter("page");
          
          if(pagenum==null)
          {
              showPage=1;
          }
          try
          {
              showPage=Integer.parseInt(pagenum);
          }
          catch(NumberFormatException nf){
             showPage=1;
          }
          
          if(showPage<=1)
             showPage=1;
          if(showPage>=pageCount)
             showPage=pageCount;
             
          
       %>
       <br/><p>目前显示的页面<%=showPage %></p>
       
       <%//如果想要显示showpage页,那么将游标移到position的位置
            
            int position=(showPage-1)*pageSize+1;
            
            rs.absolute(position);
            
            out.print("<table border>");
            out.print("<tr>");
            out.print("<th>ID</th>");
            out.print("<th>组织全称</th>");
            out.print("<th>代码</th>");
            out.print("</tr>");
            
            for(int i=1;i<=pageSize;i++)
            {
                out.print("<tr>");
                out.print("<td>");out.print(rs.getInt(1));out.print("</td>");
                out.print("<td>");out.print(rs.getString(2));out.print("</td>");
                out.print("<td>");out.print(rs.getString(3));out.print("</td>");
                out.print("</tr>");
                rs.next();
            }
            
            out.print("</table>");
            
            dbConn.close();
        %>
  </body>
</html>

你可能感兴趣的:(html,sql,jsp,jdbc,Microsoft)