📄 userdao.java
字号:
/**
*
*/
package Entity.Dao;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import Entity.Pojo.UserInfo;
/**
* @author Administrator
*
*/
public class UserDao {
private static Session session = null;
private static Transaction tx = null;
/***************************************************************************
* 登录处理
*
* @param name
* @param pasd
* @return
*/
public static boolean getAdmin(String name, String pasd) {
int count = 0;
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
List list;
try {
String hql = "select count(*) from UserInfo as u where u.userName=? and u.userPwd=?";
Query query = session.createQuery(hql);
query.setString(0, name);
query.setString(1, pasd);
list = query.list();
tx.commit();
count = ((Integer) list.get(0)).intValue();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
if (count == 0) {
return false;
} else {
return true;
}
}
public static int getUserID(String name, String pasd) {
int user_id = 0;
try {
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
List lt;
String hql = "select u.userId from UserInfo as u where u.userName=? and u.userPwd=?";
Query query = session.createQuery(hql);
query.setString(0, name);
query.setString(1, pasd);
lt = query.list();
tx.commit();
Iterator iter = lt.iterator();
while (iter.hasNext()) {
user_id = (Integer) iter.next();
}
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
HibernateSessionFactory.closeSession();
}
return user_id;
}
/***************************************************************************
* 会员注册处理
*
* @param user
*/
public void regNewUer(UserInfo user) {
try {
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
session.save(user);
session.flush();
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
HibernateSessionFactory.closeSession();
}
}
/***************************************************************************
* 得到登录用户的个人信息
*/
public static List getSglUser(Integer userId) {
List list = null;
try {
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
String hql = "From UserInfo u where u.userId=?";
Query query = session.createQuery(hql);
query.setInteger(0, userId);
list = query.list();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
HibernateSessionFactory.closeSession();
}
return list;
}
/***************************************************************************
* 修改用户信息
*/
public static boolean upUserInfo(Integer userid, String name, String pasd,
String email) {
int count = 0;
try {
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
String hql = "update UserInfo set userName=?,userPwd=?,userEmail=? where userId=?";
Query query = session.createQuery(hql);
query.setString(0, name);
query.setString(1, pasd);
query.setString(2, email);
query.setInteger(3, userid);
count = query.executeUpdate();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
System.out.println("user infomation update fail............");
if (tx != null) {
tx.rollback();
}
} finally {
HibernateSessionFactory.closeSession();
}
if (count == 0) {
return false;
} else {
return true;
}
}
public static List getAllUser() {
List ulist = null;
try {
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
String hql = "From UserInfo";
Query query = session.createQuery(hql);
ulist = query.list();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
HibernateSessionFactory.closeSession();
}
return ulist;
}
public static boolean deleteUser(Integer userId) {
int count = 0;
try {
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
UserInfo uf = (UserInfo) session.load(UserInfo.class, userId);
session.delete(uf);
session.flush();
tx.commit();
count = 1;
} catch (Exception e) {
e.printStackTrace();
count = 0;
if (tx != null) {
tx.rollback();
}
} finally {
HibernateSessionFactory.closeSession();
}
if (count == 0) {
return false;
} else {
return true;
}
}
/***************************************************************************
* 实例化dao方法
*/
private static UserDao dao = new UserDao();
public static UserDao getInstance() {
return dao;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -