jsp多条件查询,谁有完整的小项目,发给我一个,纠结中、或者帮忙看下哪儿出错了
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp多条件查询,谁有完整的小项目,发给我一个,纠结中、或者帮忙看下哪儿出错了相关的知识,希望对你有一定的参考价值。
<%
//获取搜索框中的值
String searchname=request.getParameter("search");
//获取省
String province=request.getParameter("s_province");
//获取市
String city=request.getParameter("s_city");
//获取县
String county=request.getParameter("s_county");
//获取下拉框中的值
String users=request.getParameter("users");
//连接数据库 进行搜索查询
Connection conn=null;
PreparedStatement pst=null;
ResultSet rst=null;
Store store=new Store();
List<Store> list=new ArrayList<Store>();
conn=HelpDB.getConnection();
pst=conn.prepareStatement("select name,remark from store where name like '%?%' and sheng like '%?%' and shi like '%?%' and xian like '%?%'");
pst.setString(1,searchname);
pst.setString(2,province);
pst.setString(3,city);
pst.setString(4,county);
rst=pst.executeQuery();
while(rst.next())
store.setName(rst.getString(1));
store.setRemark(rst.getString(2));
list.add(store);
//request.setAttribute(list);
response.sendRedirect("/jiagebaike/result/searchshops.jsp");
%>
searchshops.jsp页面
<c:forEach var="maile" items="$list" varStatus="num">
<div><a href="#">$maile.name</a></div>
<div>$maile.remark</div><br />
</c:forEach>
Store store=new Store();//把之前的这条语句放在这里
store.setName(rst.getString(1));
store.setRemark(rst.getString(2));
list.add(store);
每条记录应该是一个Store对象,你那样写实际上是一个对象保存一条数据,rs.next之后继续用同一个Store来保存 所以是最后一个.追问
~~~~(>_<)~~~~ 还是不对啊、、
追答//request.setAttribute(list);
response.sendRedirect("/jiagebaike/result/searchshops.jsp");
把这两行代码修改为
request.setAttribute(list);
request.getRequestDispatcher("/jiagebaike/result/searchshops.jsp").forward(request,response);
因该是pst=conn.createStatement();
rst=sm.executeQuery(sql);
JSP+Servlet+javabean+oracle实现页面多条件模糊查询
之前写过一篇JSP+Servlet+javabean+mysql实现页面多条件模糊查询 使用的是mysql进行的分页查询,mysql用limit控制,而oracle则是用rownum,今天第一次写oracle,查了一下资料试了一下,把代码帖出来
oracle的数据源配置也不同:
driverName=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
username=marketManager
pwd=marketManager
直接帖实现类(特别注意红色代码体):
package pb.market.com.cn.dao.impl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import pb.market.com.cn.dao.ProviderDao; import pb.market.com.cn.pojo.Provider; import pb.market.com.cn.util.BaseDao; public class ProviderDaoImpl extends BaseDao implements ProviderDao { //获取供应商列表 public List<Provider> getProviderList(int pageNo, int pageSize, String where) { List<Provider> providerList =new ArrayList<Provider>(); String sql="select * from ( select rownum as r,a.* from gongyingshang a where "+where+" rownum<=? ) where r>?"; Object[] params ={pageSize*pageNo,(pageNo-1)*pageSize}; if(this.getConnection()){ System.out.println(sql); ResultSet rs = this.executeQuery(sql, params); try { while(rs.next()){ Provider provider = new Provider(); provider.setId(rs.getInt("id")); provider.setGysno(rs.getInt("gysno")); provider.setGysname(rs.getString("gysname")); provider.setGystel(rs.getString("gystel")); provider.setGysfax(rs.getString("gysfax")); provider.setCreatedate(rs.getTimestamp("createdate")); provider.setCreateby(rs.getString("createby")); provider.setModifydate(rs.getTimestamp("modifydate")); provider.setModifyby(rs.getString("modifyby")); providerList.add(provider); } } catch (SQLException e) { e.printStackTrace(); }finally{ this.closeConnection(); } } return providerList; } /*public static void main(String[] args) { ProviderDao dd=new ProviderDaoImpl(); String where="金"; String where1="gysname like ‘%"+where+"%‘ and"; List<Provider> providerList =dd.getProviderList(1, 5, where1); for (Provider provider : providerList) { System.out.println("11111111111111"); System.out.println(provider.getId()); } }*/ //获取供应商总数量 public int getProviderCount(String where) { int count=0; String sql="select count(*)as count from gongyingshang "+where; Object[] params={}; if(this.getConnection()){ ResultSet rs =this.executeQuery(sql, params); try { if(rs.next()){ count = rs.getInt("count"); } } catch (SQLException e) { e.printStackTrace(); }finally{ this.closeConnection(); } } return count; } }
中间的service实现类,我的业务较简单,service层基本不做什么,只是调用一下dao层实现类的方法而已:
package pb.market.com.cn.service.impl; import java.util.List; import pb.market.com.cn.dao.ProviderDao; import pb.market.com.cn.dao.impl.ProviderDaoImpl; import pb.market.com.cn.pojo.Provider; import pb.market.com.cn.service.ProviderService; public class ProviderServiceImpl implements ProviderService { private ProviderDao providerDao; public ProviderServiceImpl(){ providerDao=new ProviderDaoImpl(); } public List<Provider> getProviderList(int pageNo, int pageSize, String where) { return providerDao.getProviderList(pageNo, pageSize, where); } public int getProviderCount(String where) { return providerDao.getProviderCount(where); } }
servlet:
package pb.market.com.cn.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import pb.market.com.cn.pojo.Provider; import pb.market.com.cn.service.ProviderService; import pb.market.com.cn.service.impl.ProviderServiceImpl; public class ProviderListServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); ProviderService providerService =new ProviderServiceImpl(); StringBuffer sb =new StringBuffer(1024); StringBuffer sb1 =new StringBuffer(1024); //获取模糊查询的值 String where = request.getParameter("proName"); //System.out.println("where================="+where); //如果为空 if(where!=null && !"".equals(where)){ sb.append("where gysname like ‘%"+where+"%‘"); sb1.append("gysname like ‘%"+where+"%‘ and"); } //设置每页显示3条 int pageSize=3; //得总条数 int totalCount =providerService.getProviderCount(sb.toString()); //得到总页数,如果总页数对一页的量取模==0,则。。。 int totalPageCount=0; if((totalCount%pageSize)==0){ totalPageCount=totalCount/pageSize; }else{ totalPageCount=totalCount/pageSize +1; } //获取当前页,默认一打开进来即是第1页 String pageIndex = request.getParameter("pageIndex"); //System.out.println("pageIndex==============="+pageIndex); int currentPageNo=0; if(pageIndex==null){ currentPageNo=1; }else{ //System.out.println("这里怎么可能进得来???????????"); currentPageNo= Integer.parseInt(pageIndex); } // 首页和尾页的异常控制 if (currentPageNo <= 0) { currentPageNo = 1; } else if (currentPageNo > totalPageCount) { currentPageNo = totalPageCount; } //把各个页码传给JSp request.setAttribute("pageSize", pageSize); request.setAttribute("totalCount", totalCount); request.setAttribute("totalPageCount", totalPageCount); request.setAttribute("currentPageNo", currentPageNo); //获取providerList List<Provider> providerList = providerService.getProviderList(currentPageNo, pageSize, sb1.toString()); request.setAttribute("providerList", providerList); request.getRequestDispatcher("/jsp/providerList.jsp").forward(request, response); }catch(Exception e){ e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
后面的JSP页面我就省略不写了。。。。。跟文头说的那篇mysql流程是一样,写法也类似
以上是关于jsp多条件查询,谁有完整的小项目,发给我一个,纠结中、或者帮忙看下哪儿出错了的主要内容,如果未能解决你的问题,请参考以下文章
wpf 从不同方向看一个model?比如,从正面,方面,上面等方向 谁有这方面的demo,发给我邮箱,万分感谢
JSP中,有很多的查询条件,比如最近一个月,或者租房的价格区间,等等条件,该如何实现条件查找?