?? 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 + -