📄 companymanageimpl.java
字号:
/**
*
*/
package com.seavision.PermissionManage.components;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.seavision.PermissionManage.help.Constants;
import com.seavision.PermissionManage.help.DaoFectory;
import net.sf.hibernate.HibernateException;
import com.seavision.PermissionManage.help.HibernateUtil;
import com.seavision.PermissionManage.vo.CompanyVO;
/**
* @author Administrator
*
*/
public class CompanyManageImpl implements CompanyManage {
/**
* 1.获取公司列表 返回CompanyVO对象组成的list
*/
public List getCompanyList() {
List result = new ArrayList();
String hql = "from CompanyVO";
try {
// 调用HibernateUtil方法进行查询
result = HibernateUtil.queryHQL(hql);
} catch (HibernateException e) {
e.printStackTrace();
}
if (null == result || result.size() == 0) {
return result;
} else {
return result;
}
}
public List getCompanyList(int companyId) {
List result = new ArrayList();
String hql = "from CompanyVO where company_p != " + companyId;
try {
// 调用HibernateUtil方法进行查询
result = HibernateUtil.queryHQL(hql);
} catch (HibernateException e) {
e.printStackTrace();
}
if (null == result || result.size() == 0) {
return result;
} else {
return result;
}
}
/**
*
* 2.获取公司信息
*
* return 根据参数从表company中读取信息, 封装CompanyVO对象中,成功返回CompanyVO对象,失败返回null。
*/
public CompanyVO getCompany(int companyId) {
// 调用HibernateUtil方法,通过主见,得到对象
CompanyVO companyVO = (CompanyVO) HibernateUtil.getVOByID(
CompanyVO.class, companyId);
//System.out.println("dkfdkjfkdf"+companyVO.getCompany_p());
return companyVO;
}
/**
* 3.获取上级公司信息列表 return 根据参数从表company中读取信息,
* 封装String类型的对象中,成功返回String类型对象,失败返回null。
*/
public static void main(String[] args) {
CompanyManage companyManage = DaoFectory.getCompanyManage();
String b = companyManage.getParentOfCompanyName(0);
System.out.println(b);
}
public String getParentOfCompanyName(int companyId) {
// 调用HibernateUtil方法,通过主键,得到对象
CompanyVO companyVO = new CompanyVO();
companyVO = (CompanyVO) HibernateUtil.getVOByID(CompanyVO.class, companyId);
String rr = companyVO.getCompanyName();
return rr;
}
/**
* 4.获取下级公司信息列表
*
* @param companyId
* @return根据参数从表companyRelation中读取下级公司编号,
* @封装到List对象中。根据List编号列表,从表company中读取信息,
* @封装到CompanyVO对象中,成功返回List,失败返回null。
*/
public List getSonOfCompanyList(int companyId) {
Connection conn = HibernateUtil.getConnection();
String sql = "select * from company where company_p = " + companyId;
List result = new ArrayList();
Statement stat = null;
ResultSet rs = null;
try {
// 定义数据集
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while(rs.next()) {
CompanyVO companyvo = new CompanyVO();
companyvo.setCompany_p(rs.getInt("company_p"));
companyvo.setCompanyAddress(rs.getString("companyAddress"));
companyvo.setCompanyAuthority(rs.getString("companyAuthority"));
companyvo.setCompanyId(rs.getInt("companyId"));
companyvo.setCompanyName(rs.getString("companyName"));
companyvo.setCompanyNet(rs.getString("companyNet"));
companyvo.setCompanyPhone(rs.getString("companyPhone"));
companyvo.setCompanyState(rs.getInt("companyState"));
companyvo.setCompanyType(rs.getString("companyType"));
result.add(companyvo);
}
rs.close();
stat.close();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** 通过companyId获得company_s */
private List getCompany_sId(int companyId) {
// 建立数据库连接
Connection conn = HibernateUtil.getConnection();
String sql = "select company_s from companyrelation where company_p="
+ companyId;
Statement stm = null;
ResultSet rs = null;
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
List c_s = new ArrayList();
while (rs.next()) {
c_s.add(rs.getString("company_s"));
}
return c_s;
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
rs.close();
stm.close();
}catch(NullPointerException n){
n.printStackTrace();
}catch(SQLException s){
s.printStackTrace();
}
}
return null;
}
/**
* 5.增加新公司
*
* @param comapany
* @return将companyVO对象新增到表company中, 成功返回SUCCESS,失败返回FALSE。
*/
public String saveCompany(CompanyVO comapany) {
String str = HibernateUtil.save(comapany);
return str;
}
/**
* 6.更新公司
*
* @param companyVO
* 将companyVO对象更新到表company中, 成功返回SUCCESS,失败返回FALSE。
*/
public String updateCompany(CompanyVO companyVO) {
HibernateUtil.saveOrUpdate(companyVO);
String company_p = String.valueOf(companyVO.getCompany_p());
String sql = "update companyrelation set company_p = "+companyVO.getCompany_p()+" where company_s = "+companyVO.getCompanyId();
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;
}
/**
* 7.改变公司状态
*
* @param companyId
* 根据参数调用方法getCompany()
* 得到对象CompanyVO,通过companyId值,更改CompanyVO对象状态 ,成功返回SUCCESS,
* 失败返回FALSE。
*/
public String changeCompanyState(int companyId) {
Statement stm = null;
String sql = "";
CompanyVO companyVO = (CompanyVO) HibernateUtil.getVOByID(CompanyVO.class, companyId);
Connection conn = HibernateUtil.getConnection();
if (companyVO.getCompanyState() == 0) {
sql = "update company set companyState=" + 1 + " where companyId=" + companyId;
} else {
sql = "update company set companyState=" + 0 + " where companyId=" + companyId;
}
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();
}
}
}
/**
* 8.删除公司
*
* @param companyID
* 根据参数从表中删除数据,成功返回SUCCESS,失败返回FALSE。
*/
public String deleteCompany(int companyId) {
// 调用HibernateUtil方法,通过主键,得到对象
CompanyVO companyVO = (CompanyVO) HibernateUtil.getVOByID(CompanyVO.class,
companyId);
// 调用方法进行删除
Connection con=HibernateUtil.getConnection();
Statement stm = null;
try {
HibernateUtil.delete(companyVO);
stm = con.createStatement();
stm.execute("delete from companyrelation where company_p="+companyId);
stm.execute("delete from companyrelation where company_s="+companyId);
stm.execute("Update company Set company_p=0 where companyId = "+companyId);
stm.execute("Update company Set company_p=0 where company_p = "+companyId);
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();
}
}
}
/**
* 9.新增公司关系表
* @param CompanyVO companyVO
* @return String
* */
public String saveDepartmentRelation(CompanyVO companyVO) {
Connection con=HibernateUtil.getConnection();
Statement stm = null;
int companyId = 0;
String sql1 = "";
String sql2 = "";
sql2 = "insert into company(companyName, company_p, companyAddress, companyPhone,companyAuthority, companyNet, companyState, companyType)" +
"values ('"+companyVO.getCompanyName()+"', "+companyVO.getCompany_p()+", '"+companyVO.getCompanyAddress()+"','"+companyVO.getCompanyPhone()+"',"+companyVO.getCompanyAuthority()+",'"+companyVO.getCompanyNet()+"','"+companyVO.getCompanyState()+"','"+companyVO.getCompanyType()+"')";
try {
stm=con.createStatement();
stm.execute(sql2);
ResultSet rs = stm.executeQuery("SELECT @@IDENTITY as id");
if (rs.next()) {
//companyId = rs.getInt(1);
companyId = Integer.parseInt(rs.getString("id").toString());
System.out.println("companyId==================="+companyId);
}
sql1 = "insert into companyrelation(company_p, company_s) values ("+companyVO.getCompany_p()+", "+companyId+")";
stm.execute(sql1);
stm.close();
return Constants.SUCCESS;
} catch (SQLException e) {
e.printStackTrace();
return Constants.FAILURE;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -