Use herbernate to access MySQL

Source: Internet
Author: 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 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.