⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractpagedao.java

📁 基于struts的网上商店源码
💻 JAVA
字号:
package com.mole.struts.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public abstract class AbstractPageDAO extends AbstractDAO {

	protected int pageSize = 1;
	protected int currentPage = 1;
	protected int pageCount = 0;
	protected int recordCount = 0;

	protected String columnCondition;
	protected String whereCondition;
	protected String groupCondition;
	protected String orderCondition;
	protected String idCondition;

	public AbstractPageDAO() {
	}

	public void getDBRecordCount() {
		Connection conn = getConn();
		String sql = null;
		if (groupCondition == null || groupCondition.equals("")) {
			sql = "SELECT count(*) FROM " + whereCondition;
		} else {
			sql = "SELECT count(distinct(" + idCondition + ")) FROM "
					+ whereCondition;
		}
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn.setAutoCommit(true);
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				recordCount = rs.getInt(1);
			}
			if ((recordCount % pageSize) == 0) {
				pageCount = recordCount / pageSize;
			} else {
				pageCount = recordCount / pageSize + 1;
			}
		} catch (Exception e) {
			e.printStackTrace();

		} finally {
			try {
				rs.close();
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public String getSql() {
		String groupBY = null;
		String orderBY = null;
		if (groupCondition == null || groupCondition.equals("")) {
			groupBY = "";
		} else {
			groupBY = " GROUP BY " + groupCondition;
		}
		if (orderCondition == null || orderCondition.equals("")) {
			orderBY = "";
		} else {
			orderBY = " ORDER BY " + orderCondition;
		}
		String sql = "SELECT top " + pageSize + " " + columnCondition
				+ " FROM " + whereCondition + " AND " + idCondition
				+ " NOT IN( SELECT top " + ((currentPage - 1) * pageSize)
				+ idCondition + " FROM " + whereCondition + groupBY + orderBY
				+ ") " + groupBY + orderBY;
		return sql;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;

		if ((recordCount % pageSize) == 0) {
			pageCount = recordCount / pageSize;
		} else {
			pageCount = recordCount / pageSize + 1;
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageCount() {
		return pageCount;
	}

	public int getRecordCount() {
		return recordCount;
	}

	public void setColumnCondition(String columnCondition) {
		this.columnCondition = columnCondition;
	}

	public void setWhereCondition(String whereCondition) {
		if (whereCondition != null
				&& !whereCondition.equals(this.whereCondition)) {
			this.whereCondition = whereCondition;
			getDBRecordCount();
		}
	}

	public void setGroupCondition(String groupCondition) {
		this.groupCondition = groupCondition;
	}

	public void setOrderCondition(String orderCondition) {
		this.orderCondition = orderCondition;
	}

	public void setIdCondition(String idCondition) {
		this.idCondition = idCondition;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -