日期:2014-05-16  浏览次数:20644 次

jsp单页面数据库查询模板:支持分页、排序、简单搜索、设置每页显示页数功能

不多说,直接贴代码

show.jsp(显示页面)

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="analysis.JDBCUtils"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'show.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  

  
  <body>
    <form action="servlet/DatabaseSearch" method="post">
    		<td> 输入搜索词 <input type="text" name="keyWord"></td>
    		<td><input type="submit" value="so easy" ></td>
    </form>
    <hr>
    <form action="servlet/DatabaseProcess" method="post">
   	 功能列表 : <a href="servlet/DatabaseProcess?choose=1&subchoose1=0">默认排序</a>
   	 		<a href="servlet/DatabaseProcess?choose=1&subchoose1=1">按价格降序排序</a>
   	 		<a href="servlet/DatabaseProcess?choose=1&subchoose1=2">按价格升序排序</a>
   	 价格区间<input  style="width:45px" type="text" name="minprice">-<input style="width:45px" type="text" name="maxprice">
   	 <input type="submit" value="Go" >
   	 </form>
	<hr><br>
    
    <!--   <tr><td>商品名称</td><td>价格</td><td>超链接</td></tr> -->
      <%
      	String temp = null;
      	String keyWord = (String) request.getSession().getAttribute("keyword");
      	//haven't input keyword
      	if(keyWord == null || keyWord.equals("")){
      		%><h2><font color="red">no keyword input</font></h2><%
      		return;
      	} 
      	
      	//the lack of the select sentence is for the easy use of the follow sql query
      	String sql = " from goods where name like '%"+keyWord+"%'";
      	
      	
      	//determin where is price in
      	String sqlBetween = "";
      	String minprice = (String)request.getSession().getAttribute("minprice");
      	String maxprice = (String)request.getSession().getAttribute("maxprice");    	
      	if(minprice!=null&&maxprice!=null){
      		sqlBetween = " and price>="+minprice+" and price<="+maxprice+" ";
      	}
      	
 	
      	
      	//determine which orderby to choose
      	String sqlOrderBy = "";
      	if((temp=(String)request.getSession().getAttribute("orderby"))!=null&&!temp.equals("0")){
	      	if(temp.equals("1")){
	      		sqlOrderBy