JSP+Servlet+javabean+oracle实现页面多条件模糊查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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+Servlet+javabean+oracle实现页面多条件模糊查询的主要内容,如果未能解决你的问题,请参考以下文章

代写Servlet代写JSP代写JavaBean程序作业

jsp+servlet+javaBean+Dao

JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)

jsp+servlet+javabean开发web项目

jsp+javabean+servlet实现登录功能user.java用来干啥?

Jsp+Servlet+JavaBean经典MVC模式理解