package de.uhilger.baselink; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Ulrich Hilger */ public class Updater extends DBActor { private static final Logger logger = Logger.getLogger(Updater.class.getName()); public Updater(PersistenceManager pm) { this.pm = pm; } /** * update an object in the database * *

Use this method for single updates. In cases where * several subsequent updates for objects of the same class * are required the use of method update(Object, Record) * is recommended instead to minimise instantiation * overhead.

* * @param o object to update * @return the object that was updated */ public Object update(Object o) { return update(o, new GenericRecord(o.getClass())); } /** * update an object in the database * *

Use this method for single updates. In cases where * several subsequent updates for objects of the same class * are required the use of method update(Connection, Object, Record) * is recommended instead to minimise instantiation * overhead.

* * @param c the connection to use, expected to be open and established * @param o object to update * @return the object that was updated */ public Object update(Connection c, Object o) { return update(c, o, new GenericRecord(o.getClass())); } /** * update an object in the database * @param o object to update * @param record reference to object to use to map between object and database * @return the object that was updated */ public Object update(Object o, Record record) { Connection c = null; try { c = pm.getConnection(); o = update(c, o, record); c.close(); c = null; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeConnectionFinally(c); } return o; } /** * update an object in the database * @param c the connection to use, expected to be open and established * @param o object to update * @param record reference to object to use to map between object and database * @return the object that was updated */ public Object update(Connection c, Object o, Record record) { Object updatedObject = null; PreparedStatement ps = null; try { ps = record.getUpdateStatment(c, o); ps.executeUpdate(); ps.close(); ps = null; updatedObject = o; } catch (Exception ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeStatementFinally(ps); } return updatedObject; } }