Use Herbernate to access MySQL

來源:互聯網
上載者:User

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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.