📄 javabean实现数据库分页.txt
字号:
使用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);
}
}
}(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -