userdao.java

来自「实现在线书店的基本功能 应用了hibernate+jsp 技术采用了mvc三层」· Java 代码 · 共 71 行

JAVA
71
字号
package mypack;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.cfg.Configuration;import org.hibernate.SessionFactory;import org.hibernate.Transaction;/** * Hibernate Utility class with a convenient method to get Session Factory object. * * @author Administrator */public class UserDao {    private static final SessionFactory sessionFactory;    static {        try {            // Create the SessionFactory from standard (hibernate.cfg.xml)             // config file.            sessionFactory = new Configuration().configure().buildSessionFactory();        } catch (Throwable ex) {            // Log the exception.             System.err.println("Initial SessionFactory creation failed." + ex);            throw new ExceptionInInitializerError(ex);        }    }    public static SessionFactory getSessionFactory() {        return sessionFactory;    }    public Customer userCheck(String name,String password)    {       Session session1=sessionFactory.openSession();        Query query=session1.createQuery("from Customer as c where c.userName like:name and c.password like:password");        query.setString("name", name);        query.setString("password", password);        Customer user=(Customer) query.uniqueResult();        if(user!=null){             return user;}        else             return null;            }    public void insert(String name,String password,String email)    {        Session session=getSessionFactory().openSession();        Transaction tx=session.beginTransaction();        try        {            Customer customer=new Customer();            customer.setUserName(name);            customer.setPassword(password);            customer.setEmail(email);            session.save(customer);            tx.commit();        }catch(Exception ex)        {                  }finally        {            session.close();        }            }    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?