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.