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;}