Use Herbernate to access MySQL

MySQL database used vary from general MIS system to game development: check the current player whether is a valid or not while they login in the game lobby, and overwrite the critical player record during game playing to avoid some one user sensitive data losing or some one crack the user player rule(this will do periodically, may be once per minute for example).

Herbernate itself provides some good structure  to build MIS system rapidly, and it wrap the database access well so we do not need to care the lower level now. And the flow of using Herbernate to access database is very easy:
1)   Get a new access session;
2)   Do any SQL operation (SELECT, UPDATE, DELETE);
3)   Commit your transition(this will make your several operation like just one atom operation ).
4)    Close your database access session.
Let check the source code here.

Get database session
    private static final SessionFactory sessionFactory;
    private static Session sSession;
    
    static 
    {
        sSession = null;
        
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable ex)
        {
            System.err.println("Inital SessionFactory creatiion failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static Session OpenSession()
    {
        sSession = sessionFactory.openSession();
        return sSession;
    }
 
Close database access session
    public static void CloseSession()
    {
        if ( sSession != null ) {
            sSession.close();
            sSession = null;
        }
    }
 
SQL Operation
    public void Save(News news) 
    {
        try {
            Session session = OpenSession();
            session.beginTransaction();
            session.save(news);
            session.getTransaction().commit();
        }
        catch (HibernateException ex) {
            ex.printStackTrace();
        }
        finally {
            CloseSession();
        }
    }
    
    public void SelectById(int id)
    {
        try {
            Session session = OpenSession();
            session.beginTransaction();
            
            String sql = " from News where id=" + id;
            List<News> result = session.createQuery(sql).list();
            
            for (int i = 0; i < result.size(); i++)
            {
                News news = result.get(i);
                
                System.out.println(news.getId());
                System.out.println(news.getTitle());
                System.out.println(news.getContent());
                System.out.println(news.getDate());
                
                System.out.println("====================");
            }
            
            session.getTransaction().commit();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        finally {
            CloseSession();
        }
    }
    
    public void UpdateTitle(int id, String title) 
    {
        try 
        {
            Session session = OpenSession();
            session.beginTransaction();
            News news = (News)session.load(News.class, new Integer(id));
            news.setTitle(title);
            session.update(news);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        finally {
            CloseSession();
        }
    }
    
    public void DeleteById(int id) 
    {
        try 
        {
            Session session = OpenSession();
            session.beginTransaction();
            News news = (News)session.load(News.class, new Integer(id));            
            session.delete(news);
            session.getTransaction().commit();
        }catch (HibernateException e) {
            e.printStackTrace();
        }
        finally {
            CloseSession();
        }
    }

The full source code could be found from here.

转载于:https://www.cnblogs.com/open-coder/archive/2013/05/05/3060556.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值