📄 right.jsp
字号:
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<%@ include file="check.jsp"%>
<%@ page import="dbBean.*"%>
<jsp:useBean id="conn" class="dbBean.DBBean" scope="session"/>
<%!
//每页显示的记录总数
int size=8;
//当前页数
int p=1;
int totalPage=1;
String str="";
//此方法用于显示页号为P的一页
public String printPage(ResultSet rs,int p,int size,int logId)
{
str="";
//将访问游标定位到页号为P的页要显示的没一条记录的位置
try{
for(int k=0;k<(p-1)*size;k++)
rs.next();
}
catch(SQLException e){}
for(int iPage=1;iPage<=size;iPage++){
str+=printRow(rs,iPage,p,logId);
try{
if(!rs.next()) break;
}
catch(Exception e){}
}
return str;
}
//此方法用于显示单行记录
public String printRow(ResultSet rs,int i,int p,int logId)
{
String temp="";
try{
if(i%2==1)
temp+="<tr bgcolor='#e7e7e7'>";
else
temp+="<tr bgcolor='#f7f7f7'>";
String id=rs.getString("id");
//若是管理员,则可通过单击每条记录的id号进行修改用户信息的操作
if(logId<10001)
temp+="<td><a href='update.jsp?updateid="+id+"&p="+p+"'target='left'>"+id+"</a></td>";
//否则不能修改其他用户的信息
else
temp+="<td>"+id+"</td>";
temp+="<td>"+rs.getString("name")+"</td>";
temp+="<td>"+rs.getString("sex")+"</td>";
temp+="<td>"+rs.getString("age")+"</td>";
temp+="<td>"+rs.getString("phone")+"</td>";
temp+="<td>"+rs.getString("email")+"</td>";
temp+="<td>"+rs.getString("address")+"</td>";
temp+="</tr>";
}
catch(SQLException e){}
return temp;
}
%>
<%
request.setCharacterEncoding("gb2312");
String type="";
String key="";
//将本次查询的类型和关键字存入session中,以备下次请求时使用
if(request.getParameter("stype")!=null)
{
type=request.getParameter("stype");
session.setAttribute("stype",type);
}
if(request.getParameter("skey")!=null)
{
key=request.getParameter("skey");
session.setAttribute("skey",key);
}
ResultSet rs=null;
String sql="select * from user";
if(session.getAttribute("stype")!=null)
type=(String)session.getAttribute("stype");
if(session.getAttribute("skey")!=null)
key=(String)session.getAttribute("skey");
//按查询类型和查询关键字生成相应的SQL语句
if(type!=null&&key!=null&&!type.equals("")&&!key.equals(""))
{
if(type.equals("id")||type.equals("age"))
sql+=" where "+type+"="+key;
else
sql+=" where "+type+"='"+key+"'";
}
sql+=" order by id asc";
rs=conn.executeQuery(sql);
%>
<center>
<table border="1"borderColorDark="#ffffec"borderColorLight="#5e5e00"width="100%">
<tr bgcolor="#CCCCCC"align="center">
<th>id号</th><th>用户名</th><th>性别</th><th>年龄</th><th>联系电话</th><th>电子邮件</th><th>地址</th>
<%
ResultSet rsTmp=null;
//计算user表中所有的记录个数
String sql2="select count(*)from user";
if(type!=null&&key!=null&&!type.equals("")&&!key.equals(""))
{
if(type.equals("id")||type.equals("age"))
sql2+=" where "+type+"="+key;
else
sql2+=" where "+type+"='"+key+"'";
}
rsTmp=conn.executeQuery(sql2);
int totalrecord=0;
if(rsTmp.next())
totalrecord=rsTmp.getInt(1);
if(totalrecord%size==0)totalPage=totalrecord/size;
else totalPage=totalrecord/size+1;
if(totalPage==0)totalPage=1;
rsTmp.close();
try{
if(request.getParameter("p")==null||request.getParameter("p").equals(""))
{
if(session.getAttribute("rightp")==null)
p=1;
else
p=((Integer)session.getAttribute("rightp")).intValue();
}
else
{
p=Integer.parseInt(request.getParameter("p"));
session.setAttribute("rightp",new Integer(p));
}
}
//捕获用户从浏览器地址栏直接输入非数字信息而引起的异常
catch(NumberFormatException e){p=1;}
if(p<1)p=1;
if(p>totalPage)p=totalPage;
if(rs.next())
{
int logId=Integer.parseInt((String)session.getAttribute("loginId"));
//输出当前页需要显示的内容
out.println(printPage(rs,p,size,logId));
}
%>
</table>
<form Action="right.jsp"Method="GET">
<%
for(int i=1;i<=totalPage;i++){
out.println("<a href=right.jsp?p="+i+">"+i+"</a> ");
}
%>
<p>输入页数:<input type="text"name="p"size="3">
页数:<font color="red"><%=p%>/<%=totalPage%></font>
</p>
</form>
</center>
<%
rs.close();
conn.close();
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -