공통 적 인 페이지 클래스 ~

4889 단어 sqljsp
지금 jsp 를 배우 고 있 습 니 다. 페이지 를 나 누 는 방법 을 생각 하고 있 습 니 다.인터넷 에서 페이지 를 나 누 는 방법 은 몇 가지 가 있 는 것 같다.나 는 방금 모든 기록 을 하나의 list 에 거꾸로 놓 고 마지막 으로 페이지 를 나 누 어 기록 을 꺼 내 려 고 생각 했다.할 수 있 지만 뭔 가 이상 한 것 같 아 요. 데이터 가 많 으 면 얼마나 긴 list 가 필요 해 요 ~ ~ 또 생각 이 있어 요.(학교 에 있 는 데이터베이스 가 너무 수준 이 없어 서 가 르 치 는 sql server 2000, top 은 자세히 말 하지 못 했 습 니 다 ~ ~. sqlserver 2000 과정 설계 로 인해 컴퓨터 에 중독 되 었 습 니 다. 원 망 스 럽 습 니 다.) 한참 동안 생각 했 지만 데이터베이스 종류 에 의존 하지 않 는 방법 이 생각 나 지 않 았 습 니 다. 자바 sql 가방 에 있 는 것 에 대해 잘 모 르 기 때 문 입 니 다.
    나중에 인터넷 에서 일반적인 방법 을 보 았 습 니 다. 이 방법 은 데이터 가 저 장 된 ResultSet 에서 안의 방법 으로 페이지 를 모 의 하여 매번 그 중에서 현재 페이지 의 데 이 터 를 꺼 내 는 것 입 니 다. 이런 방법 을 한 종류 에 봉 하면 ResultSet 을 조작 하 는 것 과 같 습 니 다. 효율 적 으로 는 상대 적 으로 높 지 않 지만 중용 성과 효율 을 고려 하면 비교적 절충 이 될 것 입 니 다.방법 이 죠. 물론 입 니 다. 구체 적 인 응용 은 실제 상황 에 따라 선택 하 는 것 이 좋 을 것 같 습 니 다.
    공 부 를 하 는 이상 이해 만 으로 는 안 됩 니 다. 실현 에 옮 겨 야 자신의 것 이 될 수 있 습 니 다. 자신 은 이런 방법 에 따라 이런 종 류 를 써 보 았 습 니 다. 세부 적 으로 엄밀 하지 않 을 수도 있 습 니 다. 경험 이 없 는 학생 에 속 하지만 페이지 기능 은 실현 되 었 습 니 다.
    인터페이스 Pageable 을 먼저 정 의 했 습 니 다.
public interface Pageable {

	public boolean gotoPage(int curPage) throws SQLException;

	public boolean beforeFirst() throws SQLException;

	public boolean first() throws SQLException;

	public boolean next() throws SQLException;
	
	public boolean isAfterLast();

	public int getTotalPage();

	public int getTotal();

	public int getCurPage();

	public int getPageNum();

}

 Pagination 을 통 해 구현:
import java.sql.ResultSet;
import java.sql.SQLException;

public class Pagination implements Pageable {

	protected ResultSet rs = null;
	
	protected int total;	//    
	protected int pageNum;	//       
	protected int curPage;	//     
	protected int row;		//        
	protected int rowFlag;	//      
	protected int totalPage;//   
	
	/*
	 *    ResultSet   ,      ,  getResultSet()     ResultSet
	 */
	public Pagination(ResultSet rs) throws SQLException{
		this.rs = rs;
	}
	/*
	 *         
	 */
	public Pagination(ResultSet rs,int pageNum) throws SQLException{
		
		this(rs);
		rs.last();
		this.total = rs.getRow();
		this.pageNum = pageNum;
		
		if(this.pageNum <= 0){ 
			throw new SQLException("         :" + this.pageNum);
		}
		
		if(total % pageNum == 0){
			this.totalPage = total / pageNum;
		}else{
			this.totalPage = total / pageNum + 1;
		}
	}
	
	
	/* 
	 *    curPage 
	 */
	public boolean gotoPage(int curPage) throws SQLException{
		this.curPage = curPage;
		
		if(this.curPage > this.totalPage){
			throw new SQLException("   " + this.curPage);
		}
		
		this.row = (curPage - 1) * this.pageNum + 1;
		return rs.absolute(row);
	}
	
	/* 
	 *               
	 */
	public boolean beforeFirst() throws SQLException{
		
		this.rowFlag = -1;
		return this.rs.absolute(this.row - 1);
	}
	/* 
	 *              
	 */
	public boolean first() throws SQLException{
		this.rowFlag = 0;
		return this.rs.absolute(this.row);
	}
	
	/* 
	 *         
	 */
	public boolean next() throws SQLException{
		
//		//             ,          ,     next
//		if(this.rowFlag > this.pageNum - 1){
//			return false;
//		} 
//		//                 ,            ,     next
//		if(total % pageNum != 0 && this.curPage == this.totalPage){
//			if(this.rowFlag > this.total % this.pageNum - 1){
//				return false;
//			}
//		}
		this.rowFlag++;
		return rs.next();
	}
	
	/*
	 *               
	 */
	public boolean isAfterLast(){
		//             ,          
		if(this.rowFlag > this.pageNum - 1){
			return true;
		}
		//                 ,            
		if(total % pageNum != 0 && this.curPage == this.totalPage){
			if(this.rowFlag > this.total % this.pageNum - 1){
				return true;
			}
		}
		return false;
	}
	
	/* 
	 * getter ...
	 */
	public ResultSet getResultSet(){
		return rs;
	}
	public int getTotalPage(){
		return this.totalPage;
	}

	public int getTotal(){
		return this.total;
	}
	
	public int getCurPage(){
		return this.curPage;
	}
	
	public int getPageNum(){
		return this.pageNum;
	}
	
}

 
    ResultSet 다 쓰 고 close () 기억 해! 계속 공부 중... 블 로그 채널 에 보 내 서 뭐 하 는 거 야?? 실험 해 봐.

좋은 웹페이지 즐겨찾기