对于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;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
◆下面是一个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;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
◆下面是显示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﹥条 ﹤%=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﹥
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
以上就是在MYSQL数据库下的JSP分页查询的实现,希望对你有所帮助!
【编辑推荐】