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

📄 roledao.java

📁 实现统一的人员日志管理系统管理后台
💻 JAVA
字号:
package com.tb.log.model.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.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.tb.log.factory.OracleDAOFactory;
import com.tb.log.model.dao.DAOFactory;
import com.tb.log.model.dao.idao.IRoleDAO;
import com.tb.log.system.SystemException;
import com.tb.log.util.tools.ToolKit;

public class RoleDAO implements IRoleDAO{

//	String add_UserRole_By_UseridAndRoleid = "insert into t_userrole values(?,?)";
	private static final String FIND_BY_ID = "select roleid from t_userrole where userid=?";
	
	public List findAll(int currentPage) throws SystemException {
		// TODO Auto-generated method stub
		return null;
	}

	public List findByCondition(String consql, int currentPage)
			throws SystemException {
		// TODO Auto-generated method stub
		return null;
	}

	public List findById(String id) throws SystemException {
		//System.out.println("id"+id);
		List roleList = new ArrayList();
		Connection conn = OracleDAOFactory.getConnection();
		try {
			PreparedStatement psm = conn.prepareStatement(FIND_BY_ID);
			psm.setInt(1, Integer.parseInt(id));
			ResultSet rs = psm.executeQuery();
			while(rs.next()){
				roleList.add(rs.getInt(1));
				System.out.println("roleid"+rs.getInt(1));
			}
		} catch (Exception e) {
			String err = "SQLException in UserRoleDAO():findById()---------"+e;
			throw new SystemException(err);
		}
		return roleList;
	}
	
	public boolean removeUserRole(String delName,int delid) throws SystemException{
		String remove = "delete from t_userrole where "+delName+"=?";
		Connection coon = OracleDAOFactory.getConnection();
		try {
			coon.setAutoCommit(false);
			PreparedStatement psm = coon.prepareStatement(remove);
			psm.setInt(1, delid);
			psm.executeUpdate();
			
			coon.commit();
			coon.setAutoCommit(true);
		} catch (SQLException e) {
			String err = "SQLException in UserRoleDAO():remove()---------"+e;
			throw new SystemException(err);
		}
		return true;
	}

	public void addUserRole(int userid, int roleid) throws SystemException{
		String ADD = "insert into t_userrole values (?,?)";
		Connection coon = OracleDAOFactory.getConnection();
		try {
			PreparedStatement psm = coon.prepareStatement(ADD);
			psm.setInt(1, userid);
			psm.setInt(2, roleid);
			psm.executeUpdate();
		} catch (SQLException e) {
			String err = "SQLException in UserRoleDAO():add()---------"+e;
			throw new SystemException(err);
		}
	}
	
	
	public List findById(String findId,String conName,int id) throws SystemException{
		String find_by_id = "select "+findId+" from t_userrole where "+conName+"=?";
		//System.out.println(find_by_id);
		List list = new ArrayList();
		Connection conn = OracleDAOFactory.getConnection();
		try{
		PreparedStatement psm = conn.prepareStatement(find_by_id) ;
		psm.setInt(1, id);
		ResultSet rs = psm.executeQuery();
		while(rs.next()){
			list.add(rs.getString(1));
		}
		}catch (SQLException e) {
			String err = "SQLException in UserRoleDAO():findById()---------"+e;
			throw new SystemException(err);
		}
		return list;
	}
	
	
	public void addUserRoleByUserList(int roleid,String[] usersid)throws SystemException{
		IRoleDAO ipro = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getRoleDAO();
		Connection coon = OracleDAOFactory.getConnection();
		try {
			if(!ipro.findById("userid","roleid",roleid).isEmpty() ){
				ipro.removeUserRole("roleid",roleid);
			//	System.out.println("ok");
			}
			for(int i=0;i<usersid.length;i++){
				ipro.addUserRole(Integer.parseInt(usersid[i]), roleid);
			}		
		} catch (Exception e) {
			String err = "SQLException in UserRoleDAO():addUserProByUserList()---------"+e;
			throw new SystemException(err);
		}
	}
	
	public void addUserRoleByRoleList(int userid,String[] rolesid)throws SystemException{
		IRoleDAO ipro = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getRoleDAO();
		Connection coon = OracleDAOFactory.getConnection();
		try {
			if(ipro.findById("roleid","userid",userid)!=null ){
				ipro.removeUserRole("userid",userid);
			}
			for(int i=0;i<rolesid.length;i++){
				ipro.addUserRole(userid,Integer.parseInt(rolesid[i]));
			}		
		} catch (Exception e) {
			String err = "SQLException in UserRoleDAO():addUserProByUserList()---------"+e;
			throw new SystemException(err);
		}
	}
	
	

	public void remove(Object obj) throws SystemException {
		// TODO Auto-generated method stub
		
	}

	public void save(Object obj) throws SystemException {
		// TODO Auto-generated method stub
		
	}

	public void update(Object obj) throws SystemException {
		// TODO Auto-generated method stub
		
	}
	
	
	
	public static void main(String[] args) {
		RoleDAO role = new RoleDAO();
		String str = "23,1,43";
		try {
			//role.addUserRoleByRoleList(1, new ToolKit().StringToArray(str));
			//role.removeUserRole("userid", 1);
			List roleList = role.findById("roleid", "userid", 3);
			for (Iterator iterator = roleList.iterator(); iterator.hasNext();) {
				String name = (String) iterator.next();
				
				System.out.println(name);
			}
			if(roleList.contains("1")){
				System.out.println("contains4");
			}
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}

⌨️ 快捷键说明

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