📄 groupdaoim.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.AdminGroup;
import com.yhcms.manage.admin.itface.GroupDao;
/**
* <p>Title:系统后台用户组的相关操作</p>
* <li>后台用户组的各项操作</li>
* <b>CopyRight: yyhweb[由由华网]</b>
* @author stephen
* @version YH-2.0
*/
public class GroupDaoIm implements GroupDao {
private static Logger yhlog = Logger.getLogger(GroupDaoIm.class.getName());
private DBConnect dbconn = null;
private static GroupDaoIm groupdao = new GroupDaoIm();
/**
* @return 取得一个后台用户组操作对象
*/
public static GroupDaoIm getInstance(){
return groupdao;
}
public int getMaxId() throws DBConnException {
int maxId = 0;
String sql = "select max(id) from magroup";
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 admin group,throw an Exception!");
}finally{
dbconn.close();
}
return maxId;
}
public boolean addGroup(AdminGroup group) throws DBConnException {
int i = 0;
// 取得后台用户组最大Id+1 作为新用户组的Id
int maxid = getMaxId()+1;
AdminGroup curGroup = group;
String name = curGroup.getName();
String popedom = curGroup.getPopedom();
String about = curGroup.getAbout();
String addby = curGroup.getAddby();
String sql = "insert into magroup(id,name,popedom,about,addby) values ("+
maxid+",'"+name+"','"+popedom+"','"+about+"','"+addby+"')";
try{
dbconn = new DBConnect(sql);
i = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("When add an admin group,throw an Exception!");
}finally{
dbconn.close();
}
if(i>0){
yhlog.info("Add an admin group successfully.");
return true;
}else{
yhlog.info("Add an admin group unsuccessfully.");
return false;
}
}
public boolean delGroup(int id) throws DBConnException {
int i = 0;
int j = 0;
String sqlg = "delete from magroup where id="+id;
String sqlu = "update admin set groupid=0,groupname='' where groupid="+id;
try{
dbconn = new DBConnect(sqlg);
i = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("When delete an admin group,throw an Exception!The group id is:"+id+".");
}finally{
dbconn.close();
dbconn = null;
}
try{
dbconn = new DBConnect(sqlu);
j = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("When delete a group,update admin users,throw an Exception!");
}finally{
dbconn.close();
}
if(i>0 && j>0){
yhlog.info("Delete a group and update admin users successfully,the group id is:"+id+".");
return true;
}else{
yhlog.info("Delete a group unsuccessfully,the group id is:"+id+".");
return false;
}
}
public AdminGroup getGroupById(int id) throws DBConnException {
AdminGroup curGroup = null;
String sql = "select m.name,m.about,m.popedom,m.addby from magroup as m where m.id="+id;
try{
dbconn = new DBConnect(sql);
dbconn.executeQuery();
if(dbconn.next()){
curGroup = new AdminGroup();
curGroup.setId(id);
curGroup.setName(dbconn.getString(1));
curGroup.setAbout(dbconn.getString(2));
curGroup.setPopedom(dbconn.getString(3));
curGroup.setAddby(dbconn.getString(4));
}
}catch(Exception e){
yhlog.warn("When get a group,throw an Exception!The group id is:"+id+".");
}finally{
dbconn.close();
}
return curGroup;
}
public int getGroupById(String gname) throws DBConnException {
int gId = 0;
String sql = "select id from magroup where name='"+gname+"'";
try{
dbconn = new DBConnect(sql);
dbconn.executeQuery();
if(dbconn.next()){
gId = dbconn.getInt(1);
}
}catch(Exception e){
yhlog.warn("When check a group whether exist,throw an Exception!The group name is:"+gname+".");
}finally{
dbconn.close();
}
return gId;
}
public List getAllGroup() throws DBConnException {
ArrayList grouplist = new ArrayList();
AdminGroup curGroup = null;
String sql = "select m.id,m.name,m.about,m.popedom,m.addby from magroup as m ";
try{
dbconn = new DBConnect(sql);
dbconn.executeQuery();
while(dbconn.next()){
curGroup = new AdminGroup();
curGroup.setId(dbconn.getInt(1));
curGroup.setName(dbconn.getString(2));
curGroup.setAbout(dbconn.getString(3));
curGroup.setPopedom(dbconn.getString(4));
curGroup.setAddby(dbconn.getString(5));
grouplist.add(curGroup);
}
}catch(Exception e){
e.printStackTrace();
yhlog.warn("When get all groups of system,throw an Exception!");
}finally{
dbconn.close();
}
return grouplist;
}
public boolean updateGroup(AdminGroup group) throws DBConnException {
int i = 0;
int j = 0;
AdminGroup curGroup = group;
int id = curGroup.getId();
String name = curGroup.getName();
String about = curGroup.getAbout();
String popedom = curGroup.getPopedom();
String addby = curGroup.getAddby();
String sql = "update magroup set name='"+name+"',about='"+about+"',popedom='"+popedom+"',addby='"+addby+"' where id="+id;
String sqlu = "update admin set groupname='"+name+"' where groupid="+id;
try{
dbconn = new DBConnect(sql);
i = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("When update a group,throw an Exception!The group id is:"+id+".");
}finally{
dbconn.close();
}
if(i>0){
try{
dbconn = new DBConnect(sqlu);
j = dbconn.executeUpdate();
}catch(Exception e){
yhlog.warn("When update all a group's users,throw an Exception!The group id is:"+id+".");
}finally{
dbconn.close();
}
return true;
}else{
yhlog.info("Update a group unsuccessfully,the group id is:"+id+".");
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -