数据库小技能:序列和伪列

176 阅读4分钟

“开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 5 天,点击查看活动详情

I 序列

序列是数据库中特有的一组能够实现自动增长的数字。

create sequence 序列名 increment by  每次增长的步长 start with 起始值。
select 序列名.nextval from dual.--获取序列的下一个值;获取序列的当前值采用currval属性。

II 伪列

oracle特有的默认提供给每一个表的,以便实现某些功能。

2.1 rownum

表示每条记录的序号(查到结果集后才分配的序号),常常用于分页。

2.2 rowid

表示每条记录的唯一标识(一组32为的字符串),可用来获取记录的物理地址。

III 例子

3.1 删除表中的相同记录

方法一:

delete from  t_table t1 where t1.rowid!=(select max(rowid) from t_table t2 where t1.id=t2.id and t.name=t2.name);;--其中 t1.id=t2.id and t.name=t2.name是定义相同记录的条件

方法二:

delete from t_table where roeid not in(select  max(rowid) from t_table group by 字段一,字段二);

3.2 删除表中不重复的记录

delete from  t_table t1 where t1.rowid=(select max(rowid) from t_table t2 where t1.id=t2.id and t.name=t2.name) and t1.rowid=(select min(rowid) from t_table t2 where t1.id=t2.id and t.name=t2.name) ;

3.3 分页

  1. 三目运算来计算总页数
 totalpage=sum/pagesize+sum%pagesize==0?0:1;//计算总页数,sum为总记录数
  1. 第page页的记录的起始位置和结束位置分别为:
pagesize*(page-1)+1;//起始位置

pagesize*page;//结束位置

  1. 可以使用差集(minus)在数据库查询中实现分页,但效率低。

  2. 常用子查询将rownum作为另一结果集的字段来实现分页。

select ee.* fromselect e.* , rownum rr from (select * from emp where sal is not  null order by sal ) e )ee where ee.rr berween &start and &end
  • rownum的分页地java代码示例
package com.backstage.biz.impl;
 
import java.util.List;
 
import com.backstage.bean.CardPage;
import com.backstage.bean.PlatePage;
import com.backstage.biz.CardContextBiz;
import com.backstage.dao.CardContextDao;
import com.backstage.dao.PlateContextDao;
import com.exception.DAOException;
import com.entity.MainCard;
import com.entity.Plate;
import com.factory.Factory;
import com.util.DeleteSpaceUtil;
 
public class CardContextBizImpl implements CardContextBiz {
	private int pageSize=10;
	private CardContextDao dao=(CardContextDao) Factory.getImpl("CardContextDaoImpl");//定义dao层
	public CardPage searchAllCard(int pageNum) throws DAOException {
		// 获取所有的帖子
		//计算分页的页面总数
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard ";
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		List list=dao.searchCardByPage(start,end);
		cardPage.setCard(list);
		return cardPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public CardContextDao getDao() {
		return dao;
	}
	public void setDao(CardContextDao dao) {
		this.dao = dao;
	}
	public CardPage searchCardById(int pageNum,long id) throws DAOException {
		//根据id获取主贴信息
		
		//计算分页的页面总数
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where cardId="+id;
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		List list=dao.searchCardByPage(start,end,id);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardBySendPeople(int pageNum, String name) throws DAOException {
		//根据发帖人获取主贴细信息
		//计算分页的页面总数
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where name='"+name+"'";
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		List list=dao.getCardByPageAndPeopleName(start,end,name);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardByTime(int pageNum, String time) throws DAOException {
		// 根据时间获取主贴
		//计算分页的页面总数
		if(time.contains(".")){
			time=time.substring(0,time.lastIndexOf("."));//去掉时间的秒的小数点
		}
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where time=to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where time=to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')"+") t) t1" +
				" where t1.r between  "+start+" and "+end;
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
//SQL> select * from emp where hiredate between (to_date('1981-2-20','yyyy-mm-dd')) and (to_date('1981-2-23','yyyy-mm-dd'));
	public CardPage searchAllCardByTimeInnerOneDay(int pageNum, String time) throws DAOException {
		//根据某个时间点的一天内查询的主贴
		//计算分页的页面总数
		if(time.contains(".")){
			time=time.substring(0,time.lastIndexOf("."));//去掉时间的秒的小数点
		}
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where time between to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')"+"and to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')+1";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where time between to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')"+"and to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')+1"+") t) t1" +
				" where t1.r between  "+start+" and "+end;
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardByOrderTime(int pageNum, String time) throws DAOException {
		// 根据查询某个时间点至今的有主贴,并按时间排序
		//计算分页的页面总数
		if(time.contains(".")){
			time=time.substring(0,time.lastIndexOf("."));//去掉时间的秒的小数点
		}
		
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where time between to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')"+"and sysdate";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where time between to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')"+"and sysdate"+") t) t1" +
				" where t1.r between  "+start+" and "+end+" order by time ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardBySubject(int pageNum, String subject) throws DAOException {
		//根据主题获取主贴信息
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where subject='"+subject+"'";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where subject='"+subject+"') t) t1" +
				" where t1.r between  "+start+" and "+end+" order by subject ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardByLikeSubject(int pageNum, String subject) throws DAOException {
		//根据帖子的主题查询主贴支持模糊查询
		subject=DeleteSpaceUtil.deleteSpace(subject);//除掉模糊查询的关键字的中间空白
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where subject like '%"+subject+"%'";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where subject like '%"+subject+"%') t) t1" +
				" where t1.r between  "+start+" and "+end+" order by time ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardByNum(int pageNum, int num) throws DAOException {
		//根据回帖数查询主贴
		
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where replyNum="+num;
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where replyNum="+num+") t) t1" +
				" where t1.r between  "+start+" and "+end+" order by replyNum ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardByGreateNum(int pageNum, int num) throws DAOException {
		// 
		if(num<0){
			num=0;
		}
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where replyNum>="+num;
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where replyNum>="+num+") t) t1" +
				" where t1.r between  "+start+" and "+end+" order by replyNum ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;		
	}
	public CardPage searchAllCardByPerfectCard(int pageNum, String perfectCard) throws DAOException {
		// 查看精品帖
		//进行页面显示的内容与数据库的表示转化
		int num=0;
		if("yes".equals(perfectCard)){
			String sql="select count(*) from tb_mainCard where perfectCard!="+num;
			System.out.println(sql);
			CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
			
			//System.out.println(sql);
			int totalCount=dao.getTotalCount(sql);
			int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
			//处理请求页面大于总页面的情况
			if(pageNum>totalPage){
				pageNum=totalPage;
			}
			if(pageNum<=0){
				pageNum=1;
			}
			cardPage.setTotalPage(totalPage);
			cardPage.setCurrPage(pageNum);
			if(totalCount==0){
				return cardPage;
			}
			//使用基于查询的分页
			int start=(pageNum-1)*this.getPageSize()+1;
			int end=pageNum*this.getPageSize();
			//调用dao层的方法获取显示数据
			String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where perfectCard!="+num+") t) t1" +
					" where t1.r between  "+start+" and "+end+"";
			List list=dao.getCard(sql2);
			cardPage.setCard(list);
			return cardPage;
 
		}else{
			String sql="select count(*) from tb_mainCard where perfectCard="+num;
			CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
			
			//System.out.println(sql);
			int totalCount=dao.getTotalCount(sql);
			int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
			//处理请求页面大于总页面的情况
			if(pageNum>totalPage){
				pageNum=totalPage;
			}
			if(pageNum<=0){
				pageNum=1;
			}
			cardPage.setTotalPage(totalPage);
			cardPage.setCurrPage(pageNum);
			if(totalCount==0){
				return cardPage;
			}
			//使用基于查询的分页
			int start=(pageNum-1)*this.getPageSize()+1;
			int end=pageNum*this.getPageSize();
			//调用dao层的方法获取显示数据
			String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where perfectCard="+num+") t) t1" +
					" where t1.r between  "+start+" and "+end+"";
			List list=dao.getCard(sql2);
			cardPage.setCard(list);
			return cardPage;
 
		}
	}
	public CardPage searchAllCardByPlateName(int pageNum, String plateName) throws DAOException {
		//根据板块获取主贴
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where plateName='"+plateName+"'";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where plateName='"+plateName+"') t) t1" +
				" where t1.r between  "+start+" and "+end+" order by replyNum ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public boolean deleteCardById(long id) throws DAOException {
		//删除主贴 
		int num =dao.deleteCardById(id);
		//System.out.println(num);
		if(num==1){
			return true;
		}else{
			return false;
		}
		
	}
	public boolean deleteCardById(String[] arrId) throws DAOException {
		//删除选中的主贴
		return dao.deleteCardByArrId(arrId);
	}
}
 list=dao.searchCardByPage(start,end);
		cardPage.setCard(list);
		return cardPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public CardContextDao getDao() {
		return dao;
	}
	public void setDao(CardContextDao dao) {
		this.dao = dao;
	}
	public CardPage searchCardById(int pageNum,long id) throws DAOException {
		//根据id获取主贴信息
		
		//计算分页的页面总数
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where cardId="+id;
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		List list=dao.searchCardByPage(start,end,id);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardBySendPeople(int pageNum, String name) throws DAOException {
		//根据发帖人获取主贴细信息
		//计算分页的页面总数
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where name='"+name+"'";
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		List list=dao.getCardByPageAndPeopleName(start,end,name);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardByTime(int pageNum, String time) throws DAOException {
		// 根据时间获取主贴
		//计算分页的页面总数
		if(time.contains(".")){
			time=time.substring(0,time.lastIndexOf("."));//去掉时间的秒的小数点
		}
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where time=to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where time=to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')"+") t) t1" +
				" where t1.r between  "+start+" and "+end;
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
//SQL> select * from emp where hiredate between (to_date('1981-2-20','yyyy-mm-dd')) and (to_date('1981-2-23','yyyy-mm-dd'));
	public CardPage searchAllCardByTimeInnerOneDay(int pageNum, String time) throws DAOException {
		//根据某个时间点的一天内查询的主贴
		//计算分页的页面总数
		if(time.contains(".")){
			time=time.substring(0,time.lastIndexOf("."));//去掉时间的秒的小数点
		}
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where time between to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')"+"and to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')+1";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where time between to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')"+"and to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')+1"+") t) t1" +
				" where t1.r between  "+start+" and "+end;
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardByOrderTime(int pageNum, String time) throws DAOException {
		// 根据查询某个时间点至今的有主贴,并按时间排序
		//计算分页的页面总数
		if(time.contains(".")){
			time=time.substring(0,time.lastIndexOf("."));//去掉时间的秒的小数点
		}
		
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where time between to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')"+"and sysdate";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where time between to_date('"+time+"','yyyy-mm-dd hh24:mi:ss')"+"and sysdate"+") t) t1" +
				" where t1.r between  "+start+" and "+end+" order by time ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardBySubject(int pageNum, String subject) throws DAOException {
		//根据主题获取主贴信息
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where subject='"+subject+"'";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where subject='"+subject+"') t) t1" +
				" where t1.r between  "+start+" and "+end+" order by subject ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardByLikeSubject(int pageNum, String subject) throws DAOException {
		//根据帖子的主题查询主贴支持模糊查询
		subject=DeleteSpaceUtil.deleteSpace(subject);//除掉模糊查询的关键字的中间空白
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where subject like '%"+subject+"%'";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where subject like '%"+subject+"%') t) t1" +
				" where t1.r between  "+start+" and "+end+" order by time ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardByNum(int pageNum, int num) throws DAOException {
		//根据回帖数查询主贴
		
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where replyNum="+num;
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where replyNum="+num+") t) t1" +
				" where t1.r between  "+start+" and "+end+" order by replyNum ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public CardPage searchAllCardByGreateNum(int pageNum, int num) throws DAOException {
		// 
		if(num<0){
			num=0;
		}
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where replyNum>="+num;
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where replyNum>="+num+") t) t1" +
				" where t1.r between  "+start+" and "+end+" order by replyNum ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;		
	}
	public CardPage searchAllCardByPerfectCard(int pageNum, String perfectCard) throws DAOException {
		// 查看精品帖
		//进行页面显示的内容与数据库的表示转化
		int num=0;
		if("yes".equals(perfectCard)){
			String sql="select count(*) from tb_mainCard where perfectCard!="+num;
			System.out.println(sql);
			CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
			
			//System.out.println(sql);
			int totalCount=dao.getTotalCount(sql);
			int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
			//处理请求页面大于总页面的情况
			if(pageNum>totalPage){
				pageNum=totalPage;
			}
			if(pageNum<=0){
				pageNum=1;
			}
			cardPage.setTotalPage(totalPage);
			cardPage.setCurrPage(pageNum);
			if(totalCount==0){
				return cardPage;
			}
			//使用基于查询的分页
			int start=(pageNum-1)*this.getPageSize()+1;
			int end=pageNum*this.getPageSize();
			//调用dao层的方法获取显示数据
			String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where perfectCard!="+num+") t) t1" +
					" where t1.r between  "+start+" and "+end+"";
			List list=dao.getCard(sql2);
			cardPage.setCard(list);
			return cardPage;

		}else{
			String sql="select count(*) from tb_mainCard where perfectCard="+num;
			CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
			
			//System.out.println(sql);
			int totalCount=dao.getTotalCount(sql);
			int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
			//处理请求页面大于总页面的情况
			if(pageNum>totalPage){
				pageNum=totalPage;
			}
			if(pageNum<=0){
				pageNum=1;
			}
			cardPage.setTotalPage(totalPage);
			cardPage.setCurrPage(pageNum);
			if(totalCount==0){
				return cardPage;
			}
			//使用基于查询的分页
			int start=(pageNum-1)*this.getPageSize()+1;
			int end=pageNum*this.getPageSize();
			//调用dao层的方法获取显示数据
			String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where perfectCard="+num+") t) t1" +
					" where t1.r between  "+start+" and "+end+"";
			List list=dao.getCard(sql2);
			cardPage.setCard(list);
			return cardPage;

		}
	}
	public CardPage searchAllCardByPlateName(int pageNum, String plateName) throws DAOException {
		//根据板块获取主贴
		CardPage cardPage=(CardPage) Factory.getImpl("CardPage");
		String sql="select count(*) from tb_mainCard where plateName='"+plateName+"'";
		//System.out.println(sql);
		int totalCount=dao.getTotalCount(sql);
		int totalPage=(totalCount/this.getPageSize())+(totalCount%this.getPageSize()==0?0:1);
		//处理请求页面大于总页面的情况
		if(pageNum>totalPage){
			pageNum=totalPage;
		}
		if(pageNum<=0){
			pageNum=1;
		}
		cardPage.setTotalPage(totalPage);
		cardPage.setCurrPage(pageNum);
		if(totalCount==0){
			return cardPage;
		}
		//使用基于查询的分页
		int start=(pageNum-1)*this.getPageSize()+1;
		int end=pageNum*this.getPageSize();
		//调用dao层的方法获取显示数据
		String sql2="select * from (select rownum r,t.* from ("+"select * from tb_mainCard where plateName='"+plateName+"') t) t1" +
				" where t1.r between  "+start+" and "+end+" order by replyNum ";
		List list=dao.getCard(sql2);
		cardPage.setCard(list);
		return cardPage;
	}
	public boolean deleteCardById(long id) throws DAOException {
		//删除主贴 
		int num =dao.deleteCardById(id);
		//System.out.println(num);
		if(num==1){
			return true;
		}else{
			return false;
		}
		
	}
	public boolean deleteCardById(String[] arrId) throws DAOException {
		//删除选中的主贴
		return dao.deleteCardByArrId(arrId);
	}
}