📄 roleservice.java
字号:
/**
*
*/
package com.eshop.service;
import java.sql.Statement;
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.eshop.form.roleForm;
import com.eshop.util.DBManager;
import com.eshop.vo.Product;
import com.eshop.vo.Role;
import com.eshop.vo.RolePerm;
/**
* 进行角色相关的业务处理
* @author stu
*
*/
public class RoleService {
public static boolean saveRole(Role role,RolePerm rolePerm){
boolean success=false;
DBManager dbManager=new DBManager();
Connection conn=dbManager.GetConnection();
try {
conn.setAutoCommit(false);//设置自动提交为FALSE
ResultSet rs=null;
Statement st=conn.createStatement();
rs=st.executeQuery("select max(id) as id from role");
int id=0;
while(rs.next()){
id=rs.getInt("id")+1;
}
//向ROLE表中插入数据
PreparedStatement ps=conn.prepareStatement("INSERT INTO role(id,roleName,roleDescn) values(?,?,?)");
ps.setInt(1,id);
ps.setString(2, role.getRoleName());
ps.setString(3, role.getRoleDescn());
ps.executeUpdate();
ps.close();
//向ROLE_PER插入数据
for(int i=0;i<rolePerm.getPid().length;i++){
PreparedStatement ps1=conn.prepareStatement("INSERT INTO role_per(rid,pid) values(?,?)");
ps1.setInt(1, id);
ps1.setInt(2,Integer.parseInt(rolePerm.getPid()[i]));
ps1.executeUpdate();
ps1.close();
}
conn.commit();//提交事务
success=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try{
conn.rollback();//回滚事务
}catch(SQLException ex){
ex.printStackTrace();
}
}
return success;
}
/**获得所有权限
* @return
*/
public List getAllrole(){
List list=new ArrayList();
DBManager dbManager= new DBManager();
Connection conn=dbManager.GetConnection();
try {
PreparedStatement ps=conn.prepareStatement("select id,roleName,roleDescn from role");
ResultSet rs=ps.executeQuery();
Role roleForm=null;
while (rs.next()){
roleForm=new Role();
roleForm.setId(rs.getLong("id"));
roleForm.setRoleName(rs.getString("roleName"));
roleForm.setRoleDescn(rs.getString("roleDescn"));
list.add(roleForm);
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**根据ID或得角信息
* @return
*/
public List getroleById(long id){
List list=new ArrayList();
DBManager dbManager= new DBManager();
Connection conn=dbManager.GetConnection();
try {
PreparedStatement ps=conn.prepareStatement("select id,roleName,roleDescn from role where id="+id);
ResultSet rs=ps.executeQuery();
Role roleForm=null;
while (rs.next()){
roleForm=new Role();
roleForm.setId(rs.getLong("id"));
roleForm.setRoleName(rs.getString("roleName"));
roleForm.setRoleDescn(rs.getString("roleDescn"));
list.add(roleForm);
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 根据角色ID获得权限信息
* @return
*/
public List getPermisByRid(long id){
List list=new ArrayList();
DBManager dbManager= new DBManager();
Connection conn=dbManager.GetConnection();
try {
PreparedStatement ps=conn.prepareStatement("select id,rid,pid from role_per where rid="+id);
ResultSet rs=ps.executeQuery();
RolePerm rolePerm=null;
while (rs.next()){
rolePerm=new RolePerm();
rolePerm.setId(rs.getLong("id"));
rolePerm.setRid(rs.getLong("rid"));
rolePerm.setPermisValue(rs.getLong("pid"));
list.add(rolePerm);
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 删除类
* @param Role
* @return
*/
public boolean deleteRole(Role role){
boolean success=false;
DBManager dbManager= new DBManager();
Connection conn=dbManager.GetConnection();
try {
// Statement stm=conn.createStatement();
PreparedStatement ps=conn.prepareStatement
("delete from role where id='"+role.getId()+"'");
ps.executeUpdate();
ps.close();
conn.commit();
success=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try{
conn.rollback();
}catch(SQLException ex){
ex.printStackTrace();
}
}
return success;
}
/**
* 修改类
* @param product
* @return
*/
public boolean updateRole(Role role,RolePerm rolePerm){
boolean success=false;
DBManager dbManager= new DBManager();
Connection conn=dbManager.GetConnection();
try {
conn.setAutoCommit(false);
// Statement stm=conn.createStatement();
PreparedStatement ps=conn.prepareStatement
("update role set roleName=?,roleDescn=? where id="+role.getId());
ps.setString(1,role.getRoleName());
ps.setString(2, role.getRoleDescn());
ps.executeUpdate();
ps.close();
// 删除ROLE_PER中与该角色ID相关的数据
PreparedStatement ps2=conn.prepareStatement
("delete from role_per where rid='"+role.getId()+"'");
ps2.executeUpdate();
ps2.close();
// 向ROLE_PER插入数据
for(int i=0;i<rolePerm.getPid().length;i++){
PreparedStatement ps1=conn.prepareStatement("INSERT INTO role_per(rid,pid) values(?,?)");
ps1.setLong(1, role.getId());
ps1.setInt(2,Integer.parseInt(rolePerm.getPid()[i]));
ps1.executeUpdate();
ps1.close();
}
conn.commit();
success=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try{
conn.rollback();
}catch(SQLException ex){
ex.printStackTrace();
}
}
return success;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -