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 loby, 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; 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 VoidClosesession (){If(Ssession! =Null) {SSession. Close (); Ssession=Null;}}
SQL operation
Public Void Save (News news ){ Try {Session =Opensession (); Session. begintransaction (); Session. Save (News); Session. gettransaction (). Commit ();} Catch (Hibernateexception ex) {ex. printstacktrace ();} Finally {Closesession ();}} Public Void Selectbyid ( Int ID ){ Try {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 =Opensession (); Session. begintransaction (); 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 = Opensession (); Session. begintransaction (); 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 cocould be found from here.