?? usermanageimpl.java
字號:
for (int i = 0; i < list.size(); i++) {
a[i] = String.valueOf(list.get(i));
if (a[i].trim().equals("1")) {
mkOne = true;
}
if (!a[i].trim().equals("1") && a[i].trim().equals("2")) {
mkTwo = true;
}
}
if (mkOne == true) {
userVO.setMk(1);
}
if (mkOne != true && mkTwo == true) {
userVO.setMk(2);
}
if (mkOne != true && mkTwo != true) {
userVO.setMk(0);
}
HibernateUtil.saveOrUpdate(userVO);
int userId = userVO.getUserId();
Connection conn = HibernateUtil.getConnection();
try {
Statement stm = conn.createStatement();
if (!list.isEmpty()) {
// 刪除原來的紀錄
String sql_delete = "delete from userAndRole where userId="
+ userId;
stm.execute(sql_delete);
for (int j = 0; j < list.size(); j++) {
String sql = "insert into userAndRole values(" + userId
+ "," + a[j] + ")";
stm.execute(sql);
}
}
stm.close();
} catch (Exception e) {
commit = false;
e.printStackTrace();
}
try {
HibernateUtil.endTransaction(commit);
} catch (HibernateException e) {
e.printStackTrace();
}
if (commit) {
return Constants.SUCCESS;
} else {
return Constants.FAILURE;
}
}
/**
* 7.改變用戶狀態
*
* @param userId
* @return根據參數,更新表user中的用戶狀態。 成功返回SUCCESS,失敗返回FALSE。
*/
public String changeUserState(String userId) {
UserVO userVO = (UserVO) HibernateUtil.getVOByID(UserVO.class, Integer
.parseInt(userId));
String sql = null;
int state = userVO.getUserState();
if (state == 0) {
sql = "update users set userState=" + 1 + " where userId='"
+ userId + "'";
}
if (state == 1) {
sql = "update users set userState=" + 0 + " where userId='"
+ userId + "'";
}
Connection conn = HibernateUtil.getConnection();
try {
Statement stm = conn.createStatement();
stm.execute(sql);
stm.close();
return Constants.SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return Constants.FAILURE;
} finally {
// try {
// conn.close();
// } catch (SQLException e1) {
// e1.printStackTrace();
// }
}
}
/**
* 8.刪除用戶
*
* @param userId
* @return根據參數從表user和userAndRole中刪除數據, 用事務管理機制。成功返回SUCCESS,失敗返回FALSE。
*/
public String deleteUser(String userId) {
UserVO aa = (UserVO) HibernateUtil.getVOByID(UserVO.class, Integer
.parseInt(userId));
String str = HibernateUtil.delete(aa);
return str;
}
/**
* 9.修改用戶口令
*
* @param userId
* @param password
* @根據參數修改表user信息。 成功返回SUCCESS,失敗返回FALSE。
*/
public String editUserPassword(String userId, String password) {
UserVO uservo = (UserVO) HibernateUtil.getVOByID(UserVO.class, Integer
.parseInt(userId));
// String pw=uservo.getUserPassWord();
uservo.setUserPassWord(password);
String str = HibernateUtil.saveOrUpdate(uservo);
return str;
}
/**
* 10.判斷用戶
*
* @param userVO
* @return根據參數從表user信息讀取信息。判斷用戶是否存在, 成功返回SUCCESS,失敗返回FALSE。
*/
public String checkUser(UserVO userVO) {
Connection conn = HibernateUtil.getConnection();
int userid = userVO.getUserId();
String sql = "select userId from users where userid=" + userid;
try {
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(sql);
if (rs.next()) {
return Constants.SUCCESS;
}
stm.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
HibernateUtil.closeConnection();
}
return Constants.FAILURE;
}
/**
* 11.判斷用戶
*
* @param companyId
* @param departmentId
* @return根據參數從表user表讀取信息。 成功返回List,失敗返回null
*/
public List getUserListById(String companyId, String departmentId) { // 新版
List result = new ArrayList();
String hql = null;
if (companyId.equals("0") && (departmentId.equals("0"))) {
hql = "from UserVO";
System.out.println("hql= " + hql);
// return null;
} else if ((!companyId.equals("0")) && departmentId.equals("0")) {
hql = "from UserVO as u where " + " u.companyId=" + companyId;
System.out.println("hql=" + hql);
} else if (companyId.equals("0") && (!departmentId.equals("0"))) {
hql = "from UserVO as u where " + " u.departmentId = "
+ departmentId;
System.out.println("hql=" + hql);
} else {
hql = "from UserVO as u where " + "u.companyId=" + companyId
+ " and u.departmentId= " + departmentId;
System.out.println("hql=" + hql);
}
// String hql="from DepartmentVO";
try {
// 調用HibernateUtil方法進行查詢
result = HibernateUtil.queryHQL(hql);
} catch (HibernateException e) {
e.printStackTrace();
}
if (null == result || result.size() == 0) {
return null;
} else {
return result;
}
}
/**
* 12.判斷登陸用戶是否存在
*
* @param userName
* @param ps
* @return 用戶信息 存在返回userVO.不存在返回null
*/
public UserVO loginUser(String userName, String userPsWd) {
UserVO user = null;
if (userName == null || userName.trim().length() == 0) {
return null;
}
if (userPsWd == null || userPsWd.trim().length() == 0) {
return null;
}
boolean result = false;
Connection conn = HibernateUtil.getConnection();
String sql = " select * from users where userName = '" + userName
+ "'and" + " userPassWord='" + userPsWd + "'";
System.out.println("sql=" + sql);
try {
// 創建Statement對象
Statement stat = conn.createStatement();
// 定義數據集
ResultSet rs = stat.executeQuery(sql);
if (rs.next()) {
// String password = rs.getString("userPassWord");
// System.out.println("dfdfdf"+password);
// if (userPsWd.equals(password)) {
System.out.println("dfdfdf");
// 輸入密碼和數據庫的密碼一致,把用戶取到返回去
user = (UserVO) HibernateUtil.getVOByID(UserVO.class, rs
.getInt("userId"));
stat.close();
rs.close();
return user;
} else {
return null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
HibernateUtil.closeConnection();
}
return user;
}
public boolean checkUserName(String userName) {
List result_c = null;
List result_d = null;
boolean commit = true;
String hql_c = null;
String hql_d = null;
hql_c = "from CompanyVO as c where c.companyName='" + userName + "'";
hql_d = "from DepartmentVO as d where d.departmentName='" + userName
+ "'";
try {
HibernateUtil.beginTransaction();
} catch (HibernateException e) {
e.printStackTrace();
}
// //操作區
try {
result_c = HibernateUtil.queryHQL(hql_c);
result_d = HibernateUtil.queryHQL(hql_d);
} catch (Exception e) {
commit = false;
e.printStackTrace();
}
try {
HibernateUtil.endTransaction(commit);
} catch (HibernateException e) {
e.printStackTrace();
} finally {
}
// 判斷返回值
if (result_c.isEmpty() && result_d.isEmpty()) {
return false;
} else {
return true;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -