/**
 * 
 */
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.IRoleDAO;
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.Role;

/**
 * @author Jack
 * 
 */
public class RoleDAOImpl extends BaseDAO implements IRoleDAO {

	@Override
	public void add(Role role) throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "insert into role(RoleName,Description) values (?,?)";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, role.getRoleName());
			ps.setString(2, role.getDescription());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("添加角色失败");
		} finally {
			DBUtil.close(null, ps, conn);
		}
	}

	@Override
	public void delete(Integer roleID) throws PermissionException {
		String sql = "delete from role where RoleID=" + roleID;
		try {
			executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("删除角色失败");
		}
	}

	@Override
	public Role findByRoleID(Integer roleID) throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Role role = null;
		String sql = "select RoleID,RoleName,Description from role where RoleID=?";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, roleID);
			rs = ps.executeQuery();
			if (rs.next()) {
				role = new Role();
				role.setRoleID(rs.getInt(1));
				role.setRoleName(rs.getString(2));
				role.setDescription(rs.getString(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("根据角色ID查询角色信息失败");
		} finally {
			DBUtil.close(rs, ps, conn);
		}
		return role;
	}

	@Override
	public List<Role> list(PageRoll pageRoll) throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Role> list = new ArrayList<Role>();
		String sql = "select RoleID,RoleName,Description from role 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()) {
				Role role = new Role();
				role.setRoleID(rs.getInt(1));
				role.setRoleName(rs.getString(2));
				role.setDescription(rs.getString(3));
				list.add(role);
			}
			sql = "select count(*) from role";
			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(Role role) throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "update role set RoleName=?,Description=? where RoleID=?";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, role.getRoleName());
			ps.setString(2, role.getDescription());
			ps.setInt(3, role.getRoleID());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("修改角色失败");
		} finally {
			DBUtil.close(null, ps, conn);
		}
	}

	@Override
	public Role findByRoleName(String roleName) throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Role role = null;
		String sql = "select RoleID,RoleName,Description from role where RoleName=?";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, roleName);
			rs = ps.executeQuery();
			if (rs.next()) {
				role = new Role();
				role.setRoleID(rs.getInt(1));
				role.setRoleName(rs.getString(2));
				role.setDescription(rs.getString(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("根据角色名称查询角色信息失败");
		} finally {
			DBUtil.close(rs, ps, conn);
		}
		return role;
	}

	@Override
	public List<Role> list() throws PermissionException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Role> list = new ArrayList<Role>();
		String sql = "select RoleID,RoleName,Description from role";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Role role = new Role();
				role.setRoleID(rs.getInt(1));
				role.setRoleName(rs.getString(2));
				role.setDescription(rs.getString(3));
				list.add(role);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PermissionException("查询角色列表失败");
		} 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日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友