这个是分页工具类
package www.csdn.net.jquery.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* @author Hongyu
*
* @param <T>
*/
public class PageNation<T> {
// 当前页
private Integer nowPage;
// 页面要显示信息条数
private Integer pageSize;
// 根据页面显示的条数计算总页数
private Integer countPage;
// 根据传入的数据库查询数据库中的信息的条数
private Integer countSize;
// 向数据库查询时的开始的下标
private Integer startIndex;
// 向数据库查询时的查询条数
private Integer endIndex;
// 回传到页面上的开始的下标
private Integer startRow;
// 回传到页面上的结束的下标
private Integer endRow;
// 将查询到的数据存放到这里
private List<T> rows;
// 定义数据中操作的数据
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
public PageNation(Integer nowPage, Integer pageSize, String tableName) {
// 赋值
this.nowPage = nowPage;
this.pageSize = pageSize;
// 向数据库发送查询,查询出指定表格的数据的总数
this.countSize = this.getCountSize(tableName);
// 计算总页数
this.countPage = this.countSize % this.pageSize == 0 ? this.countSize
/ this.countSize : this.countSize / this.pageSize + 1;
// 判断当前也是否合法
if (this.nowPage < 1) {
this.nowPage = 1;
}
if (this.nowPage >= this.countPage) {
this.nowPage = this.countPage;
}
// 计算出开始的记录下标,和每页要显示的条数
this.startIndex = (this.nowPage - 1) * this.pageSize;
this.endIndex = this.pageSize;
// 回传给页面的开始的记录
this.startRow = this.startIndex + 1;
this.endRow = this.nowPage * this.pageSize;
// 判断结束的记录数是否大于总记录数
if (this.endRow >= this.countSize) {
this.endRow = this.countSize;
}
}
// 根据提供的表名向数据库发送请求,计算指定数据表中的数据总条数
public Integer getCountSize(String tableName) {
int countRecord = 0;
String sql = "select count(*) as c from " + tableName;
conn = JdbcUtil.getConn();
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
countRecord = rs.getInt("c");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JdbcUtil.release(rs, pstmt);
}
return countRecord;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
public Integer getNowPage() {
return nowPage;
}
public Integer getPageSize() {
return pageSize;
}
public Integer getCountPage() {
return countPage;
}
public Integer getCountSize() {
return countSize;
}
public Integer getStartIndex() {
return startIndex;
}
public Integer getEndIndex() {
return endIndex;
}
public Integer getStartRow() {
return startRow;
}
public Integer getEndRow() {
return endRow;
}
public Connection getConn() {
return conn;
}
public PreparedStatement getPstmt() {
return pstmt;
}
public ResultSet getRs() {
return rs;
}
@Override
public String toString() {
return "PageNation [nowPage=" + nowPage + ", pageSize=" + pageSize
+ ", countPage=" + countPage + ", countSize=" + countSize
+ ", startIndex=" + startIndex + ", endIndex=" + endIndex
+ ", startRow=" + startRow + ", endRow=" + endRow + ", rows="
+ rows + "]";
}
}
这个是链接数据库用的类
package www.csdn.net.jquery.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtil {
private static Connection conn;
private static PreparedStatement pstmt;
private static ResultSet rs;
private JdbcUtil() {
super();
// TODO Auto-generated constructor stub
}
// 连接数据库的操作
public static Connection getConn() {
if (conn == null) {
// 加载属性配置文件
// 创建属性对象
Properties prop = new Properties();
try {
// 加载指定名称的属性文件
prop.load(JdbcUtil.class.getClassLoader().getResourceAsStream(
"jdbc.properties"));
try {
// 根据文件的名称查找类文件
Class.forName(prop.getProperty("driver"));
// 创建连接对象
try {
conn = DriverManager.getConnection(
prop.getProperty("url"),
prop.getProperty("user"),
prop.getProperty("pass"));
System.out.println("连接数据库连接成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("连接数据库失败");
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out
.println("没有" + prop.getProperty("driver") + "文件");
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("属性文件加载出错");
e.printStackTrace();
}
}
return conn;
}
public static void release(ResultSet rs, PreparedStatement pstmt) {
// 释放结果集
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 释放准备语句
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 更新数据的操作增,删,改要用到的封装方法
public static boolean upDate(String sql, Object[] obj) {
boolean flag = false;
try {
// 准备语句的创建,带有sql命令的对象
pstmt = getConn().prepareStatement(sql);
for (int i = 1; i <= obj.length; i++) {
pstmt.setObject(i, obj[i - 1]);
}
int i = pstmt.executeUpdate();
if (i > 0) {
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
release(rs, pstmt);
}
return flag;
}
// 进行批量删除处理
public static boolean updateBatchDel(String sql, Integer[] ids) {
boolean flag = false;
Connection conn = getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < ids.length; i++) {
pstmt.setInt(1, ids[i]);
System.out.println(sql + "---------------" + ids[i]);
pstmt.addBatch();
}
int[] num = pstmt.executeBatch(); // 批量执行
for (int i = 0; i < num.length; i++) {
if (num[i] == 0) {
try {
conn.rollback(); // 进行事务回滚
return flag;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
conn.commit();// 提交事务
flag = true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
release(rs, pstmt);
}
return flag;
}
// 根据传入的表的名称,和每页数据得到传入表的所有的页数
// tableName:::::操作的数据表名称
// pagesize::::::每页显示的信息条数
// conn::::::::::XXXXDaoImpl中的连接对象
// pstmt::::::::XXXXDaoImpl中的预处理对象
// rs:::::::::::XXXXDaoImpl中的集合
public static Integer getCountPage(String tableName, Integer pagesize,
Connection conn, PreparedStatement pstmt, ResultSet rs) {
Integer countPage = 0;
String sql = "select count(*) as c from " + tableName;
conn = JdbcUtil.getConn();
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
int countRecord = rs.getInt("c");
countPage = countRecord % pagesize == 0 ? countRecord
/ pagesize : countRecord / pagesize + 1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JdbcUtil.release(rs, pstmt);
}
return countPage;
}
}
封装好的对数据库操作的类文件
package www.csdn.net.jquery.daoImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import www.csdn.net.jquery.bean.Admin;
import www.csdn.net.jquery.dao.AdminDao;
import www.csdn.net.jquery.util.JdbcUtil;
public class AdminDaoImpl implements AdminDao {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
@Override
public List<Admin> findNowPageInfo(int nowpage, int pagesize) {
// TODO Auto-generated method stub
List<Admin> adminList = new ArrayList<Admin>();
Admin admin = null;
String sql = "select * from admin limit ?,?";
conn = JdbcUtil.getConn();
try {
pstmt = conn.prepareStatement(sql);
int index = 1;
pstmt.setInt(index++, nowpage);
pstmt.setInt(index++, pagesize);
rs = pstmt.executeQuery();
while (rs.next()) {
admin = new Admin(rs.getInt("id"), rs.getString("name"),
rs.getString("pass"), rs.getString("permission"));
adminList.add(admin);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return adminList;
}
}
下面是测试类的代码
package www.csdn.net.jquery.test;
import java.util.List;
import org.junit.Test;
import www.csdn.net.jquery.bean.Admin;
import www.csdn.net.jquery.dao.AdminDao;
import www.csdn.net.jquery.daoImpl.AdminDaoImpl;
import www.csdn.net.jquery.util.JdbcUtil;
import www.csdn.net.jquery.util.PageNation;
public class AdminTest {
// 测试时先建立
AdminDao adminDao = new AdminDaoImpl();
@Test
public void testpagenation() {
PageNation<Admin> pageNation = new PageNation<Admin>(1, 3, "admin");
List<Admin> list = adminDao.findNowPageInfo(pageNation.getNowPage(),
pageNation.getPageSize());
pageNation.setRows(list);
System.out.println(pageNation.toString());
}
/**输出结果:
* PageNation [nowPage=1, pageSize=3, countPage=1, countSize=6,
* startIndex=0, endIndex=3, startRow=1, endRow=3,
* rows=[
* Admin [id=19, name=DONGHONGYU, pass=123, permission=admin],
* Admin [id=20, name=刘倩, pass=123, permission=admin],
* Admin [id=21, name=刘亚森, pass=123, permission=admin]]]
* */
}