📄 list.jsp
字号:
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<%@ include file="top.jsp"%>
<jsp:useBean id="conn" class="dbBean.DBBean" scope="session"/>
<jsp:useBean id="sp" class="spage.ShowPage" scope="session"/>
<html>
<head>
<title>查看图书</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body background="images/bg01.gif">
<%!
String searchtype="";
String searchkey="";
String booktype="所有书籍";
String sql1="";
String sql2="";
ResultSet rs=null;
ResultSet temp=null;
//每页显示的记录个数
int size = 10;
//当前页号
int p = 1;
//全部的页数
int totalPage = 1;
%>
<%
request.setCharacterEncoding("gb2312");
if(request.getParameter("stype")!=null)
searchtype=request.getParameter("stype");
if(request.getParameter("skey")!=null)
searchkey=request.getParameter("skey");
if(request.getParameter("booktype")!=null)
booktype=request.getParameter("booktype");
String rank=(String)session.getAttribute("rank");
%>
<p><center>
<form action="list.jsp" method="post">
请选择查询类别:<select name="stype">
<option value="bookname" <%if(searchtype!=null) if(searchtype.equals("bookname")) out.print("selected");%>>书名</option>
<option value="author" <%if(searchtype!=null) if(searchtype.equals("author")) out.print("selected");%>>作者</option>
<option value="pubhouse" <%if(searchtype!=null) if(searchtype.equals("pubhouse")) out.print("selected");%>>出版社</option>
<option value="isbn" <%if(searchtype!=null) if(searchtype.equals("isbn")) out.print("selected");%>>ISBN</option>
</select>
请输入查询关键字:<input type="text" name="skey" size=10 value="<%if(searchkey!=null) out.print(searchkey);%>">
<input type="submit" value="确定">
</form>
<div align="left"><font size="2" color="#FF3399"> <b><%=booktype%>>>></b></font></div>
<table width="95%" border bordercolor="blue" align="center" cellpadding="0" cellspacing="0" bordercolorlight="#FFFFFF">
<tr align="center" height="20">
<th background="images/bg04.gif">
书名</th><th background="images/bg04.gif">
作者</th><th background="images/bg04.gif">
出版社</th><th background="images/bg04.gif">
价格</th>
<%
if(rank.equals("1"))
{
%>
<th background="images/bg04.gif">
销量</th>
<th background="images/bg04.gif">
修改</th>
<th background="images/bg04.gif">
删除</th>
<%
}
else
{
%>
<th background="images/bg04.gif">
加入购物车</th>
<%
}
sql1="select * from book";
sql2="select count(*) from book";
if(booktype.equals("所有书籍"))
{
if(!searchtype.equals("") && !searchkey.equals(""))
{
sql1 += " where "+searchtype+" like '%"+searchkey+"%'";
sql2 += " where "+searchtype+" like '%"+searchkey+"%'";
}
}
else
{
sql1 += " where type='"+booktype+"'";
sql2 += " where type='"+booktype+"'";
if(!searchtype.equals("") && !searchkey.equals(""))
{
sql1 += " and "+searchtype+" like '%"+searchkey+"%'";
sql2 += " and "+searchtype+" like '%"+searchkey+"%'";
}
}
sql1+=" order by bookid desc";
temp=conn.executeQuery(sql2);
int totalrecord=0;
if(temp.next())
totalrecord = temp.getInt(1);
// 如果是当前页码的整数倍
if(totalrecord % size ==0) totalPage = totalrecord / size;
// 如果最后还空余一页
else totalPage = totalrecord / size + 1;
if(totalPage == 0) totalPage = 1;
temp.close();
try {
if(request.getParameter("p")==null || request.getParameter("p").equals(""))
{
if(session.getAttribute("bookp")==null)
p = 1;
else
p = ((Integer)session.getAttribute("bookp")).intValue();
}
else
{
p = Integer.parseInt(request.getParameter("p")); session.setAttribute("bookp",new Integer(p));
}
}
// 捕获用户从浏览器地址拦直接输入非数字信息而引起的异常
catch(NumberFormatException e) {
p = 1;
}
if(p < 1) p = 1;
if(p > totalPage) p = totalPage;
rs=conn.executeQuery(sql1);
if(rs.next())
{
if(rank.equals("1"))
out.println(sp.bookPage(rs,p,size,true));
else
out.println(sp.bookPage(rs,p,size,false));
}
%>
</table>
<form Action="list.jsp" Method="GET">
<%
for(int i=1;i<=totalPage;i++) {
out.println("<a href=list.jsp?p=" + i +">" + i + "</a> ");
}
%>
<p>输入页数:<input type="text" name="p" size="3">
页数:<font color="red"><%=p%>/<%=totalPage%></font>
</form>
</center>
<%
rs.close();
conn.close();
%>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -