使用JSP的分页技术浏览数据库信息,请问如何实现??? 急急!!

bonnieweb 2001-09-15 03:13:39
比如数据库中有10000条记录,其中有5000条是我想要的,结果返回以后进行分页显示。而且有“上一页”、“下一页”等按钮,请问如何实现???

Bonnie

bonniemail@sina.com
...全文
213 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
leon_chi 2001-09-18
  • 打赏
  • 举报
回复
使用 Java Bean 实现数据分页显示
geoff [2001-07-26]

提交的前一个有误
使用bean可以简化页面代码,将逻辑封装到bean中,提高了代码的保密性和系统的安全性,另外由于jsp代码没有很好的调试环境,调试比较困难,逻辑代码封装到bean中还方便了程序的调试。
这个Bean 使用到了一个自己写的pool
com.cdc.database.util.*
输入参数:
setPage(String page) //当前页码取得(默认为第一页)
setPageSize(String size) //设定每页显示记录数目(默认为每页显示10条记录)

setSqlParam(String param) //检索返回字段(只有一个字段)

setSqlstr(String str) //取得sql语句的检索条件
setTable(String table) //设定检索数据表。

返回参数:

getData() //取得记录集(返回为hashtable, key为顺序号)
getPageCount() //返回总页码数

getRowCount() //总记录数

以下为源代码,请大家指正

package cdcapp;

/**
* 此处插入类型说明。
* 记录分页BEAN
* 创建日期:(2001-7-23 9:20:19)
* @author:Administrator
*/
import java.util.*;
import java.io.*;
import java.sql.*;
import com.cdc.database.util.*;
public class PagesBean {
int intPage; //当前页码
int intPageSize; //每页记录数
int intRowCount; //总记录数
int intPageCount ; //总页码数
String tablename; //数据表名
String sqlstr; //sql语句的检索条件
String sqlparam; //选取参数
/**
* 此处插入方法说明。
* 取得记录集(返回为hashtable, key为顺序号)
* 创建日期:(2001-7-24 11:19:56)
* @return java.util.Hashtable
*/
public Hashtable getData()
{

Hashtable returnrow = new Hashtable();
Connection conn = null;
try
{
String selectsql;
conn = LocalPool.getConnection(); //连接数据库
int RowCount = rowcount();
selectsql = selectstr();
intPageCount = (RowCount + intPageSize - 1) / intPageSize; //总页码数
PreparedStatement pstmt = conn.prepareStatement(selectsql);
ResultSet rs = pstmt.executeQuery();
if (intPage > RowCount)
intPage = intPageCount; //输入页码数大于总页码数的处理

if (intPageCount > 0)
{
int startrow = (intPage - 1) * intPageSize + 1; //开始显示记录数
for (int i = 0; i < startrow; i++)
{
rs.next();
} //将记录指针定位到待显示页的第一条记录上

//判断字段数据类型
//date型 返回 93
//int型 返回 2
//String型 返回 12
ResultSetMetaData columnType = rs.getMetaData();
int t = 0;
System.out.println("type=" + columnType.getColumnType(1));
if (columnType.getColumnType(1) == 12)
{

//输出数据
int i = 0;
while (i < intPageSize && !rs.isAfterLast())
{
String ResultSet = rs.getString(1);

//获取数据写入hashtable
returnrow.put(new Integer(i),ResultSet);
rs.next();
i++;
}

} else if (columnType.getColumnType(1) == 2)
{

//输出数据
int i = 0;
while (i < intPageSize && !rs.isAfterLast())
{
int ResultSet = rs.getInt(1);
//获取数据写入hashtable
returnrow.put(new Integer(i), new Integer(ResultSet));

rs.next();
i++;
}

} else if (columnType.getColumnType(1) == 93)
{

//输出数据
int i = 0;
while (i < intPageSize && !rs.isAfterLast())
{
String ResultSet = rs.getDate(1).toString();
//获取数据写入hashtable
returnrow.put(new Integer(i), ResultSet);

rs.next();
i++;
}
}

}
pstmt.close();

conn.close();

conn = null;

} catch (SQLException ex)
{

System.out.print("错误=");
ex.printStackTrace(System.out);
if (conn != null)
{

try
{

conn.close();

} catch (Exception e)
{

}

}
}
return returnrow;
}
/**
* 此处插入方法说明。
* 返回总页码数
* 创建日期:(2001-7-24 13:51:04)
* @return int
*/
public int getPageCount()
{
return intPageCount;
}
/**
* 此处插入方法说明。
* 总记录数
* 创建日期:(2001-7-24 13:50:09)
* @return int
*/
public int getRowCount()
{

return intRowCount;
}
/**
* 此处插入方法说明。
* 创建日期:(2001-7-23 10:03:58)
* @param args java.lang.String[]
*/
public static void main(String[] args)
{
PagesBean pb = new PagesBean();
pb.setPageSize("10");
pb.setSqlstr(" username like '%%' ");
//pb.setSqlstr(" 版次=10 ");
pb.setTable("t_user");
//pb.setTable("demo");
pb.setSqlParam("passwd");
//pb.setSqlParam("作者");
pb.setPage("2");
Hashtable myarray = pb.getData();
System.out.println("size=" + myarray.size());
int d = 1;
System.out.println("msg=" + myarray.get(new Integer(d)));
int cc = pb.rowcount();
System.out.println("rowcount=" + cc);
System.out.println("pagecount=" + pb.getPageCount());
String[] cq = new String[25];
System.out.println("lent=" + cq.length);
for (int i = 0 ;i<myarray.size();i++ )
{
System.out.println("msg"+i+"=" + myarray.get(new Integer(i)));

}
}
/**
* 此处插入方法说明。
* 返回记录总数。
* 创建日期:(2001-7-25 8:42:47)
* @return int

*/
private int rowcount()
{
Connection conn = null;
try
{
String countsql; //sql语句计算记录总数
if (sqlstr == null)
{
countsql = new String("select count(*) " + " from " + tablename);
} else
{
countsql =
new String("select count(*) " + " from " + tablename + " where " + sqlstr);
}
conn = LocalPool.getConnection(); //连接数据库
PreparedStatement pstmt = conn.prepareStatement(countsql);
ResultSet rs = pstmt.executeQuery();
rs.next();
intRowCount = rs.getInt(1); //总记录数
rs.close();
pstmt.close();
conn = null;

} catch (Exception ex)
{
System.out.println("错误!");
if (conn != null)
{

try
{

conn.close();

} catch (Exception e)
{

}

}
}
return intRowCount;
}
/**
* 此处插入方法说明。
* 返回查询sql语句.
* 创建日期:(2001-7-26 8:57:39)
* @return java.lang.String
*/
private String selectstr()
{
String sql;
if (sqlstr == null)
{
sql = new String("select " + sqlparam + " from " + tablename);
} else
{
sql =
new String(
"select " + sqlparam + " from " + tablename + " where " + sqlstr);
}
return sql;
}
/**
* 此处插入方法说明。
* 当前页码取得(默认为第一页)
* 创建日期:(2001-7-23 10:14:26)
* @param param int
*/
public void setPage(String page)
{
if (page.trim().length() == 0)//检查输入参数是否为空
{
this.intPage = 1;
} else if (page.trim().length() != 0)
{
this.intPage = Integer.parseInt(page);
}
if (this.intPage < 1)
this.intPage = 1;

}
/**
* 此处插入方法说明。
* 设定每页显示记录数目(默认为每页显示10条记录)
* 创建日期:(2001-7-23 10:19:06)
* @param pagenumber int
*/
public void setPageSize(String size)
{
if (size.trim().length() == 0)//检查输入参数是否为空
{
this.intPageSize = 10;
} else if (size.trim().length() != 0)
{
this.intPageSize = Integer.parseInt(size);
}
if (this.intPageSize < 1)
this.intPageSize = 10;

}
/**
* 此处插入方法说明。
* 检索返回字段(只有一个字段)
* 创建日期:(2001-7-24 18:08:02)
* @return java.lang.String
* @param param java.lang.String
*/
public void setSqlParam(String param)
{
if (param.trim().length() != 0)//检查输入参数是否为空
{
this.sqlparam = param.trim();
}

}
/**
* 此处插入方法说明。
* 取得sql语句的检索条件
* 创建日期:(2001-7-23 10:00:30)
* @param sqlstr java.lang.String
*/
public void setSqlstr(String str)
{
if (str.trim().length() != 0) //检查输入参数是否为空
{
this.sqlstr = str.trim();
} else
this.sqlstr = new String(" ");
}
/**
* 此处插入方法说明。
* 设定检索数据表。
* 创建日期:(2001-7-24 10:41:51)
* @param tablename java.lang.String
*/
public void setTable(String table)
{

if (table.trim().length() != 0) //检查输入参数是否为空
{
this.tablename = table.trim();
}
}
}


另外还用到localpool源代码如下

package com.cdc.database.util;




/**
* 在此处插入类型说明。
* 创建日期:(2000-5-16 14:01:56)
* @author:Administrator
*/
import java.sql.*;
import javax.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.*;
import javax.servlet.*;

public class LocalPool
{
private static java.util.ResourceBundle rb = null;
public static OracleConnectionCacheImpl odsU = null;
public static OracleConnectionCacheImpl ods = null;
static
{
try
{
//rb = java.util.ResourceBundle.getBundle("com.cdc.database.util.DatabaseConfig");
String url = "jdbc:oracle:thin:@192.9.200.76:1521:cdc";
String user = "cdc";
String pass = "cdc";
ods = new OracleConnectionCacheImpl();
odsU= new OracleConnectionCacheImpl();

//odsU.setURL(rb.getString("userdb_url"));
//odsU.setUser(rb.getString("userdb_user"));
//odsU.setPassword(rb.getString("userdb_pass"));

odsU.setURL(url);
odsU.setUser(user);
odsU.setPassword(pass);

//ods.setCacheScheme(OracleConnectionCacheImpl.FIXED_WAIT_SCHEME);
// Set the Max Limit
int nMin = 2;
int nMax = 5;
/*
try
{
nMin = Integer.parseInt(rb.getString("userdb_minlimit"));
nMax = Integer.parseInt(rb.getString("userdb_maxlimit"));
}
catch (Exception ex)
{
}
*/
odsU.setMaxLimit(nMax);
odsU.setMinLimit(nMin);

//ods.setURL(rb.getString("url"));
//ods.setUser(rb.getString("user"));
//ods.setPassword(rb.getString("pass"));
ods.setURL(url);
ods.setUser(user);
ods.setPassword(pass);
// Set the Max Limit
nMin = 2;
nMax = 5;
try
{
nMin = Integer.parseInt(rb.getString("minlimit"));
nMax = Integer.parseInt(rb.getString("maxlimit"));
}
catch (Exception ex)
{
}
ods.setMaxLimit(nMax);
ods.setMinLimit(nMin);
}
catch (Exception ex)
{

//throw ex;
ex.printStackTrace(System.out);
}
}
/**
* DBobj 构造子注解。
*/
public LocalPool() {
super();

}
/**
* 当将此对象作为垃圾收集时要执行的代码。
*
* 完成方法所抛出的任何异常都会导致完成过程
* 停止。但如果是其它异常,则将被忽略。
*/
protected void finalize() throws Throwable {
//在此处插入用来完成接收器的代码。
//此实现仅把信息转发给上一级。您可以替换或补充此实现。
super.finalize();
}
/**
* 在此处插入方法说明。
* 创建日期:(01-4-2 22:38:16)
* @return int
*/
static public int getActiveSize()
{
if(ods!=null)
{
return ods.getActiveSize();
}
else
{
return 0;
}
}
/**
* 在此处插入方法说明。
* 创建日期:(01-4-2 22:38:16)
* @return int
*/
static public int getActiveSizeU()
{
if(odsU!=null)
{
return odsU.getActiveSize();
}
else
{
return 0;
}
}
/**
* 在此处插入方法说明。
* 创建日期:(01-4-2 22:39:21)
* @return int
*/
static public int getCacheSize()
{
if(ods!=null)
{
return ods.getCacheSize();
}
else
{
return 0;
}
}
/**
* 在此处插入方法说明。
* 创建日期:(01-4-2 22:39:21)
* @return int
*/
static public int getCacheSizeU()
{
if(odsU!=null)
{
return odsU.getCacheSize();
}
else
{
return 0;
}
}
/**
* 在此处插入方法说明。
* 创建日期:(2000-5-16 14:27:57)
* @return java.sql.Connection
*/
static public Connection getConnection() throws SQLException
{
Connection conn=null;
try
{
if(ods!=null)
{
synchronized(ods)
{
conn = ods.getConnection();
return conn;
}
}
else
{
return null;
}
}
catch(SQLException ex)
{
throw ex;
}
catch (Exception e)
{
throw new SQLException("获取数据库连接时出现异常");
}
}
/**
* 在此处插入方法说明。
* 创建日期:(2000-5-16 14:27:57)
* @return java.sql.Connection
*/
static public Connection getConnectionU() throws SQLException
{
Connection conn=null;
try
{
if(odsU!=null)
{
synchronized(odsU)
{
conn = odsU.getConnection();
return conn;
}
}
else
{
return null;
}
}
catch(SQLException ex)
{
ex.printStackTrace(System.out);
throw ex;
}
catch (Exception e)
{
System.out.println("获取数据库连接时出现异常");
e.printStackTrace(System.out);
throw new SQLException("获取数据库连接时出现异常");
}
}
/**
* 在此处插入方法说明。
* 创建日期:(01-4-3 0:40:59)
*/
static public void reCreatePool() throws SQLException
{
try
{
OracleConnectionCacheImpl ods1 = new OracleConnectionCacheImpl();
ods1.setURL(rb.getString("url"));
ods1.setUser(rb.getString("user"));
ods1.setPassword(rb.getString("pass"));
// Set the Max Limit
int nMin = 50;
int nMax = 200;
try
{
nMin = Integer.parseInt(rb.getString("minlimit"));
nMax = Integer.parseInt(rb.getString("maxlimit"));
}
catch (Exception ex)
{
}
ods1.setMaxLimit(nMax);
ods1.setMinLimit(nMin);

OracleConnectionCacheImpl ods2=ods;
ods=ods1;
synchronized (ods2)
{
if(ods2!=null) ods2.close();
}
ods2=null;
}
catch(Exception ex)
{
ex.printStackTrace(System.out);
}
}
/**
* 在此处插入方法说明。
* 创建日期:(01-4-3 0:40:59)
*/
static public void reCreatePoolU() throws SQLException
{
try
{
OracleConnectionCacheImpl odsU1 = new OracleConnectionCacheImpl();
odsU1.setURL(rb.getString("userdb_url"));
odsU1.setUser(rb.getString("userdb_user"));
odsU1.setPassword(rb.getString("userdb_pass"));
//ods.setCacheScheme(OracleConnectionCacheImpl.FIXED_WAIT_SCHEME);
// Set the Max Limit
int nMin = 10;
int nMax = 100;
try
{
nMin = Integer.parseInt(rb.getString("userdb_minlimit"));
nMax = Integer.parseInt(rb.getString("userdb_maxlimit"));
}
catch (Exception ex)
{
}
odsU1.setMaxLimit(nMax);
odsU1.setMinLimit(nMin);
OracleConnectionCacheImpl odsU2 = odsU;
odsU = odsU1;
synchronized (odsU2)
{
if (odsU2 != null)
odsU2.close();
}
odsU2 = null;
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
}
}
yorkchen 2001-09-18
  • 打赏
  • 举报
回复
to leon_chi(leon_chi) 
每次查符合条件的
在稳定性上是没问题的
你稳定性(查询后的排序不一致)是指业务需求的稳定性吧(这可能就需要看需求是怎么定的了)
我一般不考虑 实际使用下来 感觉影响不大
效率的话就要看我们的结果集大小的假设了
(如果结果集是能控制(在一定范围内 300条左右)在jsp中可以将结果集存在session中 每次从sesion中取N条,数目很多的话每次去数据库取一点可能比较好些)

ifrank 2001-09-18
  • 打赏
  • 举报
回复
to yorkchen(天同) 
是一次查出存在缓冲区,还是每次查符合条件的?
如果数据库内的数据改变的特别快,或每次查询后的排序不一致,或翻页查询次数过多:
对稳定性和效率有没有影响?
yorkchen 2001-09-18
  • 打赏
  • 举报
回复
to leon_chi(leon_chi) 
我一般在数据库里解决分页问题
既SQL查询的是符合要求的第几条到第几条(用存储过程)
leon_chi 2001-09-18
  • 打赏
  • 举报
回复
这不是我写的代码,不过我看过很多相似的代码,都是这么做的。
我也就得这样很影响效率。请谈谈你的建议。
yorkchen 2001-09-18
  • 打赏
  • 举报
回复
to leon_chi(leon_chi) 
看了你的代码,我有个疑问 是不是每做一次操作(翻页),都将重新从数据库中把符合SQL查询要求的完整记录取回来吗?是不是会影响效率?
wjfling 2001-09-17
  • 打赏
  • 举报
回复
这个问题,不是一下子就能说清楚的,我刚写了代码,只是不在手头,如果想要的话可以和我联系,wjfling@163.com
hexiaofeng 2001-09-15
  • 打赏
  • 举报
回复
gz
leitm 2001-09-15
  • 打赏
  • 举报
回复
在容器中作个象数组一样的东西来识别数据。在页面上加上当前页和哪些被选中作为隐藏域。
虎叔 2001-09-15
  • 打赏
  • 举报
回复
一般是服务端实现!
也就是先用一个容器将你要的数据状进去,然后根据JSP的浏览的条件进行显示。容器可以考虑XML的DOM对象。

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧