java分頁查詢(oracle)dao樣例

/**
	 * 根據用戶ID和時間範圍查詢條件得到相關記錄   
	 * @author Bimy
	 * @created 2015年6月23日
	 * @lastModified 
	 * @param empCode     員工ID
	 * @param beginDate   開始時間
	 * @param endDate     結束時間
	 * @param currentPage 當前頁面
	 * @param pageSize    每頁行數
	 * @return QueryResult
	 * void
	 */
	public QueryResult getPayInfoBySearch(String empCode,Date beginDate,Date endDate,int currentPage,int pageSize) throws Exception{
		QueryResult qr  = new QueryResult();
		String sql      = "select * from ... where em.emp_oid='"+empCode+"'";                  //根據員工ID設置查詢語句
		String countSql = "select count(*) totalCount from ... where em.emp_oid='"+empCode+"'; //設置查詢數目的語句
		if(beginDate!=null){    //如果有開始時間則加上開始時間約束
			String bgDate = format.format(beginDate);
			sql = sql + "and em.cost_datetime>=to_date('"+ bgDate +" 00:00:00', 'yyyy-mm-dd hh24:mi:ss')";
			countSql = countSql + "and em.cost_datetime>=to_date('"+ bgDate +" 00:00:00', 'yyyy-mm-dd hh24:mi:ss')";
		}
		if(endDate!=null){      //如果有結束時間則加上結束書劍約束
			String edDate = format.format(endDate);
			sql = sql + "and em.cost_datetime<=to_date('"+ edDate +" 23:59:59', 'yyyy-mm-dd hh24:mi:ss')";
			countSql = countSql + "and em.cost_datetime<=to_date('"+ edDate +" 23:59:59', 'yyyy-mm-dd hh24:mi:ss')";
		}		
		Connection conn = DBConnection.getConnection();
		Statement stmt  = conn.createStatement();
		ResultSet countRS = stmt.executeQuery(countSql);			
		if(countRS.next()) {    //根據查詢的數目填寫分頁的頁碼信息
			qr.setTotalCount(countRS.getInt("totalCount"));			
			qr.setMaxPage((int)Math.ceil((double)countRS.getInt("totalCount")/(double)pageSize)); 
			qr.setCurrentPage(currentPage);
			qr.setPageSize(pageSize);
			if(qr.getMaxPage() < qr.getCurrentPage()){  //若最大可先顯示頁碼小於當前應頁碼則直接返回
				return qr;
			}
		}else{
			return null;
		}
		//進行分頁查詢
		String querySql = "select * from(select * from(select t.*,row_number() over(order by COST_DATETIME desc) as rownumber from("+ sql +") t) p where p.rownumber>"+((currentPage-1)*pageSize)+") where rownum<="+pageSize+"";
		ResultSet queryRS = stmt.executeQuery(querySql);
		List<Object> resultList = new ArrayList<Object>();
		String account ="";
		String description ="";
		String payMoney ="";
		String originalMoney ="";
		String payTime ="";
		while(queryRS.next()){    //將查詢結果放入VO中
			PayRecordVO record = new PayRecordVO();
			account       = queryRS.getString("COST_REMAIN");
			description   = queryRS.getString("COST_DESNAME");
                        ...
			record.setEmpCode(empCode);
                        ...
            resultList.add(record);
		}
		qr.setResultList(resultList);
		stmt.close();
		conn.close();
		return qr;
	}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章