📄 departmentmanageimpl.java
字号:
}
return result;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stm.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
// if(result!=null&&result.size()>0){
// for(int i=0;i<result.size();i++){
// CompanyVO companyVO=(CompanyVO) result.get(i);
// result2.add(companyVO);
// }
// }else{
// return null;
// }
return null;
}
/**
* 6.新增部门
*
* @param departmentvo
* @return将对象departmentVO新增到表department中, 成功返回SUCCESS,失败返回FALSE。
*/
public String saveDepartment(DepartmentVO departmentvo) {
String str = HibernateUtil.save(departmentvo);
return str;
}
/**
* 7更新部门.
*
* @param departmentvo
* @return将对象departmentVO更新到表department中, 成功返回SUCCESS,失败返回FALSE。
*/
public String updateDepartment(DepartmentVO departmentvo) {
HibernateUtil.saveOrUpdate(departmentvo);
String company_p = String.valueOf(departmentvo.getDepartment_p());
String sql = "update departmentrelation set department_p = "
+ departmentvo.getDepartment_p() + " where department_s = "
+ departmentvo.getDepartmentId();
if (!company_p.trim().equals("0")) {
Connection con = HibernateUtil.getConnection();
Statement sta = null;
try {
sta = con.createStatement();
sta.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
return Constants.FAILURE;
} finally {
try {
sta.close();
HibernateUtil.closeConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
return Constants.SUCCESS;
}
/**
* 8.改变部门状态
*
* @param depmentId
* @return根据参数调用方法getDepartment()得到对象DepartmentVO, 更改DepartmentVO对象状态,调用方法saveDepartment(),
* 成功返回SUCCESS,失败返回FALSE。
*/
public String changeDepartmentState(String departmentId) {
String sql = "";
Statement stm = null;
DepartmentVO departmentVO = (DepartmentVO) HibernateUtil.getVOByID(
DepartmentVO.class, Integer.parseInt(departmentId));
Connection conn = HibernateUtil.getConnection();
if (departmentVO.getDepartmentState() == 0) {
sql = "update department set departmentState=" + 1
+ " where departmentId=" + departmentId;
} else {
sql = "update department set departmentState=" + 0
+ " where departmentId=" + departmentId;
}
try {
stm = conn.createStatement();
stm.execute(sql);
return Constants.SUCCESS;
} catch (SQLException e) {
e.printStackTrace();
return Constants.FAILURE;
} finally {
try {
stm.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
/**
* 9.删除部门
*
* @param depmentId
* @return根据参数从表department中删除数据, 成功返回SUCCESS,失败返回FALSE。
*/
public String deleteDepartment(String depmentId) {
DepartmentVO departmentVO = (DepartmentVO) HibernateUtil.getVOByID(
DepartmentVO.class, Integer.parseInt(depmentId));
Connection con = HibernateUtil.getConnection();
Statement stm = null;
try {
HibernateUtil.delete(departmentVO);
stm = con.createStatement();
stm.execute("delete from departmentrelation where department_p="
+ depmentId);
stm.execute("delete from departmentrelation where department_s="
+ depmentId);
stm
.execute("Update department Set department_p=0 where departmentId = "
+ depmentId);
return Constants.SUCCESS;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return Constants.FAILURE;
} finally {
try {
stm.close();
HibernateUtil.closeConnection();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
/**
* 新增部门之间关系表
*
* @param DepartmentVO
* departmentvo
* @return String
*/
public String saveDepartmentRelation(DepartmentVO departmentvo) {
Connection con = HibernateUtil.getConnection();
Statement stm = null;
int departmentId = 0;
String sql1 = "";
String sql2 = "insert into department"
+ "(departmentName,department_p,companyId,departmentPhone,departmentAuthority,departmentState,departmentType)"
+ "values('" + departmentvo.getDepartmentName() + "',"
+ departmentvo.getDepartment_p() + ","
+ departmentvo.getCompanyId() + ",'"
+ departmentvo.getDepartmentPhone() + "',"
+ departmentvo.getDepartmentAuthority() + ","
+ departmentvo.getDepartmentState() + ",'"
+ departmentvo.getDepartmentType() + "')";
try {
stm = con.createStatement();
stm.execute(sql2);
System.out.println(sql2);
ResultSet rs = con.createStatement().executeQuery(
"SELECT @@IDENTITY as id");
if (rs.next()) {
departmentId = Integer.parseInt(rs.getString("id").toString());
System.out.println(departmentId);
}
sql1 = "insert into departmentrelation(department_p, department_s) values ("
+ departmentvo.getDepartment_p()
+ ", "
+ departmentId
+ ")";
stm.execute(sql1);
System.out.println(sql1);
stm.close();
return Constants.SUCCESS;
} catch (SQLException e) {
e.printStackTrace();
return Constants.FAILURE;
}
}
public boolean checkDepartmentName(String departmentName) {
List result = null;
String hql = null;
hql = "from DepartmentVO as d where d.departmentName='"
+ departmentName + "'";
try {
result = HibernateUtil.queryHQL(hql);
System.out.println(result);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (result.isEmpty()) {
return false;
} else {
return true;
}
}
public List getSonDepartmentId(int departmentId, boolean cludSelf) {
List list = new ArrayList();
int layer = 0;
if (cludSelf) {
DepartmentVO dv = this.getDepartment(departmentId + "");
if (dv != null) {
layer++;
int id = dv.getDepartmentId();
String name = dv.getDepartmentName();
Map map = new HashMap();
map.put("ID", id + "");
map.put("NAME", name);
map.put("LAYER", layer + "");
list.add(map);
}
}
Connection conn = HibernateUtil.getConnection();
getSonId(departmentId, list, conn, layer + 1);
return list;
}
private void getSonId(int departmentId, List list, Connection conn,
int layer) {
String sql = "select departmentId,departmentName from department where department_p="
+ departmentId;
Statement stat = null;
ResultSet rs = null;
try {
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("departmentId");
String name = rs.getString("departmentName");
// System.out.println(departmentId + "==" +
// id+"--"+rs.getString("departmentName"));
Map map = new HashMap();
map.put("ID", id + "");
map.put("NAME", name);
map.put("LAYER", layer + "");
list.add(map);
getSonId(id, list, conn, layer + 1);
}
return;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stat.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -