首页>代码>Struts1开发的基于角色的用户权限后台管理系统源代码下载>/AMS/src/com/permission/dao/impl/RoleAndFunctionDAOImpl.java
/**
 * 
 */
package com.permission.dao.impl;

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 com.permission.dao.IRoleAndFunctionDAO;
import com.permission.sys.BaseDAO;
import com.permission.sys.PageRoll;
import com.permission.utils.common.PermissionException;
import com.permission.utils.db.DBUtil;
import com.permission.vo.RoleAndFunction;

/**
 * @author Jack
 * 
 */
public class RoleAndFunctionDAOImpl extends BaseDAO implements
		IRoleAndFunctionDAO {

	@Override
	public void add(RoleAndFunction roleAndFunction) throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "insert into RoleAndFunction(RoleID,FunctionID) values (?,?)";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, roleAndFunction.getRoleID());
			ps.setInt(2, roleAndFunction.getFunctionID());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("添加角色功能关系失败");
		} finally {
			DBUtil.close(null, ps, conn);
		}
	}
	
	@Override
	public void delete(Integer roleID, Integer functionID)
			throws PermissionException {
		StringBuffer sql = new StringBuffer();
		sql.append("delete from RoleAndFunction where roleID=");
		sql.append(roleID);
		sql.append(" and functionID=");
		sql.append(functionID);
		try {
			executeUpdate(sql.toString());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("删除角色功能关系失败");
		}
	}

	@Override
	public RoleAndFunction findByID(Integer id) throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		RoleAndFunction roleAndFunction = null;
		String sql = "select id,RoleID,FunctionID from RoleAndFunction where id=?";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				roleAndFunction = new RoleAndFunction();
				roleAndFunction.setId(rs.getInt(1));
				roleAndFunction.setRoleID(rs.getInt(2));
				roleAndFunction.setFunctionID(rs.getInt(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("查询角色功能关系失败");
		} finally {
			DBUtil.close(rs, ps, conn);
		}
		return roleAndFunction;
	}

	@Override
	public List<RoleAndFunction> list(PageRoll pageRoll)
			throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<RoleAndFunction> list = new ArrayList<RoleAndFunction>();
		String sql = "select id,RoleID,FunctionID from RoleAndFunction limit ?,?";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (pageRoll.getCurrPage() - 1) * pageRoll.getPageSize());
			ps.setInt(2, pageRoll.getPageSize());
			rs = ps.executeQuery();
			while (rs.next()) {
				RoleAndFunction roleAndFunction = new RoleAndFunction();
				roleAndFunction.setId(rs.getInt(1));
				roleAndFunction.setRoleID(rs.getInt(2));
				roleAndFunction.setFunctionID(rs.getInt(3));
				list.add(roleAndFunction);
			}
			sql = "select count(*) from RoleAndFunction";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			rs.next();
			pageRoll.setCountSize(rs.getInt(1));
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("查询角色功能关系列表失败");
		} finally {
			DBUtil.close(rs, ps, conn);
		}
		return list;
	}

	@Override
	public void update(RoleAndFunction roleAndFunction)
			throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "update RoleAndFunction set RoleID=?,FunctionID=? where id=?";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, roleAndFunction.getRoleID());
			ps.setInt(2, roleAndFunction.getFunctionID());
			ps.setInt(3, roleAndFunction.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("修改角色功能关系失败");
		} finally {
			DBUtil.close(null, ps, conn);
		}
	}

	@Override
	public List<RoleAndFunction> findByRoleID(Integer roleID)
			throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<RoleAndFunction> list = new ArrayList<RoleAndFunction>();
		String sql = "select id,RoleID,FunctionID from RoleAndFunction where RoleID=?";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, roleID);
			rs = ps.executeQuery();
			if (rs.next()) {
				RoleAndFunction roleAndFunction = new RoleAndFunction();
				roleAndFunction.setId(rs.getInt(1));
				roleAndFunction.setRoleID(rs.getInt(2));
				roleAndFunction.setFunctionID(rs.getInt(3));
				list.add(roleAndFunction);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("根据角色ID查询角色功能关系失败");
		} finally {
			DBUtil.close(rs, ps, conn);
		}
		return list;
	}
	
	
}
最近下载更多
hongdongdong  LV13 2023年6月17日
wanglinddad  LV55 2022年4月24日
judy0971  LV12 2020年9月8日
281273445  LV3 2020年6月18日
981210X  LV2 2020年5月25日
wssm2333  LV6 2020年5月22日
110330  LV10 2020年5月6日
小码农001  LV1 2020年3月31日
houliukou  LV37 2020年2月17日
最近浏览更多
WBelong  LV7 2023年12月27日
1997akkk  LV5 2023年8月8日
hongdongdong  LV13 2023年6月17日
泓鼎168  LV19 2023年3月30日
wwdddff333333333  LV2 2023年1月6日
微信网友_6248713511227392  LV11 2022年12月5日
小瑞子来了 2022年10月21日
暂无贡献等级
hoffbug  LV19 2022年8月25日
177711057 2022年6月12日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友