核心代码:
search.jsp
<body>
<form action="search_result.jsp" method="post" name="form1" onsubmit="return check()">
<table border="0" align="center" bgcolor="#0099FF">
<tr bgcolor="#FFFFFF">
<th>查询项目:</th>
<td>
<select name="item" size=1>
<option value="">请选择</option>
<option value="p_id">p_id</option>
<option value="p_type">p_type</option>
<option value="p_name">p_name</option>
</select>
</td>
<td>查询内容:</td>
<td><input type="text" name="content"/></td>
<td><input type="submit" name="submit" value="查询"/></td>
</tr>
</table>
</form>
</body>
search_result.jsp
<body>
<jsp:include page="search.jsp"></jsp:include>
<%
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String connectDB="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=ShopSystem";
try{
Class.forName(JDriver);
}catch(ClassNotFoundException e){
System.out.println("数据库加载失败");
System.exit(0);
}
try{
String user="sa";
String password="xuelong";
Connection con=DriverManager.getConnection(connectDB, user, password);
System.out.println("数据库连接成功");
Statement tmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//设置编码格式,处理中文乱码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String strItem = request.getParameter("item");
String strContent = request.getParameter("content");
String strSql="";
if(strItem==null || strItem==""){
strSql="SELECT p_id,p_type,p_name,p_price,p_quantity,p_time FROM product";
}else{
//指定查找。参考:www.cnblogs.com/sunada2005/p/3411873.html
strSql="SELECT p_id,p_type,p_name,p_price,p_quantity,p_time FROM product WHERE "+strItem.trim()+"='"+strContent.trim()+"'";
}
ResultSet rsAll=tmt.executeQuery(strSql);
%>
<table>
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品类别</td>
<td>商品价格</td>
<td>商品数量</td>
<td>上架日期</td>
</tr>
<!--实现分页查找 -->
<%
String str=request.getParameter("page");
if(str==null){
str="0";
}
int pagesize=10;
rsAll.last();
int recordCount = rsAll.getRow();
int maxPage=0;
maxPage=(recordCount%pagesize==0)?(recordCount/pagesize):(recordCount/pagesize+1);
int currentPage = Integer.parseInt(str);
if(currentPage<1){
currentPage=1;
}else if(currentPage>maxPage){
currentPage=maxPage;
}
rsAll.absolute((currentPage-1)*pagesize+1);
for(int i=1;i<=pagesize;i++){
%>
<tr>
<td><%=rsAll.getString("p_id") %></td>
<td><%=rsAll.getString("p_name") %></td>
<td><%=rsAll.getString("p_type") %></td>
<td><%=rsAll.getFloat("p_price") %></td>
<td><%=rsAll.getInt("p_quantity") %></td>
<td><%=rsAll.getString("p_time") %></td>
<td><a href="#">详情</a></td>
<td><a href="#">购买</a></td>
</tr>
<%
try{
if(!rsAll.next()){
break;
}
}catch(Exception e){
out.println(e.getMessage());
}
}
%>
</table>
<p align="center">
跳转到:<input type="text" name="page" size="3"/>当前页数:[<%=currentPage %>/<%=maxPage %>]
<%
if(currentPage>1){
%>
<a href="search_result.jsp?page=1">第一页</a>
<a href="search_result.jsp?page=<%=currentPage-1 %>">上一页</a>
<%
}
if(currentPage<maxPage){
%>
<a href="search_result.jsp?page=<%=currentPage+1 %>">下一页</a>
<a href="search_result.jsp?page=<%=maxPage %>">最后一页 </a>
</p>
<%
}
rsAll.close();
tmt.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
%>
</body>