jsp的分页查询代码(mysql数据库)

来源:互联网 发布:java http post 文件 编辑:程序博客网 时间:2024/04/30 04:03

      这个功能一共创建了两个javabean组件和一个jsp页面显示分页页面,第一个是处理以数据库连接的javabean,第一个javabean是处理分页查询结果的代码,第三个jsp是调用第二个javabean,显示分页查询结果!
      //下面是连接mysql数据库的一个javabean的代码:

package data;
import java.sql.*;

public class LoginData{
    Connection conn=null;
    public LoginData(){
              this.connect();   
    }
  
    public Connection getConn(){
            return this.conn;
    }
    public boolean connect(){
           try{
          //使用JDBC桥创建数据库连接
       Class.forName("org.gjt.mm.mysql.Driver").newInstance();
       
     //使用DriverManager类的getConnection()方法建立连接
     //第一个参数定义用户名,第二个参数定义密码
     this.conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/logindemo?useUnicode=true&characterEncoding=gb2312","root","123456");
      }catch(Exception ex){
           ex.printStackTrace();
     return false;
      }
      return true;
    }

      //下面是一个javabean的处理mysql数据库的分页显示查询的代码

package data;
import java.sql.*;
import java.util.*;
public class strongSplitPage
{
       private Connection conn=null;
    private Statement stmt=null;
    private ResultSet rs=null;
    private ResultSetMetaData rsmd=null;
    //sql 查询语句
    private String sqlStr;
    //总纪录数目
    private int rowCount;
    //所分得逻辑页数
    private int pageCount;
    //每页显示的纪录数目
    private int pageSize;
    //定义表的列数目
    private int columnCount;
    private int irows;
    public void initialize(String sqlStr,int pageSize,int showPage)
    {
            this.sqlStr=sqlStr;
      this.irows=pageSize*(showPage-1);
      this.pageSize=pageSize;
      try
      {
          LoginData loginData=new data.LoginData();
          this.conn=loginData.getConn();
       this.stmt=this.conn.createStatement();
       this.rs=this.stmt.executeQuery(this.sqlStr);
       this.rsmd=this.rs.getMetaData();
       if(this.rs!=null)
       {
          this.rs.last();
       this.rowCount=this.rs.getRow();
       this.rs.first();
       this.columnCount=this.rsmd.getColumnCount();
       this.pageCount=(this.rowCount-1)/this.pageSize+1;
       this.rs.close();
       this.stmt.close();
       }
       this.sqlStr=this.sqlStr+" limit "+this.irows+","+this.pageSize;
       this.stmt=this.conn.createStatement();
       this.rs=this.stmt.executeQuery(this.sqlStr);  
       }catch(Exception ex)
    {
              ex.printStackTrace();
        }
    }
    public Vector getPage()
    {
           Vector vData=new Vector();
     try
     {
         if(this.rs!=null)
      {
           
         while(this.rs.next())
      {    
             String[] sData=new String[this.columnCount];
          for(int j=0;j<this.columnCount;j++)
       {
               sData[j]=this.rs.getString(j+1);
          }
          vData.addElement(sData);
        }
        this.rs.close();
        this.stmt.close();
        this.conn.close();
       }
      }catch(Exception ex)
      {
          ex.printStackTrace();
      }
            return vData;
  }
      
     //获得页面总数
     public int getPageCount()
     {
             return this.pageCount;
     }
     //获得数据表中总纪录数
     public int getRowCount()
     {
             return this.rowCount;
     }
}

      //下面是jsp显示分页页面

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="data.*"%>
<jsp:useBean id="pages" scope="page" class="data.strongSplitPage" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%!
      //显示每页的纪录数
   int pageSize=10;
   String sqlStr="";
   //当前页
   int showPage=1;
%>

<%
      sqlStr="select * from userinfo order by id ";
   String strPage=null;
   //获得跳转到的页面 
   strPage=request.getParameter("showPage");    
   if(strPage==null){
      showPage=1;
   pages.initialize(sqlStr,pageSize,showPage);
   }else{
         try{
         showPage=Integer.parseInt(strPage);
      pages.initialize(sqlStr,pageSize,showPage);
   }catch(NumberFormatException ex){
          showPage=1;
        pages.initialize(sqlStr,pageSize,showPage);
   }
   if(showPage<1){
          showPage=1;
        pages.initialize(sqlStr,pageSize,showPage);
   }
   if(showPage>pages.getPageCount()){
           showPage=pages.getPageCount();
      pages.initialize(sqlStr,pageSize,showPage);
   }
   }
   //取得要显示的数据集合
   Vector vData=pages.getPage();  
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>分页显示</title>
</head>

<body bgcolor="#ffffff" text="#000000">
       <h1 align=center>个人基本信息</h1>
<div align=center>
    <table border="1" cellspacing="0" cellpadding="0" width="80%">
    <tr>
         <th width="20%">编号</th>
   <th width="40%">学号</th>
   <th width="40%">姓名</th>
    </tr>
    <%
          for(int i=0;i<vData.size();i++)
    {
          //显示数据数
       String[] sData=(String[])vData.get(i);
    %>
                 <tr>
           <td><%=sData[0]%></td>
        <td><%=sData[1]%></td>
        <td><%=sData[2]%></td>
     </tr>
  <%
       }
  %>      
    </table>
    <p>
  <form action="word_list_javabean.jsp" method="get" target="_self">
      <p>共<font color=red><%=pages.getRowCount()%></font>条&nbsp;<% =pageSize%>条/页&nbsp;&nbsp;第<font color=red><%=showPage%></font>页/共<font color=red><%=pages.getPageCount()%></font>页&nbsp; &nbsp;[<a href="word_list_javabean.jsp?showPage=1" target="_self">首页</a>]&nbsp;
       <%
       //判断“上一页”链接是否要显示
    if(showPage>1){
    %>
       [<a href="word_list_javabean.jsp?showPage=<%=showPage-1%>" target="_self">上一页</a>]&nbsp;
    <%
       }
       else{   
    %>
            [上一页]&nbsp;
  <%
         }
      //判断“下一页”链接是否显示
      if(showPage<pages.getPageCount())
      {
  %>   
    [<a href="word_list_javabean.jsp?showPage=<%=showPage+1%>" target="_self">下一页</a>]&nbsp;
    <%
       }
       else{   
    %>
            [下一页]&nbsp;
  <%
     }
  %>   
 
    [<a href="word_list_javabean.jsp?showPage=<%=pages.getPageCount()%>" target="_self">尾页</a>]&nbsp;转到
        <select name="select">
  <%
       for(int x=1;x<=pages.getPageCount();x++)
    {
  %>
            <option value="<%=x%>"
      <%
          if(showPage==x){
           out.println("selected");
        }  
      %> ><%=x%></option>
  <%
       }
  %>   
        </select>
        页&nbsp;&nbsp;&nbsp;
        <input type="submit" name="go" value="提交" />
    </p>
  </form>
    </p>
    </div>
</body>
</html>
 

原创粉丝点击