package buzzerproxy.db; import org.hibernate.*; import buzzerproxy.db.view.BaseEntry; /** * @author Enger * */ public class DatabaseManager { public Session ses = null; public HibernateUtil util = null; public SessionFactory sesFac = null; // private Log Log = LogFactory.getLog(DatabaseManager.class); /** * */ public DatabaseManager() { try { this.sesFac = HibernateUtil.getSessionFactory(); this.ses = sesFac.openSession(); } catch ( Exception e ) { e.printStackTrace(); } this.util = new HibernateUtil(); } /** * @param baseEntry */ public void createTable(BaseEntry baseEntry) { Transaction tx = null; try { tx = ses.beginTransaction(); ses.createQuery( baseEntry.createTable() ); tx.commit(); ses.close(); } catch ( HibernateException e ) { e.printStackTrace(); if ( tx != null ) try { tx.rollback(); } catch ( HibernateException e1 ) { System.out.println( "rollback not successful" ); } if ( ses != null ) try { ses.close(); } catch ( HibernateException e2 ) { System.out.println( "session close not successful" ); } } } /** * @param dbentry * @return */ public synchronized boolean query(BaseEntry dbentry) { if ( dbentry == null ) throw new NullPointerException(); Transaction tx = null; try { tx = ses.beginTransaction(); ses.save( dbentry ); tx.commit(); ses.close(); return true; } catch ( HibernateException e ) { e.printStackTrace(); if ( tx != null ) try { tx.rollback(); } catch ( HibernateException e1 ) { System.out.println( "rollback not successful" ); } if ( ses != null ) try { ses.close(); } catch ( HibernateException e2 ) { System.out.println( "session close not successful" ); } return false; } } }