jsp分页代码(jsp+JavaBean)

ShowRecordByPage.java
package database.operation;
import java.sql.*;
import com.sun.rowset.*;
public class ShowRecordByPage
{   int pageSize=10;                      //每页显示的记录数
    int pageAllCount=0;                   //分页后的总页数
    int showPage=1   ;                   //当前显示页
    StringBuffer presentPageResult;      //显示当前页内容
    CachedRowSetImpl rowSet;            //用于存储ResultSet对象
    String databaseName="";             //数据库名称
    String tableName="";                //表的名字
    String user=""       ;             //用户
    String password="" ;               //密码
    String 字段[]=new String[100]  ;
    int 字段个数=0;
    public ShowRecordByPage()
    {  presentPageResult=new StringBuffer();
       try{  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
          }
       catch(Exception e){}
    }
    public void setPageSize(int size)
    {  pageSize=size;
       字段个数=0;
       String uri="jdbc:sqlserver://127.0.0.1:1433;DatabaseName="+databaseName;
       try{  Connection con=DriverManager.getConnection(uri,user,password);
             DatabaseMetaData metadata=con.getMetaData();
             ResultSet rs1=metadata.getColumns(null,null,tableName,null);
             int k=0;
             while(rs1.next())
              { 字段个数++;
                字段[k]=rs1.getString(4); //获取字段的名字
                k++;
              }
            Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
            ResultSet rs=sql.executeQuery("SELECT * FROM "+tableName);
            rowSet=new CachedRowSetImpl();  //创建行集对象
            rowSet.populate(rs);
            con.close();                  //关闭连接
            rowSet.last();
            int m=rowSet.getRow();    //总行数
            int n=pageSize;
            pageAllCount=((m%n)==0)?(m/n):(m/n+1);
           }
        catch(Exception exp){}
    }
    public int getPageSize()
    {  return pageSize;
    }
    public int getPageAllCount()
    {  return pageAllCount;
    }
    public void setShowPage(int n)
    {  showPage=n;
    }
    public int getShowPage()
    { return showPage;
    }
    public StringBuffer getPresentPageResult()
    {  if(showPage>pageAllCount)
          showPage=1;
       if(showPage<=0)
          showPage=pageAllCount;
       presentPageResult=show(showPage);
       return presentPageResult;
    }
    public StringBuffer show(int page)
    {  StringBuffer str=new StringBuffer();
       str.append("<table border=1>");
       str.append("<tr>");
       for(int i=0;i<字段个数;i++)
       {  str.append("<th>"+字段[i]+"</th>");
       }
       str.append("</tr>");
       try{  rowSet.absolute((page-1)*pageSize+1);
             for(int i=1;i<=pageSize;i++)
              { str.append("<tr>");
                 for(int k=1;k<=字段个数;k++)
                  { str.append("<td>"+rowSet.getString(k)+"</td>");
                  }
                str.append("</tr>");
                rowSet.next();
              }
          }
       catch(SQLException exp){}
       str.append("</table>"); 
       return str;
    }
   public void setDatabaseName(String s)
    {  databaseName=s.trim();
    }
    public String getDatabaseName()
    {  return databaseName;
    }
    public void setTableName(String s)
    {  tableName=s.trim();
    }
    public String getTableName()
    {  return tableName;
    }
    public void setPassword(String s)
    {  password=s.trim();;
    }
    public void setUser(String s)
    {  user=s.trim();
    }
    public String getUser()
    {  return user;
    }
}  

showByPage.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<%@ page import="database.operation.*" %>
<jsp:useBean id="look" class="database.operation.ShowRecordByPage" scope="session" />
<jsp:setProperty  name="look"  property="databaseName" value="factory" />
<jsp:setProperty  name="look"  property="tableName" value="employee" />
<jsp:setProperty  name="look"  property="user" value="sa" />
<jsp:setProperty  name="look"  property="password" value="sa" />
<jsp:setProperty  name="look" property="pageSize" value="2" />
<HTML><BODY>
     数据库
     <jsp:getProperty  name= "look" property="databaseName"/>中
     <jsp:getProperty  name= "look" property="tableName"/>表的记录将被分页显示。
<br>共有 <jsp:getProperty name="look" property="pageAllCount"/> 页.
<br>每页最多显示<jsp:getProperty  name="look"  property="pageSize" />条记录。
  <jsp:setProperty  name= "look"  property="showPage"  />
  <jsp:getProperty  name= "look"  property="presentPageResult" />
<BR>当前显示第 <jsp:getProperty  name= "look"  property="showPage"  /> 页,
<BR>单击“前一页”或“下一页”按纽查看记录
<Table>
  <tr><td><FORM action="">
          <Input type=hidden name="showPage" value="<%=look.getShowPage()-1 %>" >
          <Input type=submit name="g" value="前一页">
          </FORM>
      </td>
      <td><FORM action="">
          <Input type=hidden name="showPage" value="<%=look.getShowPage()+1 %>" >
          <Input type=submit name="g" value="后一页">
          </Form>
      </td>
      <td> <FORM action="">
           输入页码:<Input type=text name="showPage" size=5 >
           <Input type=submit name="g" value="提交">
           </FORM>
      </td>
  </tr>
  </Table>
</BODY></HTML>
转载:校园生活(www.xysh123.com)

文章出处:http://www.xysh123.com/bbs/viewTopic.do?topicId=305

你可能感兴趣的:(sql,jsp,jdbc,生活,bbs)