|
发表于 2011-11-4 10:29:18
|
显示全部楼层
Re:怎样在实现网站的搜索功能
搜索数据库的代码,仅供参考:
search.jsp部分代码:
<form method="post" name="myform" action="result.jsp?username=<%=username%>">
<td width="394" colspan="2" height="77">
资源搜索:
<select size="1" name="action" style="color:#008080;font-size:9pt">
<option value="title">资源名称</option>
<option value="content">资源简介</option>
</select>
<select name="classid" size="1" style="color:#008080;font-size:9pt">
<%
String sqlname = "select * from dbo.class";
ResultSet rs3 = stmt.executeQuery(sqlname);
while(rs3.next()){
%>
<option value="<%=rs3.getInt("classID")%>"><%=rs3.getString("class")%></option>
<%
}
rs3.close();
%>
</select>
<input type="text" name="keyword" class="smallinput" size="14" value="关键字" maxlength="50" style="color:#008080;font-size:9pt">
<input type="submit" name="submit2" value="搜索" style="height:21">
</td>
</form>
result.jsp部分代码:
<%
String sql="",bb="",classid="",nclassid="",classname="",nclassname="",lasthits="",sqlname="";
request.setCharacterEncoding("gb2312");
String username = request.getParameter("username");
String keyword = request.getParameter("keyword");
String keyword2 = request.getParameter("keyword2");
//System.out.println(keyword);
if((keyword==null) || (keyword.equals("")))
{
out.print("<script language=javascript>alert('請输入查询条件!');");
out.print("javascript:history.go(-1);</script>");
}
%>
<%
int intPageSize;
int intRowCount;
int intPageCount;
int intPage;
String strPage;
int i,j;
intPageSize=4;
strPage = request.getParameter("page");
if((strPage == null) || (strPage.equals("")))
{
intPage=1;
}
else
{
intPage = Integer.parseInt(strPage);
if(intPage < 1)
{
intPage=1;
}
}
if((keyword == null)||(keyword.equals("")))
{
sqlname = "select count(ID) from dbo.download";
}
else
{
sqlname = "select count(ID) from dbo.download where showname like '%"keyword"%' or note like '%"keyword"%'";
}
ResultSet rt = stmt.executeQuery(sqlname);
rt.next();
intRowCount = rt.getInt(1);
intPageCount = (intRowCount+intPageSize-1)/intPageSize;
if(intPage > intPageCount)
{
intPage = intPageCount;
}
if((keyword == null)||(keyword.equals("")))
{
sqlname = "select * from dbo.download order by ID desc";
}
else
{
sqlname = "select * from dbo.download where showname like '%"keyword"%' or note like '%"keyword"%' order by ID desc";
}
rs = stmt.executeQuery(sqlname);
i = (intPage-1)*intPageSize;
for(j=0;j<i;i++)
{
rs.next();
}
%> |
|