JSP分页技术

参数: int pageNow//当前页面
       int pageCount //总页数
   int recordCount //记录总数
   int recordSize //一个页面的记录数
   int pageFirst //页面第一页页码
   int pageLast //页面最后一页
       int pageSize //页面跨度
  
<!--先用户登录UserLogin.jsp-->
<%@page contentType = "text/html;charset=gb2312"%>
<html>
<title>
用户登录
</title>
<script language = "javascript">
   function check(){
if(loginForm.username==null){
window.alert("用户名不能为空");
return false;
}
if(loginForm.password==null){
window.alert("密码不能为空");
return false ;
}
   }
</script>
<%
int worngNum =  0 ;
String S_worngNum = request.getParameter("worngNum");
if(!S_worngNum.equals("")){
worngNum = Integer.parseInt(S_worngNum);
}
if(worngNum==1){
out.println("<h1 aglin = 'left'><font color = 'red' size = '10'>用户名或密码错误,请重新登录</font></h1>");
}
%>
<h2 align="center">用户登录</h2>
<center>
<body>
<form name = "loginForm" action = "UserCheck.jsp" method = "post">
<table border = "1" >
<tr><td>用户名:</td><td><input type = "text" name = "username"></td></tr>
<tr><td>密&nbsp;&nbsp;码:</td><td><input type = "password" name = "password"></td></tr>
<tr><td><input type = "submit" value = "登录"  onclick = "return check()"></td><td><input type = "reset" value = "重置"></td></tr>
</table>   
</form>   
</body>
</center>
</html>

//封装数据库javaBean
package com.cn.wang ;
import java.sql.*;

public class UserBean{
Connection conn = null ;
Statement st = null ;
ResultSet rs = null ;
public Statement getStatement(){

//连接数据库,一些参数初始化
String URL = "jdbc:mysql://127.0.0.1:3306/test" ;
String username = "root" ;
String password = "951357" ;
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//建立连接
conn = DriverManager.getConnection(URL,username,password);
//创建Statement
st = conn.createStatement();
}catch(ClassNotFoundException e1){
System.out.println("Class not found");  
}catch(SQLException e2){
e2.printStackTrace();
}
  return st ;
}

}

<!--验证用户合法性UserCheck.jsp-->
<%@page contentType = "text/html;charset=gb2312" import ="java.sql.*"%>
<jsp:useBean id = "jdbcbean" class = "com.cn.wang.UserBean"/>
<html>
<title>
用户验证
</title>
<body>
  <%
       String username = request.getParameter("username");
   String password = request.getParameter("password");
   Statement st = jdbcbean.getStatement();
   ResultSet rs = st.executeQuery("select username from userinfo where password = "+ password);
       if(rs.next()){
      if(rs.getString(1).equals(username)){
    response.sendRedirect("BufferPage.jsp");
session.setAttribute("username",username);
  }else{
    response.sendRedirect("UserLogin.jsp?worngNum="+1);
  }
   }else{   
        response.sendRedirect("UserLogin.jsp?worngNum="+1);
   }
  %>
</body>
</html>
   
<!--缓冲页面BufferPage.jsp-->
<%@page contentType="text/html;charset=gb2312"%>
<html>
<title>
页面跳转
</title>
<center>
<body>
<%
  
   out.print("<font size = "10" color = "red">你已成功登录,6秒钟将跳转到首页</font>");
%>
</body>
</center>
</html>

<!--登录成功,分页界面Welcome.jsp-->
<%@page contentType = "text/html;charset=gb2312"%>
<jsp:useBean id = "jdbcbean" class = "com.cn.wang.UserBean"/>
<html>
<title>
分页界面
</title>
<%
  response.setHeader("refresh","6;url=Welcome.jsp");
  String username = (String)session.getAttribute("username");//获得登录用户名
%>
<h1><font size = "10" color ="pink">欢迎<%=username%></font></h1><hr>
<%
  
   int pageNow = 1 ;//当前页面
   int pageCount = 0 ;//总页数
   int recordCount = 0 ; //记录总数
   int recordSize = 4 ; //一个页面的记录数
   int pageFirst = 1 ; //页面第一页页码
   int pageLast = 1 ;//页面最后一页
   int pageSize = 9 ;//页面跨度
   int size = (pageSize+1)/2 ;
   Statement st = jdbcbean.getStatement();  //创建Statement  
   ResultSet rs = st.executeQuery("select count(*) from userinfo");//查询总记录数
   if(rs.next()){
     recordCount = rs.getInt(1);//得到记录数
   }
   //计算总的页面数
   if(recordCount%recordSize==0){
      pageCount = recordCount/recordSize ;
   }else{
      pageCount = recordCount/recordSize+1 ;
   }
  
   String S_pageNow = request.getParameter("pageNow");//取得用户指定的页数
   if(S_pageNow!=null){
      pageNow = Integer.parseInt(S_pageNow);
   }
   //计算第一页的页数
   if(pageNow<=size){
    pageFirst = 1 ;
   }else{
    pageFirst = pageNow - size ;
   }
   //计算最后一页的页数
   if(pageCount<=(pageNow+size)){
    pageLast = pageCount ;
   }else{
    pageLast = pageNow + size ;
   }
  
%>
<body>
   <table border = "1" align = "center" bgcolor = "pink">
     <tr><td>用户编号</td><td>用户名</td><td>密码</td><td>权限</td></tr>
<%
//显示分页界面
   rs = st.executQuery("select * from userinfo limit"+(pageNow-1)*recordSize+","+recordSize);
   while(rs.next()){
       out.print("<tr><td>"+rs.getInt(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getInt(4)+"</td><td>"+rs.getInt(5)+"</td></tr>");   
  }
      
%>
    </table>  
    <hr>
<%   
 
       //判断是否显示上一页
   if(pageNow>1){
out.print("<a href = Welcome.jsp?pageNow="+(pageNow-1)+">上一页</a>");
   }
   for(int i =pageFirst ;i<=pageLast ; i++){
   if(i==pageNow){
     out.print("<a href = Welcome.jsp?pageNow="+i+">[<font color = 'red'>"+i+"</font>]</a>");
   }else
     out.print("<a href = Welcome.jsp?pageNow="+i+">["+i+"]</a>");  
   }
   //判断是否显示下一页
   if(pageLast<pageCount){
     out.print("<a href = Welcome.jsp?pageNow="+(pageNow+1)+">下一页</a>");  
   }
 

%>
</body>
</html>






























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