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

📄 userdaoim.java

📁 文章管理系统 用Java开发,以Struts为框架,以Jdbc链接Mysql数据库。系统属性:系统属性设置、留言管理、友情链接管理、网站调查、公告管理 关于我们、版权声明、联系我们
💻 JAVA
字号:
package com.yhcms.manage.admin.dao;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.yhcms.db.DBConnException;
import com.yhcms.db.DBConnect;
import com.yhcms.manage.admin.bean.AdminUser;
import com.yhcms.manage.admin.itface.UserDao;
import com.yhcms.utils.MD5;
/**
 * <p>Title:系统后台用户的相关操作</p>
 * <li>后台用户的各项操作</li>
 * <b>CopyRight: yyhweb[由由华网]</b>
 * @author stephen
 * @version YH-2.0
 */
public class UserDaoIm implements UserDao {

	private static Logger yhlog = Logger.getLogger(UserDaoIm.class.getName());
	
	private DBConnect dbconn = null;
	
	private MD5 md5 = null;
	
	private static UserDaoIm userdao = new UserDaoIm();
	
	/**
	 * @return 取得一个后台用户操作对象
	 */
	public static UserDaoIm getInstance(){
		return userdao;
	}
	
	public int getMaxId() throws DBConnException {
		int maxId = 0;
		String sql = "select max(id) from admin";
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeQuery();
			if(dbconn.next()){
				maxId = dbconn.getInt(1);
			}
		}catch(Exception e){
			yhlog.warn("When get the max id of system user,throw an Exception!");
		}finally{
			dbconn.close();
		}
		return maxId;
	}

	public boolean addAdminUser(AdminUser user) throws DBConnException {
		int i = 0;
		md5 = new MD5();
		// 取得后台用户最大Id+1 作为新用户的Id
		int maxid = getMaxId()+1;
		AdminUser curUser = user;
		String username = curUser.getUsername();
		String password = md5.getMD5ofStr(curUser.getPassword());
		String groupname = curUser.getGroupname();
		int groupid = curUser.getGroupid();
		String sql = "insert into admin(id,username,password,groupid,groupname) values ("+
					  maxid+",'"+username+"','"+password+"',"+groupid+",'"+groupname+"')";
		try{
			dbconn = new DBConnect(sql);
			i = dbconn.executeUpdate();
		}catch(Exception e){
			yhlog.warn("When add an admin user,Exception occur!");
		}finally{
			dbconn.close();
		}
		if(i>0){
			yhlog.info("Add a user successfully.");
			return true;
		}else{
			yhlog.info("Add a user unsuccessfully.");
			return false;
		}
	}

	public boolean delAdminUser(int id) throws DBConnException {
		int i = 0;
		String sql = "delete from admin where id="+id;
		try{
			dbconn = new DBConnect(sql);
			i = dbconn.executeUpdate();
		}catch(Exception e){
			yhlog.warn("When delete a user from system,throw an Exception!The user id is:"+id+".");
		}finally{
			dbconn.close();
		}
		if(i>0){
			yhlog.info("Delete a user successfully,the user id is:"+id+".");
			return true;
		}else{
			yhlog.info("Delete a user unsuccessfully.the user id is:"+id+".");
			return false;
		}
	}

	public int isAdminUser(AdminUser user,int i) throws DBConnException {
		int j = 0;
		String sql = "";
		md5 = new MD5();
		
		AdminUser curUser = user;
		String userName = curUser.getUsername();
		String password = md5.getMD5ofStr(curUser.getPassword());
		if(i==0){
			 sql = "select id from admin where username='"+userName+"' and password='"+password+"'";
		}else if(i==1){
			 sql = "select id from admin where username='"+userName+"'";
		}
		
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeQuery();
			if(dbconn.next()){
				j = dbconn.getInt(1);
			}
		}catch(Exception e){
			yhlog.warn("When check a user whether is exist,throw an Exception!The user name is:"+userName+".");
		}finally{
			dbconn.close();
		}
		if(j>0){ //该用户存在
			return j;
		}else{  // 无此用户
			return 0;
		}
	}
	
	public AdminUser getUserById(int id) throws DBConnException {
		AdminUser curUser = null;
		String sql = "select a.username,a.password,a.groupid,a.groupname,a.lasttime,a.lastip from admin as a where a.id="+id;
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeQuery();
			if(dbconn.next()){
				curUser = new AdminUser();
				curUser.setId(id);
				curUser.setUsername(dbconn.getString(1));
				curUser.setPassword(dbconn.getString(2));
				curUser.setGroupid(dbconn.getInt(3));
				curUser.setGroupname(dbconn.getString(4));
				curUser.setLasttime(dbconn.getString(5));
				curUser.setLastip(dbconn.getString(6));
				
			}
		}catch(Exception e){
			yhlog.warn("When get a user,throw an Exception!The user id is:"+id+".");
		}finally{
			dbconn.close();
		}
		return curUser;
	}

	public List getAllUser() throws DBConnException {
		ArrayList userlist = new ArrayList();
		AdminUser curUser = null;
		String sql = "select a.id,a.username,a.password,a.groupid,a.groupname,a.lasttime,a.lastip from admin as a";
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeQuery();
			while(dbconn.next()){
				curUser = new AdminUser();
				curUser.setId(dbconn.getInt(1));
				curUser.setUsername(dbconn.getString(2));
				curUser.setPassword(dbconn.getString(3));
				curUser.setGroupid(dbconn.getInt(4));
				curUser.setGroupname(dbconn.getString(5));
				curUser.setLasttime(dbconn.getString(6));
				curUser.setLastip(dbconn.getString(7));
				
				userlist.add(curUser);
			}
		}catch(Exception e){
			e.printStackTrace();
			yhlog.warn("When get all users of system,throw an Exception!");
		}finally{
			dbconn.close();
		}
		return userlist;
	}

	public boolean updateAdminUser(AdminUser user) throws DBConnException {
		int i = 0;
		String sql = "";
		md5 = new MD5();
		AdminUser curUser = user;
		int id = curUser.getId();
		String password = curUser.getPassword();
		int groupid = curUser.getGroupid();
		String groupname = curUser.getGroupname();
		
		if(password!=null && password.length()<1){	// 只更新用户组信息
			sql = "update admin set groupid='"+groupid+"',groupname='"+groupname+"' where id="+id;	
		}
		if(password!=null && password.length()>1){ 
			password = md5.getMD5ofStr(password);
			sql = "update admin set password='"+password+"',groupid='"+groupid+"',groupname='"+groupname+"' where id="+id;
		}
		
		try{
			dbconn = new DBConnect(sql);
			i = dbconn.executeUpdate();
		}catch(Exception e){
			yhlog.warn("When update a user,throw an Exception!The user id is:"+id+".");
		}finally{
			dbconn.close();
		}
		if(i>0){
			yhlog.info("Update a user successfully,the user id is:"+id+".");
			return true;
		}else{
			yhlog.info("Update a user unsuccessfully.the user id is:"+id+".");
			return false;
		}
	}

	public void loginUser(AdminUser user) throws DBConnException {
		AdminUser curUser = user;
		int id = curUser.getId();
		String lasttime = curUser.getLasttime();
		String lastip = curUser.getLastip();
		
		String sql = "update admin set lasttime='"+lasttime+"',lastip='"+lastip+"' where id="+id;
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeUpdate();
		}catch(Exception e){
			yhlog.warn("When update a login user's info,throw an Exception!The user id is:"+id+".");
		}finally{
			dbconn.close();
		}
	}

	public int getUserGroup(int id) throws DBConnException {
		int p = 0;
		String sql = "select a.groupid from admin as a where a.id="+id;
		try{
			dbconn = new DBConnect(sql);
			dbconn.executeQuery();
			while(dbconn.next()){
				p = dbconn.getInt(1);
			}
		}catch(Exception e){
			yhlog.warn("When get a user group id,throw an Exception!The user id is:"+id+".");
		}finally{
			dbconn.close();
		}
		if(p<=0){
			yhlog.info("Get a user group id unsuccessfully,the user id is:"+id+".");
		}
		return p;
	}

}

⌨️ 快捷键说明

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