当前位置: 代码迷 >> Eclipse >> jsp施用javabean连接数据库 分页显示
  详细解决方案

jsp施用javabean连接数据库 分页显示

热度:89   发布时间:2016-04-23 01:22:34.0
jsp使用javabean连接数据库 分页显示
showCourse.java文件
package Show;
import java.sql.*;
import com.sun.rowset.*;

public class showCourse{
int pageSize=10;
int pageAll=0;
int showPage=1;
StringBuffer pr;
CachedRowSetImpl rowSet;
String driverName="com.mysql.jdbc.Driver"; 
  String userName="";
  String userPasswd="";
  String dbName="";
  String tableName="";
  String i[]=new String[100];
  int j=0;
  public showCourse(){
   pr=new StringBuffer();
   try{
   Class.forName(driverName).newInstance();
   }catch(Exception e){
          e.printStackTrace();}
    }
  public void setPage(int size){
   pageSize=size;
   j=0;
  
    String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
    try{
     Connection conn=DriverManager.getConnection(url);
     DatabaseMetaData met=conn.getMetaData();
     ResultSet rs1=met.getColumns(null,null,tableName,null);
     int k=0;
     while(rs1.next()){
     j++;
     i[k]=rs1.getString(3);
     k++;
     }
     Statement sql=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
     ResultSet rs=sql.executeQuery("SELECT * FROM "+tableName);
     rowSet =new CachedRowSetImpl();
     rowSet.populate(rs);
     conn.close();
     rowSet.last();
     int m=rowSet.getRow();
     int n=pageSize;
     pageAll=((m%n)==0)?(m/n):(m/n+1);
    }catch(Exception e1){}
  }
  public int getPageSize(){
   return pageSize;
  }
  public int getPageAll(){
   return pageAll;
  }
  public void setShowPage(int n){
   showPage=n;
  }
  public int getShowPage(){
   return showPage;
  }
  public StringBuffer getPR(){
   if(showPage>pageAll)
      showPage=1;
   if(showPage<=0)
      showPage=pageAll;
    pr=show(showPage);
    return pr;
  }
  public StringBuffer show(int page){
   StringBuffer str=new StringBuffer();
   str.append("<table border=1>");
   str.append("<tr>");
   for(int t=0;t<j;t++){
   str.append("<th>"+i[t]+"</th>");
   }
   str.append("</tr>");
   try{
   rowSet.absolute((page-1)*pageSize+1);
   for(int t=1;t<=pageSize;t++){
   str.append("<tr>");
   for(int k=1;k<=j;k++){
   str.append("<td>"+rowSet.getString(k)+"</td>");
   }
  相关解决方案