?? showsp.jsp
字號:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="errorpage.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<style type="text/css">
<!--
.STYLE21 {color: #000000; font-weight: bold; }
.STYLE7 {font-size: 10; }
body,td,th {
font-size: 12px;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
.STYLE2 {color: #0000CC}
-->
</style>
</head>
<jsp:useBean id="db" class="data.shopdb"/>
<%!
int PageSize =2; //設置每張網頁顯示兩筆記錄
int ShowPage = 1; //設置欲顯示的頁數
int RowCount = 0; //ResultSet的記錄筆數
int PageCount = 0; //ResultSet分頁后的總頁數
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
public void jspInit() //執行數據庫與相關數據的初始化
{
try{
Class.forName("org.gjt.mm.mysql.Driver");
//使用DriverManager類的getConnection()方法建立聯接,
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/mydb","root","test");
//建立Statement對象, 并設置記錄指標類型為可前后移動
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT * FROM shop order by xinyong"); //建立ResultSet(結果集)對象,并執行SQL語句
rs.last(); //將指標移至最后一筆記錄
RowCount = rs.getRow(); //取得ResultSet中記錄的筆數
//計算顯示的頁數
PageCount = ((RowCount % PageSize) == 0 ?
(RowCount/PageSize) : (RowCount/PageSize)+1);
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
public void jspDestroy() //執行關閉各種對象的操作
{
try{
rs.close(); //關閉ResultSet對象
stmt.close(); //關閉Statement對象
con.close(); //關閉數據庫鏈接對象
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
%>
<body>
<table width="600" height="290" border="1" align="left" cellspacing="0" bordercolor="#993300">
<tr>
<td width="616" height="309" background="../pic/bbgg.jpg"><p align="center"><%
String ToPage = request.getParameter("ToPage");
if(ToPage != null) //判斷是否可正確取得ToPage參數
{
ShowPage = Integer.parseInt(ToPage); //取得指定顯示的分頁頁數
if(ShowPage > PageCount) //下面的if語句將判斷用戶輸入的頁數是否正確
{
ShowPage = PageCount; //判斷指定頁數是否大于總頁數, 是則設置顯示最后一頁
}
else if(ShowPage <= 0)
{
ShowPage = 1; //若指定頁數小于0, 則設置顯示第一頁的記錄
}
}
rs.absolute((ShowPage - 1) * PageSize + 1); //計算欲顯示頁的第一筆記錄位置
%>
<H3>
<div align="center">當前在第<font color = "red"> <%= ShowPage %></font>頁, 共 <font color = "red"> <%= PageCount %></font>頁
<%
//利用For循環配合PageSize屬性輸出一頁中的記錄
for(int i = 1; i <= PageSize; i++)
{
%>
</div>
<table width="494" border="1" align="center" cellpadding="0" cellspacing="2">
<tr><%String ddid=rs.getString("soldid");%>
<td width="189"><span class="STYLE21">店名:<a href="../shop/showtss.jsp?shopid=<%=ddid%>"><%=rs.getString("shopname")%></a></span></td>
<td width="158"><span class="STYLE21">店主:<%=ddid%></span></td>
<td width="167"><span class="STYLE21">信用度:<%=rs.getString("xinyong")%></span></td>
</tr>
<tr>
<td height="50"><table width="185" border="0" cellpadding="0">
<tr>
<td width="73"><strong>公告:</strong></td>
<td width="105"><strong>
<textarea name="textfield" cols="16" rows="3" readonly="readonly"><%=rs.getString("shopmes")%></textarea>
</strong></td>
</tr>
</table></td>
<td><table width="106" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><strong>圖片:</strong></td>
<td><strong><span class="STYLE2"><img src="../shoppic/<%=rs.getString("shoppic")%>" width="50" height="50" /></span></strong></td>
</tr>
</table></td>
<td><div align="center"><strong><a href="../message/sendmessage.jsp?fromid=<%=ddid%>">聯系店主</a></strong></div></td>
</tr>
</table>
<p align="center">
<%
//下面的if判斷語句用于防止輸出最后一頁記錄時,
//將記錄指標移至最后一筆記錄之后
if(!rs.next()) //判斷是否到達最后一筆記錄
break; //跳出for循環
}
%>
</p>
<table width="595">
<tr valign="baseline" align="center">
<%
//判斷目前所在分頁是否為第一頁,
//不是則顯示到第一頁與上一頁的超鏈接
if(ShowPage != 1)
{
//下面建立的各超鏈接將鏈接至自己,
//并將欲顯示的分頁以ToPage參數傳遞給自己
%>
<td width="150" height="23"><a href="showsp.jsp?ToPage=<%= 1 %>" class="STYLE7">首頁</a> </td>
<td width="150"><a href="showsp.jsp?ToPage=<%= ShowPage - 1 %>" class="STYLE7">上頁</a> </td>
<%
}
//判斷目前所在分頁是否為最后一頁,
//不是則顯示到最后一頁與下一頁的超鏈接
if(ShowPage != PageCount)
{
//下面建立的各超鏈接將鏈接至自己,
//并將欲顯示的分頁以ToPage參數傳遞自己
%>
<td width="150"><a href="showsp.jsp?ToPage=<%= ShowPage + 1%>" class="STYLE7">下頁</a> </td>
<td width="150"><a href="showsp.jsp?ToPage=<%= PageCount %>" class="STYLE7">末頁</a> </td>
<%
}
%>
<td width="150"><form action="showsp.jsp" method="post" class="STYLE7">
到
<input type="text" name="ToPage" style="HEIGHT: 15px; WIDTH: 20px"
value="<%= ShowPage%>" />
頁
</form></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -