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

📄 adminvoucherviewdao.java

📁 基于struts的网上商店源码
💻 JAVA
字号:
/*
 * 作者:荆明君
 * 时间:2007年12月15日
 * 功能:平台操作人员管理-添加删除和浏览平台操作人员(账户管理)(数据库接口)。
 */
package com.mole.struts.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.mole.struts.bean.AdminVoucherViewBean;

public class AdminVoucherViewDAO {
	private Connection conn;
	private int pageSize;

	public AdminVoucherViewDAO() {
		System.out.println("Data source init...");
		try {
			Context ctx = new InitialContext();
			if (ctx == null)
				throw new Exception("Failed to initial context!");
			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/jdbc/crmdata");
			conn = ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 根据 默认 查询方法获取页面属性及查询结果
	public int getPageInfo(int pageSize) {
		int count = 0;
		this.pageSize = pageSize;
		String sql = "SELECT COUNT(*) FROM [merchantvoucher]";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			if (rs.next())
				count = rs.getInt(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	public ArrayList<AdminVoucherViewBean> allVoucherView(String line,
			int currentPage) throws Exception {
		ResultSet rs = null;
		PreparedStatement ps = null;
		String sql = "SELECT TOP "
				+ pageSize
				+ " a.name, a.description, a.amount, a.createdate,"
				+ "b.name, c.name, d.name, e.name  "
				+ "FROM  [merchantvoucher]a ,[store]b, [storetype]c, [city]d, [province]e "
				+ "WHERE b.id=a.storeId and c.id=b.typeId and d.id=SUBSTRING(B.[areaId],1,4) and e.id=d.provinceId "
				+ " and a.[ID] NOT IN (SELECT TOP " + (currentPage - 1)
				* pageSize + " a.[ID] "
				+ "FROM [merchantvoucher]a ) order by '" + line + "'";
		ArrayList<AdminVoucherViewBean> info = new ArrayList<AdminVoucherViewBean>();
		try {
			conn.setAutoCommit(true);
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				AdminVoucherViewBean voucherInfo = new AdminVoucherViewBean();
				voucherInfo.setVoucherName(rs.getString(1));
				voucherInfo.setVoucherDiscription(rs.getString(2));
				voucherInfo.setVoucherAmount(rs.getString(3));
				voucherInfo.setVoucherDate(rs.getString(4).toString()
						.substring(0, 10));
				voucherInfo.setStoreName(rs.getString(5));
				voucherInfo.setStoreType(rs.getString(6));
				voucherInfo.setStoreCity(rs.getString(7));
				voucherInfo.setStoreProvince(rs.getString(8));

				info.add(voucherInfo);
			}
		} finally {
			if (ps != null)
				ps.close();
		}
		return info;
	}

	// 根据 StoreName 查询方法获取页面属性及查询结果
	public int getPageInfoByStoreName(String keyword, int pageSize) {
		int count = 0;
		this.pageSize = pageSize;
		String sql = "SELECT COUNT(*) FROM [merchantvoucher]a,[store]b WHERE b.id=a.storeId and b.name='"
				+ keyword + "'";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			if (rs.next())
				count = rs.getInt(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	public ArrayList<AdminVoucherViewBean> getVoucherViewByStoreName(
			String storeName, String line, int currentPage) throws Exception {
		ResultSet rs = null;
		PreparedStatement ps = null;
		String sql = "SELECT TOP "
				+ pageSize
				+ " a.name, a.description, a.amount, a.createdate,"
				+ "b.name, c.name, d.name, e.name  "
				+ "FROM  [merchantvoucher]a ,[store]b, [storetype]c, [city]d, [province]e "
				+ "WHERE b.id=a.storeId and b.name='"
				+ storeName
				+ "' and c.id=b.typeId and d.id=SUBSTRING(B.[areaId],1,4) and e.id=d.provinceId "
				+ " and a.[ID] NOT IN (SELECT TOP " + (currentPage - 1)
				* pageSize + " a.[ID] "
				+ "FROM [merchantvoucher]a ) order by '" + line + "'";
		ArrayList<AdminVoucherViewBean> info = new ArrayList<AdminVoucherViewBean>();
		try {
			conn.setAutoCommit(true);
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				AdminVoucherViewBean voucherInfo = new AdminVoucherViewBean();
				voucherInfo.setVoucherName(rs.getString(1));
				voucherInfo.setVoucherDiscription(rs.getString(2));
				voucherInfo.setVoucherAmount(rs.getString(3));
				voucherInfo.setVoucherDate(rs.getString(4).toString()
						.substring(0, 10));
				voucherInfo.setStoreName(rs.getString(5));
				voucherInfo.setStoreType(rs.getString(6));
				voucherInfo.setStoreCity(rs.getString(7));
				voucherInfo.setStoreProvince(rs.getString(8));

				info.add(voucherInfo);
			}
		} finally {
			if (ps != null)
				ps.close();
		}
		return info;
	}

	// 根据 StoreType 查询方法获取页面属性及查询结果
	public int getPageInfoByStoreType(String keyword, int pageSize) {
		int count = 0;
		this.pageSize = pageSize;
		String sql = "SELECT COUNT(*) FROM [merchantvoucher]a,[store]b,[storeType]c WHERE b.id=a.storeId and c.id=b.typeId and c.name='"
				+ keyword + "'";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			if (rs.next())
				count = rs.getInt(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	public ArrayList<AdminVoucherViewBean> getVoucherViewByStoreType(
			String storeType, String line, int currentPage) throws Exception {
		ResultSet rs = null;
		PreparedStatement ps = null;
		String sql = "SELECT TOP "
				+ pageSize
				+ " a.name, a.description, a.amount, a.createdate,"
				+ "b.name, c.name, d.name, e.name  "
				+ "FROM  [merchantvoucher]a ,[store]b, [storetype]c, [city]d, [province]e "
				+ "WHERE b.id=a.storeId and c.id=b.typeId and c.name='"
				+ storeType
				+ "' and d.id=SUBSTRING(B.[areaId],1,4) and e.id=d.provinceId "
				+ " and a.[ID] NOT IN (SELECT TOP " + (currentPage - 1)
				* pageSize + " a.[ID] "
				+ "FROM [merchantvoucher]a ) order by '" + line + "'";
		ArrayList<AdminVoucherViewBean> info = new ArrayList<AdminVoucherViewBean>();
		try {
			conn.setAutoCommit(true);
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				AdminVoucherViewBean voucherInfo = new AdminVoucherViewBean();
				voucherInfo.setVoucherName(rs.getString(1));
				voucherInfo.setVoucherDiscription(rs.getString(2));
				voucherInfo.setVoucherAmount(rs.getString(3));
				voucherInfo.setVoucherDate(rs.getString(4).toString()
						.substring(0, 10));
				voucherInfo.setStoreName(rs.getString(5));
				voucherInfo.setStoreType(rs.getString(6));
				voucherInfo.setStoreCity(rs.getString(7));
				voucherInfo.setStoreProvince(rs.getString(8));

				info.add(voucherInfo);
			}
		} finally {
			if (ps != null)
				ps.close();
		}
		return info;
	}

	// 根据 Area 查询方法获取页面属性及查询结果
	public int getPageInfoByArea(String keyword, int pageSize) {
		int count = 0;
		this.pageSize = pageSize;
		String sql = null;
		if (keyword.length() == 4) {
			sql = "SELECT COUNT(*) FROM [merchantvoucher]a,[store]b,[area]d WHERE b.id=a.storeId and d.id=b.areaId and d.cityId='"
					+ keyword + "'";
		} else {
			if (keyword.length() < 4) {
				sql = "SELECT COUNT(*) FROM [merchantvoucher]a,[store]b,[area]d WHERE b.id=a.storeId and d.id=b.areaId and d.provinceId='"
						+ keyword + "'";
			} else {
				if (keyword.length() == 6) {
					sql = "SELECT COUNT(*) FROM [merchantvoucher]a,[store]b WHERE b.id=a.storeId and b.areaId='"
							+ keyword + "'";
				}
			}
		}
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			if (rs.next())
				count = rs.getInt(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	public ArrayList<AdminVoucherViewBean> getVoucherViewByArea(String keyword,
			String line, int currentPage) throws Exception {
		ResultSet rs = null;
		PreparedStatement ps = null;
		String sql = null;
		int i = keyword.length();
		// 判断输入地址:市、省、区 进行不同查询
		if (i == 4) {
			sql = "SELECT TOP "
					+ pageSize
					+ " a.name, a.description, a.amount, a.createdate,"
					+ "b.name, c.name, g.name, d.name  "
					+ "FROM  [merchantvoucher]a ,[store]b, [storetype]c, [city]d, [province]e, [area]g "
					+ "WHERE b.id=a.storeId and c.id=b.typeId and g.cityId='"
					+ keyword + "' and d.id=g.cityId and e.id=g.provinceId "
					+ " and a.[ID] NOT IN (SELECT TOP " + (currentPage - 1)
					* pageSize + " a.[ID] "
					+ "FROM [merchantvoucher]a ) order by '" + line + "'";
		} else {
			if (i < 4) {
				sql = "SELECT TOP "
						+ pageSize
						+ " a.name, a.description, a.amount, a.createdate,"
						+ "b.name, c.name, d.name, e.name  "
						+ "FROM  [merchantvoucher]a ,[store]b, [storetype]c, [city]d, [province]e, [area]g "
						+ "WHERE b.id=a.storeId and c.id=b.typeId and g.Id=b.areaId and g.provinceId='"
						+ keyword + "' and d.id=g.cityId and e.id='" + keyword
						+ "' " + " and a.[ID] NOT IN (SELECT TOP "
						+ (currentPage - 1) * pageSize + " a.[ID] "
						+ "FROM [merchantvoucher]a ) order by '" + line + "'";
			} else {
				if (i == 6) {
					sql = "SELECT TOP "
							+ pageSize
							+ " a.name, a.description, a.amount, a.createdate,"
							+ "b.name, c.name, g.name, d.name  "
							+ "FROM  [merchantvoucher]a ,[store]b, [storetype]c, [city]d, [province]e, [area]g "
							+ "WHERE b.id=a.storeId and b.areaId='"
							+ keyword
							+ "' and c.id=b.typeId and g.Id=b.areaId and d.id=g.cityId "
							+ " and a.[ID] NOT IN (SELECT TOP "
							+ (currentPage - 1) * pageSize + " a.[ID] "
							+ "FROM [merchantvoucher]a ) order by '" + line
							+ "'";
				}
			}
		}
		ArrayList<AdminVoucherViewBean> info = new ArrayList<AdminVoucherViewBean>();
		try {
			conn.setAutoCommit(true);
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				AdminVoucherViewBean voucherInfo = new AdminVoucherViewBean();
				voucherInfo.setVoucherName(rs.getString(1));
				voucherInfo.setVoucherDiscription(rs.getString(2));
				voucherInfo.setVoucherAmount(rs.getString(3));
				voucherInfo.setVoucherDate(rs.getString(4).toString()
						.substring(0, 10));
				voucherInfo.setStoreName(rs.getString(5));
				voucherInfo.setStoreType(rs.getString(6));
				voucherInfo.setStoreCity(rs.getString(7));
				voucherInfo.setStoreProvince(rs.getString(8));

				info.add(voucherInfo);
			}
		} finally {
			if (ps != null)
				ps.close();
		}
		return info;
	}
}

⌨️ 快捷键说明

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