温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL数据库的JSP分页查询显示的代码

发布时间:2021-09-16 10:40:54 阅读:119 作者:chen 栏目:编程语言
亿速云云数据库,读写分离,安全稳定,弹性扩容,低至0.3元/天!! 点击查看>>

这篇文章主要讲解了“MySQL数据库的JSP分页查询显示的代码”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MySQL数据库的JSP分页查询显示的代码”吧!

对于JSP的学习者MySQL并不陌生,那么如何JSP分页查询模块的实现呢,让我们开始吧!

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

◆下面是连接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数据库的JSP分页查询显示的代码

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();         thisthis.stmt=this.conn.createStatement();         thisthis.rs=this.stmt.executeQuery(this.sqlStr);         thisthis.rsmd=this.rs.getMetaData();         if(this.rs!=null)         {            this.rs.last();         thisthis.rowCount=this.rs.getRow();         this.rs.first();         thisthis.columnCount=this.rsmd.getColumnCount();         this.pageCount=(this.rowCount-1)/this.pageSize+1;         this.rs.close();         this.stmt.close();         }         thisthis.sqlStr=this.sqlStr+" limit "+this.irows+","+this.pageSize;         thisthis.stmt=this.conn.createStatement();          thisthis.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"﹥         ﹤h2 align=center﹥个人基本信息﹤/h2﹥  ﹤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﹥条 ﹤%=pageSize%﹥条/页  第﹤font color=red﹥﹤%=showPage%﹥﹤/font﹥页/共﹤font color=red﹥﹤%=pages.getPageCount()%﹥﹤/font﹥页  [﹤a href="word_list_javabean.jsp?showPage=1" target="_self"﹥首页﹤/a﹥]          ﹤%         //判断“上一页”链接是否要显示      if(showPage﹥1){      %﹥         [﹤a href="word_list_javabean.jsp?showPage=﹤%=showPage-1%﹥" target="_self"﹥上一页﹤/a﹥]       ﹤%         }          else{          %﹥              [上一页]     ﹤%           }        //判断“下一页”链接是否显示        if(showPage﹤pages.getPageCount())        {     %﹥          [﹤a href="word_list_javabean.jsp?showPage=﹤%=showPage+1%﹥" target="_self"﹥下一页﹤/a﹥]       ﹤%         }          else{          %﹥              [下一页]     ﹤%       }    %﹥             [﹤a href="word_list_javabean.jsp?showPage=﹤%=pages.getPageCount()%﹥" target="_self"﹥尾页﹤/a﹥] 转到          ﹤select name="select"﹥    ﹤%         for(int x=1;x﹤=pages.getPageCount();x++)      {     %﹥              ﹤option value="﹤%=x%﹥"       ﹤%            if(showPage==x){             out.println("selected");          }           %﹥ ﹥﹤%=x%﹥﹤/option﹥    ﹤%         }    %﹥              ﹤/select﹥          页             ﹤input type="submit" name="go" value="提交" /﹥      ﹤/p﹥    ﹤/form﹥      ﹤/p﹥      ﹤/div﹥  ﹤/body﹥  ﹤/html﹥

感谢各位的阅读,以上就是“MySQL数据库的JSP分页查询显示的代码”的内容了,经过本文的学习后,相信大家对MySQL数据库的JSP分页查询显示的代码这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×