这段时间一直在做一个和查询有关的项目。因为操作的数据库是informix的。所以以前写的操作底层数据库的代码不能使用了。很希望能够实现分批对informix数据进行查询,但是实验了好多次都不行,编写的SQL语句无法在informix数据库里编译通过。最后只好用了最笨的办法,一次性把所有数据都查询出来,存放在list里,在底层操作的时候,进行分页 处理。
以下是代码:
JdbcDAO.java是操作数据的底层类
参数说明:HttpServletRequst request 为了获取session里保存的pager的值
String sql 需要进行分页的SQL语句
String viewPage,String action 分页有关的参数
Object object 最后转换的PSwitchList
public void JDBCOperationQuery2(HttpServletRequest request,String sql,String viewPage,String action,Object object) { if (sql == null) { throw new RuntimeException("SQL must not be null"); } if (logger.isInfoEnabled()) { logger.info("Executing SQL statement [" + sql + "]"); } Connection conn = null; Pageable rs = null;//改进的地方:Pageable继承了ResultSet PreparedStatement pstmt = null; PSwitchList rl = (PSwitchList) object; try { conn = super.getConnection(); pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);//这里的属性一定要记得修改 rs=new PageableResultSet(pstmt.executeQuery());//得到ResultSet HttpSession session = request.getSession(); Pager pager = (Pager)session.getAttribute("pager"); if(pager==null) pager = new Pager(); rs.setPageSize(pager.getPageSize());//pageSize 每页显示数据条数,默认为10条记录 pager.init(rs.getRowsCount(),pager.getPageSize()); if (action != null) { // 根据传递进来的参数控制页面的前进后退 if (action.equalsIgnoreCase("previous")) { pager.previous(); } else if (action.equalsIgnoreCase("next")) { pager.next(); } else if (action.equalsIgnoreCase("first")) { pager.first(); } else if (action.equalsIgnoreCase("last")) { pager.last(); } } rs.gotoPage(pager.getCurrentPage());//当前页数 rl.processRow(rs); } catch (SQLException e) { e.printStackTrace(); logger.error("查询出错-------", e); } finally { this.closeResultSet(rs); this.closeStatement(pstmt); this.closeConnection(conn); } }
protected abstract class PSwitchList { public abstract void processRow(Pageable rs) throws SQLException; }
-----DAOImpl
//列表信息 public List getPolicynoInfoList(HttpServletRequest request, String sql, String viewPage, String action) { // TODO Auto-generated method stub final List resultList = new ArrayList(); super.JDBCOperationQuery2(request,sql,viewPage,action, new PSwitchList() { public void processRow(Pageable rs) throws SQLException { for (int i = 0; i < rs.getPageRowsCount(); i++) { PolicynoInfoBean policynoInfoBean=new PolicynoInfoBean(); policynoInfoBean.setPolicyno(rs.getString("policyno")); policynoInfoBean.setClasscode(rs.getString("classcode")); policynoInfoBean.setTimestr(rs.getString("timestr")); policynoInfoBean.setAppno(rs.getString("appno")); policynoInfoBean.setPolist(rs.getString("polist")); policynoInfoBean.setDescribe(rs.getString("describe")); policynoInfoBean.setEmpno(rs.getString("empno")); if(rs.getDate("appdate")!=null && !String.valueOf(rs.getDate("appdate")).trim().equals("null")){ policynoInfoBean.setAppdate(DataDealWith.getDateFormat(rs.getDate("appdate"))); }else{ policynoInfoBean.setAppdate(""); } resultList.add(policynoInfoBean); rs.next();//不要忘记next() } } }); return resultList; }
----action里的代码
//第1次点查询按钮
String sql ="select riskcon.policyno,riskcon.classcode,riskclass.timestr,appno," + "polist,substr(describ,1,8) as describ,appdate " + "from riskcon,riskclass,policyst " + "where polist=policystat"+ "and riskcon.classcode=riskclass.classcode " + strSql +"order by 6 desc,1"; Pager pager = (Pager)request.getSession().getAttribute("pager"); int pageSize = 10;//每页显示10条信息 if(pager!=null) pageSize = pager.getPageSize(); request.getSession().removeAttribute("pager"); Pager page = new Pager(); page.setCurrentPage(1); page.setPageSize(pageSize); request.getSession().setAttribute("pager", page); String viewPage = (String) request.getParameter("viewPage"); String action = (String) request.getParameter("action"); List resultList = new ArrayList(); resultList = policynoInfoDAO.getPolicynoInfoList(request, sql, viewPage, action); if(resultList.size()>0){ request.setAttribute("resultList", resultList); request.getSession().setAttribute("sql", sql);//为分页的调用 } return mapping.findForward("SUCCESS");
//分页进行查询 public ActionForward pageSelectList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String viewPage = (String) request.getParameter("viewPage"); String action = (String) request.getParameter("action"); String sql = (String)request.getSession().getAttribute("sql"); PolicynoInfoDAOInter policynoInfoDAO = DAOFactory.getPolicynoInfoDAO(); List resultList = new ArrayList(); resultList = policynoInfoDAO.getPolicynoInfoList(request, sql, viewPage, action); if(resultList.size()>0) { request.setAttribute("resultList", resultList); request.getSession().setAttribute("sql", sql); } return mapping.findForward("SUCCESS"); }