数据库访问后数据分页显示

包:
import com.sun.rowset.*;

函数:
public StringBuffer show(int page,int pageSize,CachedRowSetImpl rowSet){
StringBuffer str=new StringBuffer();
try{
rowSet.absolute((page-1)*pageSize+1);
for(int i=1;i<=pageSize;i++){
str.append("");
for(int k=1;k<=字段个数;k++){
str.append(""+rowSet.getString(k)+"");
}
str.append("");
rowSet.next();
}
}catch(SQLException e){}
return str;
}

用doPost方法完成库访问和分页处理
/**
* The doPost method of the servlet.

*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");
	
	Connection con;
	StringBuffer presentPageResult=new StringBuffer();
	ShowRecordByPage databaseBean=null;
	HttpSession session=request.getSession(true);
	try{
		//创建javaBean对象,存放所有数据,包括jsp页bean中的数据,servlet处理生成的数据
		databaseBean=(ShowRecordByPage)session.getAttribute("database");
		if(databaseBean==null){
			databaseBean=new ShowRecordByPage();//创建
			session.setAttribute("database", databaseBean);
		}			
	}catch(Exception e){
		System.out.print(e.getStackTrace().toString());			
	}
	
	String databaseName=request.getParameter("databaseName");
	String tableName=request.getParameter("tableName");
	String ps=request.getParameter("pageSize");
	
	if(ps!=null){
		try{
			int mm=Integer.parseInt(ps);
			databaseBean.setPageSize(mm);
		}catch(NumberFormatException e){
			databaseBean.setPageSize(1);
			System.out.print(e.getStackTrace().toString());			
		}			
	}
	
	int showPage=databaseBean.getShowPage();
	int pageSize=databaseBean.getPageSize();
	boolean boo=databaseName!=null
			&&tableName!=null
			&&databaseName.length()>0
			&&tableName.length()>0;
	if(boo){
		databaseBean.setDatabaseName(databaseName);
		databaseBean.setTableName(tableName);
		//参考String uri="jdbc:sqlserver://10.220.1.37:1433;DatabaseName="+dbName;
		String uri="jdbc:sqlserver://10.254.238.42:1433;DatabaseName="+databaseName;
		try{
			字段个数=0;
			con=DriverManager.getConnection(uri,"sa","superdna_0");
			DatabaseMetaData metaData=con.getMetaData();
			ResultSet rs1=metaData.getColumns(null, null, tableName, null);
			int k=0;
			String 字段[]=new String[100];
			while(rs1.next()){
				字段个数++;
				字段[k]=rs1.getString(4);		//获取表头
				k++;
			}
			
			StringBuffer str=new StringBuffer();
			str.append("");
			for(int i=0;i<字段个数;i++){
				str.append(""+字段[i]+"");
			}
			str.append("");
			
			databaseBean.setFormTitle(str);
			
			Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			//测试时,只取1005条记录
			ResultSet rs=sql.executeQuery("select top 1005 * from "+tableName);
			rowSet=new CachedRowSetImpl();	//创建行集对象
			rowSet.populate(rs);			//获取表中数据			
			con.close();
			
			databaseBean.setRowSet(rowSet);
			rowSet.last();
			
			int m=rowSet.getRow();			//总行数
			int n=pageSize;					//每页行数
			int pageAllCount=((m%n)==0)?(m/n):(m/n+1); //总页数				
			databaseBean.setPageAllCount(pageAllCount);		
			
		}catch(SQLException e){
			
		}
	}//end if
	
	String whichPage=request.getParameter("whichPage");
	if(whichPage==null||whichPage.length()==0){
		showPage=1;
		databaseBean.setShowPage(showPage);
		CachedRowSetImpl rowSet=databaseBean.getRowSet();
		if(rowSet!=null){
			presentPageResult=show(showPage,pageSize,rowSet);
			databaseBean.setPresentPageResult(presentPageResult);
		}
	}else if(whichPage.equals("nextPage")){
		showPage++;
		if(showPage>databaseBean.getPageAllCount())
			showPage=1;
		databaseBean.setShowPage(showPage);
		CachedRowSetImpl rowSet=databaseBean.getRowSet();
		if(rowSet!=null){
			presentPageResult=show(showPage,pageSize,rowSet);
			databaseBean.setPresentPageResult(presentPageResult);
		}
	}else if(whichPage.equals("previousPage")){
		showPage--;
		if(showPage<=0)
			showPage=databaseBean.getPageAllCount();
		databaseBean.setShowPage(showPage);
		CachedRowSetImpl rowSet=databaseBean.getRowSet();
		if(rowSet!=null){
			presentPageResult=show(showPage,pageSize,rowSet);
			databaseBean.setPresentPageResult(presentPageResult);
		}
	}
	
	databaseBean.setPresentPageResult(presentPageResult);
	RequestDispatcher dispatcher=request.getRequestDispatcher("/showRecord.jsp");
	dispatcher.forward(request, response);
	
}

你可能感兴趣的:(数据库)