roledao.java

来自「struts做得固定资产管理系统 带毕业论文完整版」· Java 代码 · 共 146 行

JAVA
146
字号
package com.qrsx.appcam.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.qrsx.appcam.model.Role;

public class RoleDAO extends BaseDAO {
	/**
	 * 创建新角色
	 * 
	 * @param role
	 * @throws SQLException
	 */
	public void create(Role role) throws SQLException {

		String sql = "Insert into role(name,description) values(?,?)";
		// 设置参数并执行插入操作
		ps = conn.prepareStatement(sql);
		ps.setString(1, role.getName());// 设置角色名称
		ps.setString(2, role.getDescription());// 设置角色描述
		ps.executeUpdate();
	}

	/**
	 * 更新角色
	 * 
	 * @param role
	 * @throws SQLException
	 */
	public void update(Role role) throws SQLException {
		String sql = "Update Role set name=?, description=? where id=? ";

		// 设置参数值
		ps = conn.prepareStatement(sql);
		ps.setString(1, role.getName());
		ps.setString(2, role.getDescription());
		ps.setInt(3, role.getId());
		// 执行
		ps.executeUpdate();
	}

	/**
	 * 删除角色
	 * 
	 * @param roleId
	 * @return int<total>
	 * @throws SQLException
	 */
	public int delete(Integer roleId) throws SQLException {

		String sql = "Delete from Role where id=? ";
		// 设置参数值
		ps = conn.prepareStatement(sql);
		ps.setInt(1, roleId);
		// 执行
		int total = ps.executeUpdate();
		// 返回删除的总数
		return total;
	}

	/**
	 * 按照角色主键检索角色
	 * 
	 * @param roleId
	 * @return Role
	 * @throws SQLException
	 */
	public Role findById(Integer roleId) throws SQLException {
		String sql = "Select * from Role where id=? ";

		// 设置参数值并执行查询
		ps = conn.prepareStatement(sql);
		ps.setInt(1, roleId);
		ResultSet rs = ps.executeQuery();

		Role role = null;
		if (rs.next()) {
			role = new Role();
			role.setId(rs.getInt("id"));
			role.setName(rs.getString("name"));
			role.setDescription(rs.getString("description"));
		}
		return role;
	}

	/**
	 * 检索所有的角色
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<Role> findAll() throws SQLException {

		String sql = "Select * from Role ";

		// 设置参数并执行查询
		ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();

		// 将查询结果组装至deptment实体对象
		Role role = null;
		List<Role> list = new ArrayList<Role>();
		while (rs.next()) {
			role = new Role();
			role.setId(rs.getInt("id"));
			role.setName(rs.getString("name"));
			role.setDescription(rs.getString("description"));
			// 将结果加入到列表中
			list.add(role);
		}
		return list;
	}

	/**
	 * 按照条件检索角色
	 * 
	 * @param role
	 * @return
	 * @throws SQLException
	 */
	public List<Role> list(Role role) throws SQLException {
		String sql = "Select * from role where 1=1 ";

		// 根据资产类型名称检索
		if (role.getName() != null && !role.getName().equals("")) {
			sql = sql + " and name like '%" + role.getName() + "%'";
		}
		// 执行查询
		ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		// 将查询结果组装至deptment实体对象
		Role role1 = null;
		List<Role> list = new ArrayList<Role>();
		while (rs.next()) {
			role1 = new Role();
			role1.setId(rs.getInt("id"));
			role1.setName(rs.getString("name"));
			role1.setDescription(rs.getString("description"));
			list.add(role1);
		}
		return list;
	}
}

⌨️ 快捷键说明

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