📄 showthing.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 thing order by shopid"); //建立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="516" border="1" align="center" cellpadding="0" cellspacing="2">
<tr><%String ddid=rs.getString("shopid");%>
<td width="202"><span class="STYLE21">物品:<%=rs.getString("thingname")%></span></td>
<td width="145"><span class="STYLE21">店主:<a href="../shop/showtss.jsp?shopid=<%=ddid%>"><%=ddid%></a></span></td>
<td width="153"><span class="STYLE21">上架时间:<%=rs.getString("starttime")%></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("thingmes")%>
</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("pic")%>" 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="showthing.jsp?ToPage=<%= 1 %>" class="STYLE7">首页</a> </td>
<td width="150"><a href="showthing.jsp?ToPage=<%= ShowPage - 1 %>" class="STYLE7">上页</a> </td>
<%
}
//判断目前所在分页是否为最后一页,
//不是则显示到最后一页与下一页的超链接
if(ShowPage != PageCount)
{
//下面建立的各超链接将链接至自己,
//并将欲显示的分页以ToPage参数传递自己
%>
<td width="150"><a href="showthing.jsp?ToPage=<%= ShowPage + 1%>" class="STYLE7">下页</a> </td>
<td width="150"><a href="showthing.jsp?ToPage=<%= PageCount %>" class="STYLE7">末页</a> </td>
<%
}
%>
<td width="150"><form action="showthing.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 + -