📄 index.jsp
字号:
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<%@ include file="top.htm"%>
<jsp:useBean id="conn" class="dbBean.DBBean" scope="session"/>
<jsp:setProperty name="conn" property="connStr" value="jdbc:odbc:news"/>
<%!
String type="";
String key="";
//每页显示的记录个数
int size = 8;
//当前页号
int p = 1;
//全部的页数
int totalPage = 1;
String str = "";
//显示页号为p的一页
public String printPage(ResultSet rs, int p, int size)
{
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);
try {
if(!rs.next()) break;
}
catch(Exception e) { }
}
return str;
}
//显示单行记录
public String printRow( ResultSet rs ,int i,int p)
{
String temp = "";
try {
if(i%2==1)
temp+="<tr bgcolor='#FFFFCC'>";
else
temp+="<tr bgcolor='#f7f7f7'>";
temp+="<td><a href=javascript:detail("+rs.getInt("id")+")>"+rs.getString("title")+"</a></td>";
temp+="<td>"+rs.getString("author")+"</td>";
temp+="<td>"+rs.getString("pubdate")+"</td>";
temp += "</tr>";
}
catch(SQLException e) { }
return temp;
}
%>
<script language="javaScript">
function detail(id)
{
window.open('detail.jsp?id='+id,'infoWin','height=400,width=600,scrollbars=yes,resizable=yes');
}
</script>
<%
request.setCharacterEncoding("gb2312");
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;
ResultSet temp=null;
String sql1="select * from news";
String sql2="select count(*) from news";
if(session.getAttribute("stype")!=null)
type=(String)session.getAttribute("stype");
if(session.getAttribute("skey")!=null)
key=(String)session.getAttribute("skey");
if(type!=null && key!=null && !type.equals("") && !key.equals(""))
{
sql1 += " where "+type+" like '%"+key+"%'";
sql2 += " where "+type+" like '%"+key+"%'";
}
sql1+=" order by pubdate desc";
%>
<center>
<p><font size="4" color="red"><strong>欢迎您访问新闻发布系统!</strong></font>
<p><font size="2">
<form method="post" action="index.jsp">
请选择查询类别:
<select name="stype">
<option value="title" <%if(type!=null) if(type.equals("title")) out.print("selected");%>>按标题查询</option>
<option value="content" <%if(type!=null) if(type.equals("content")) out.print("selected");%>>按内容查询</option>
<option value="author" <%if(type!=null) if(type.equals("author")) out.print("selected");%>>按作者查询</option>
<option value="pubdate" <%if(type!=null) if(type.equals("pubdate")) out.print("selected");%>>按日期查询</option>
</select>
请输入查询关键字:
<input type="text" name="skey">
<input type="submit" value="查询">
</form></font></center>
<p>
<center>
<table border="1" borderColorDark="#ffffec" borderColorLight="#5e5e00" width="95%">
<tr bgcolor="#FFCC99" align="center">
<th width="50%">标题</th><th width="20%">作者</th><th>日期</th>
<%
temp = conn.executeQuery(sql2);
int totalrecord=0;
if(temp.next())
totalrecord = temp.getInt(1);
// 如果是当前页码的整数倍
if(totalrecord % size ==0) totalPage = totalrecord / size;
// 如果最后还空余一页
else totalPage = (int) Math.floor( totalrecord / size ) + 1;
if(totalPage == 0) totalPage = 1;
temp.close();
try {
if(request.getParameter("p")==null || request.getParameter("p").equals(""))
p = 1;
else
p = Integer.parseInt(request.getParameter("p"));
}
// 捕获用户从浏览器地址拦直接输入非数字信息而引起的异常
catch(NumberFormatException e) {
p = 1;
}
if(p < 1) p = 1;
if(p > totalPage) p = totalPage;
rs=conn.executeQuery(sql1);
if(rs.next())
out.println(printPage(rs,p,size));
%>
</table>
<form Action="index.jsp" Method="GET">
<%
for(int i=1;i<=totalPage;i++) {
out.println("<a href=index.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();
session.setMaxInactiveInterval(-1);
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -