⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userdao.java

📁 实现在线书店的基本功能 应用了hibernate+jsp 技术采用了mvc三层设计模式
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -