关闭

jsp分页技术

269人阅读 评论(0) 收藏 举报

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.sql.*"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>
<%
 request.setCharacterEncoding("UTF-8");
%>


 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">

  <title>My JSP 'Loginpage.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">
 -->
   <body>
   <h1>学生信息表分页技术 </h1>
  
  <%
   int pagesize = 3; //每页显示多少条记录
   int pagenow = 1; //希望显示第几条 默认显示第一页
   int pagecount=0; //一共有多少页
   int rowcount=0; //一共有多少条记录 从数据库中查询
     //对pagenow修改(接收用户希望显示的页数)
   String s_pagenow=request.getParameter("pagenow");
   if(s_pagenow!=null){ //当接收到的时候
   pagenow=Integer.parseInt(s_pagenow);

   }

 

   try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
    System.out.print("驱动包连接失败!");
   }

   String url = "jdbc:sqlserver://localhost:1433;DatabaseName=StuInfor";
   ;
   String user = "sa";
   String password = "sa";
   Connection con = null;
   PreparedStatement psta = null;
   String sql = "select count(*) from Student2";
   ResultSet rs = null;

   //2.连接数据库
   try {
    con = DriverManager.getConnection(url, user, password);
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.out.println("数据库连接失败!");
   }

   //3.创建 PreparedStatement对象  
   try {
    psta = con.prepareStatement(sql);
    rs = psta.executeQuery();
   } catch (SQLException e) {

    e.printStackTrace();
   }
   if (rs.next()) {
    rowcount = rs.getInt(1);  //取到数据库中的1行一列就是所有记录
   }
   if (rowcount % pagesize == 0) { //计算pagecount
    pagecount = rowcount / pagesize;
   } else {
    pagecount = rowcount / pagesize + 1;
   }
   String sql1 = "select top " + pagesize
     + " *from Student2 where id not in(select top " + pagesize
     * (pagenow - 1) + " id from Student2 )";
     psta = con.prepareStatement(sql1);
     rs=psta.executeQuery();
   //显示
  %>

   <table border="1">
   <tr>
    <td>学号</td> <td>姓名</td> <td>性别</td> <td>年龄</td><td>班级</td>
   </tr>
   <%   
   while(rs.next()){
    %>
    <tr> <td><%=rs.getInt(2)%></td> <td><%=rs.getString(3)%></td>
     <td><%=rs.getString(4)%></td> <td><%=rs.getString(5)%></td>
     <td><%=rs.getString(6)%></td>
    <% 
     }
     %>
    
  </table>
 <%
 
    //显示上一页
   if(pagenow!=1){
  out.println("<a href=Loginpage.jsp?pagenow="+(pagenow-1)+">上一页</a>");
  }
 
  //显示超链接
  for(int i=1;i<=pagecount;i++){
   out.println("<a href=Loginpage.jsp?pagenow="+i+">["+i+"]</a>");

  }
 
 //显示下一 页
   if(pagecount!=pagenow){
   out.println("<a href=Loginpage.jsp?pagenow="+(pagenow+1)+">下一页</a>");
  }
  out.println("<a href=Mypage.jsp?>  你当前在第"+pagenow+"页</a>");
  %>
</body>

 

  
  
</html>

 

 

 


 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:110445次
    • 积分:2339
    • 等级:
    • 排名:第16548名
    • 原创:126篇
    • 转载:12篇
    • 译文:0篇
    • 评论:17条
    最新评论