周未在家整理了一下JSP中的分页BEAN。加上数据库连接池
[在我的博客:http://blog.chinaunix.net/u2/84280/showart.php?id=2216913直接复制代码就行]
现在分享代码:
PageBean.java
package cn.ty.bean;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import cn.ty.tools.*;
/**
* @author hkebao@126.com
* @version 1.0
* 通用的MYSQL分页BEAN
* */
public class PageBean {
private int currentpage = 1;
private int totalPages = 0;
private String sqlConditions = ""; //查询的SQL条件
private int percentRecords = 1; //每页的记录数
private boolean hasNextPage = false; //是否有下一页
private boolean hasPreviousPage = false; //是否有前一页
private Statement statmeStatement;
private Connection connection;
private String tableName;
private String fieldName;
private String fileName;
private int recordCount;
private static ConnectionPool connPool;
//如果不是静态的话就每一个对象过来都得再重新创建!!非常重要的一点切记!
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public String getSqlConditions() {
return sqlConditions;
}
public void setSqlConditions(String sqlConditions) {
this.sqlConditions = sqlConditions;
}
public int getPercentRecords() {
return percentRecords;
}
public void setPercentRecords(int percentRecords) {
this.percentRecords = percentRecords;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getRecordCount() {
return recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
public PageBean(){
}
public void init() {
if (connPool == null) {
connPool = new ConnectionPool("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/cublog","root","123");
try {
connPool.setInitialConnections(10);
connPool.setMaxConnections(50);
connPool.createPool();
this.connection = connPool.getConnection();
this.statmeStatement = this.connection.createStatement();
} catch (Exception e) {
System.out.println(e.toString());
}
} else {
//如果连接池对象已经创建了就直接使用了。
try {
this.connection = connPool.getConnection();
this.statmeStatement = this.connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* @see: 计算出来总的页面
* */
private void getTotalPage() {
ResultSet rSet = null;
String sqlString = "select count(*) as total from " + tableName + " where " + sqlConditions;
try {
rSet = this.statmeStatement.executeQuery(sqlString);
while(rSet.next()) recordCount = Integer.parseInt(rSet.getString("total"));
if ((recordCount % this.percentRecords) == 0) {
totalPages = recordCount / this.percentRecords;
} else {
totalPages = recordCount / this.percentRecords + 1;
}
//判断是否有下一页
if (currentpage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
//判断是否有前一页
if ((currentpage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
rSet = null;
}
}
/**
* 计算当前所传入的字段、条件、表名、页数返回当前页的记录集.记录集合
* */
public List getCurrPageRecords() {
ResultSet rs = null;
List list = new ArrayList();
StringBuffer tmp = new StringBuffer();
if(currentpage < 1) { return list; //注意如果记录集为0的话这个为小于1的数字的可能 }
int limit = (currentpage - 1) * percentRecords;
tmp.append("select ");
tmp.append(fieldName);
tmp.append(" from ");
tmp.append(tableName);
tmp.append(" where ");
tmp.append(sqlConditions);
tmp.append(" limit " + String.valueOf(limit) + "," + percentRecords);
String sqlString = tmp.toString();
try {
rs = this.statmeStatement.executeQuery(sqlString);
list = convertList(rs);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 将ResultSet转换成List封装
* */
private List convertList(ResultSet rs) throws SQLException {
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()) {
Map rowData = new HashMap();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
}
return list;
}
/**
* 显示分页的条目 第一页..
* */
public String showPages() {
getTotalPage();
StringBuffer tmp = new StringBuffer();
tmp.append("共有文章"+String.valueOf(recordCount) + "篇");
tmp.append("第("+currentpage+"/"+totalPages+") ");
tmp.append("首页 ");
if (hasPreviousPage){
int prepage = currentpage - 1;
tmp.append("上一页 ");
}
else
tmp.append("上一页 ");
tmp.append("1 ");
tmp.append("2 ");
tmp.append("3 ");
tmp.append("4 ");
tmp.append("5 ..");
if (hasNextPage){
int prepage = currentpage + 1;
tmp.append("下一页 ");
}
else
tmp.append("下一页 ");
tmp.append("未页 ");
return tmp.toString();
}
/**
* 显示将数据库连接对象回收!记得要回收资源
* */
public void resetDB() {
connPool.returnConnection(this.connection);
this.connection = null;
this.statmeStatement = null;
}
}
|
以上就是分页的BEAN了。调用的JSP
showCU.jsp
如下:
<%@ page language="java" import="java.util.*;" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:useBean id="pagebean" scope="request" class="cn.ty.bean.PageBean" beanName="cn.ty.bean.PageBean"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
A:link {text-decoration: none}
A:visited {text-decoration: none}
A:active {text-decoration: none}
A:hover {font-size:24; font-weight:bold; color: red;}
</style>
</head>
<body>
<jsp:setProperty name="pagebean" property="currentpage"/>
<table border='1'>
<tr>
<td>ID</td>
<td>标题</td>
</tr>
<%
int currpage = 1;
try {
currpage = Integer.parseInt(request.getParameter("page"));
} catch(Exception e) {
currpage = 1;
}
pagebean.setSqlConditions("1=1");
pagebean.setPercentRecords(10);
pagebean.setFieldName("id,title");
pagebean.setFileName("showCU.jsp");
pagebean.setTableName("cu_blog_page");
pagebean.setCurrentpage(currpage);
pagebean.init();
List resultSet = pagebean.getCurrPageRecords();
Map rowData = new HashMap();
for (Iterator i = resultSet.iterator(); i.hasNext();) {
rowData = (Map)i.next();
out.print("" + String.valueOf(rowData.get("id")) + " | " + rowData.get("title") + " | ");
}
%>
</table>
<div>
<%
out.print(pagebean.showPages());//内容主体
%>
</div>
</body>
</html>
<%
resultSet = null;
pagebean.resetDB(); //记得将当前页面的开了的资源关闭掉
%>
|
OK!分页BEAN以后就直接使用这个代码就行了。
原创文章~~~如果大家发现问题请联系我。
阅读(811) | 评论(1) | 转发(0) |