Hibernate common database operation code _JSP programming

Source: Internet
Author: User
Tags commit sessions
Insert Method
Copy Code code as follows:

public void Insert (Object o) {Session session = Hibernatesessionfactory.currentsession (); Transaction t = session.begintransaction (); Session.save (o); T.commit (); Hibernatesessionfactory.closssession ();}

Delete Method
Copy Code code as follows:

public void Delete (Object o,serializable ID) {Sessions session = Hibernatesessionfactory.currentsession (): Transaction t = Session.begintransaction (); Object o = Session.get (o.class,id); if (o!=null) {session.delete (o);} T.commit (); Hibernatesessionfactory.closssession ();}


Update Method
Copy Code code as follows:

public void update (Object o,serializable ID) {Sessions session = Hibernatesessionfactory.currentsession (); Transaction t = session.begintransaction (); session.update (O,id); T.commit (); Hibernatesessionfactory.closssession ();}

A general Select method based on HQL
Copy Code code as follows:

Public ArrayList Select (String sql) {Sessions session = Hibernatesessionfactory.currentsession (); Query query = createquery (SQL); List List = Query.list (); Hibernatesessionfactory.closssession (); return (ArrayList) list;

a Universal Select method based on SQL
Copy Code code as follows:

Public ArrayList Select (String sql) throws exception{session session = Hibernatesessionfactory.currentsession (); Connection con = session.connection (); PreparedStatement pstmt = con.preparedstatement (sql); ResultSet rs = Pstmt.executequery (); ResultSetMetaData RSMD = Rs.getmetadata (); Hashtable ht = NULL; ArrayList array = new ArrayList () while (Rs.next ()) {ht = new Hashtable (), for (int i=0;i<rsmd.getcolumncount (); i++) { Ht.put (Rsmd.getcolumnname (i+1), Rs.getobject (i+1)); Array.add (HT);} Hibernatesessionfactory.closssession (); return array;}

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.