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

📄 ghy_page_tools.java

📁 一套网上书店系统采用JAVABEAN+SERVLET+JSP
💻 JAVA
字号:
package ghy_page_tools;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ghy_page_tools {

	// 在该类中不要关闭connection,影响事务,关闭事务是在servlet中做的

	private int page_list;// 一页显示多少行

	private String type_field_name;

	private String tablename;

	private String primary_key;

	private Connection conn;

	private Statement stmt;

	private ResultSet rs;

	private PageBean pagebean;

	private List record_list = new ArrayList();

	private int getRecordCount(String type) throws Throwable {

		int recordCount = 0;

		try {
			if (type.equals("all")) {
				stmt = conn.createStatement();
				System.out.println("select count(" + primary_key
						+ ") as id from " + tablename);
				rs = stmt.executeQuery("select count(" + primary_key
						+ ") as id from " + tablename);
				if (rs.next()) {
					recordCount = Integer.valueOf(rs.getString("id"))
							.intValue();
				} else {
					recordCount = 0;
				}
				rs.close();
				stmt.close();
			} else {
				stmt = conn.createStatement();
				rs = stmt.executeQuery("select count(id) as id from "
						+ tablename + " where " + type_field_name + "=" + "'"
						+ type + "'");
				if (rs.next()) {
					recordCount = Integer.valueOf(rs.getString("id"))
							.intValue();
				} else {
					recordCount = 0;
				}
				rs.close();
				stmt.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}
		return recordCount;

	}

	private List getRecordList(String type, String tablename, String page)
			throws Throwable {

		List recordList = new ArrayList();

		try {
			if (type.equals("all")) {

				stmt = conn.createStatement();
				System.out.println("select * from " + tablename + " order by "
						+ primary_key + " desc limit "
						+ Integer.valueOf(page_list)
						* (Integer.valueOf(page).intValue() - 1) + ","
						+ Integer.valueOf(page_list));
				rs = stmt.executeQuery("select * from " + tablename
						+ " order by " + primary_key + " desc limit "
						+ Integer.valueOf(page_list)
						* (Integer.valueOf(page).intValue() - 1) + ","
						+ Integer.valueOf(page_list));
				while (rs.next()) {
					String[] row = new String[rs.getMetaData().getColumnCount()];
					for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
						row[i] = rs.getString(i + 1);
					}

					recordList.add(row);
				}
				rs.close();
				stmt.close();

			} else {
				stmt = conn.createStatement();

				System.out.println("pag SQL is:" + "select * from " + tablename
						+ " where " + type_field_name + "='" + type + "'"
						+ " order by " + primary_key + " desc limit "
						+ Integer.valueOf(page_list)
						* (Integer.valueOf(page).intValue() - 1) + ","
						+ Integer.valueOf(page_list));

				rs = stmt.executeQuery("select * from " + tablename + " where "
						+ type_field_name + "='" + type + "'" + " order by "
						+ primary_key + " desc limit "
						+ Integer.valueOf(page_list)
						* (Integer.valueOf(page).intValue() - 1) + ","
						+ Integer.valueOf(page_list));
				while (rs.next()) {
					String[] row = new String[rs.getMetaData().getColumnCount()];
					for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
						row[i] = rs.getString(i + 1);
					}

					recordList.add(row);
				}
				rs.close();
				stmt.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			throw e;
		}
		return recordList;
	}

	private PageBean createPageBean(int page_count, int page, String type) {
		if (type.equals("all")) {
			return pagebean = new PageBean(page_count, page, "all");
		} else {
			return pagebean = new PageBean(page_count, page, type);

		}
	}

	public Map get_type_list(String type_table_name, String type_id_name,
			String type_name) throws Throwable {

		LinkedHashMap type_list = new LinkedHashMap();

		try {
			stmt = conn.createStatement();

			System.out.println("select " + type_id_name + ", " + type_name
					+ " from " + type_table_name);

			rs = stmt.executeQuery("select " + type_id_name + ", " + type_name
					+ " from " + type_table_name);

			type_list.put("all", "all");

			while (rs.next()) {
				type_list.put(rs.getString(1), rs.getString(2));
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			throw e;
		}

		return type_list;

	}

	public ghy_page_tools(Connection connection, String type,
			String type_field_name, String tablename, String primary_key,
			String page, int page_list) throws Throwable {
		try {
			this.conn = connection;

			this.type_field_name = type_field_name;

			this.primary_key = primary_key;

			this.page_list = page_list;

			this.tablename = tablename;

			int page_count;// 总页数

			int record_count = 0;// 当前类别下的记录数

			if (("".equals(type)) || (type == null)) {

				record_count = this.getRecordCount("all");
				System.out.println("记录数是:" + record_count);
			} else {
				record_count = this.getRecordCount(type);
				System.out.println("记录数是:" + record_count);

			}

			if (record_count == 0) {
				System.out.println("记录数:0 生成new PageBean(0, 0, \"all\")");
				pagebean = new PageBean(1, 1, type);
			} else {
				if ((record_count % page_list) == 0) {
					page_count = record_count / page_list;
				} else {
					page_count = record_count / page_list + 1;
				}

				if (("".equals(type)) || (type == null)) {

					record_list = getRecordList("all", tablename, "1");

					pagebean = createPageBean(page_count, 1, "all");

				} else {
					record_list = getRecordList(type, tablename, page);

					if (record_list.size() == 0) {
						record_list = getRecordList(type, tablename, "1");
						pagebean = createPageBean(page_count, Integer.valueOf(
								"1").intValue(), type);
					} else {
						pagebean = createPageBean(page_count, Integer.valueOf(
								page).intValue(), type);
					}

				}

			}
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			throw e;
		}

	}

	public PageBean getPagebean() {
		return pagebean;
	}

	public List getRecord_list() {
		return record_list;
	}

}

⌨️ 快捷键说明

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