Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package app.flex.db; import com.serializable.Phone; import java.util.ArrayList; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; /** * * @author anshuman */ public class Responder { private SessionFactory factory; private Session session; public Responder() { try { factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); } catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } session = factory.openSession(); } // public List<String> callMe() { // System.out.println("I am being invoked!"); // List<String> result = new ArrayList<String>(); // result.add("Anshuman"); // result.add("Sagar"); // result.add("Tina"); // return result; // } public List<Phone> getAllNumbers() { List<Phone> users = new ArrayList<Phone>(); Transaction trns = null; try { trns = session.beginTransaction(); users = session.createCriteria(Phone.class).list(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } factory.close(); System.out.println(users.toArray()); return users; } public Boolean addMember(String nm, String mb) { Phone ME = new Phone(); ME.setPerson_name(nm); ME.setPerson_mobile(mb); Transaction tx = null; Boolean resp = false; try { tx = session.beginTransaction(); session.save(ME); tx.commit(); resp = true; } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); resp = false; } finally { session.close(); resp = true; } factory.close(); return resp; } }