de.janrufmonitor.repository.db
Class AbstractDatabaseHandler

java.lang.Object
  extended by de.janrufmonitor.repository.db.AbstractDatabaseHandler
All Implemented Interfaces:
IDatabaseHandler
Direct Known Subclasses:
AbstractCallDatabaseHandler, AbstractCallerDatabaseHandler, AbstractMultiPhoneCallerDatabaseHandler

public abstract class AbstractDatabaseHandler
extends Object
implements IDatabaseHandler

This abstract class can be used as base class for a database related managers. It contains methods for database creation and prepared statements for manager handling.

Author:
Thilo Brandt

Field Summary
protected  int commit_count
           
protected  Connection m_con
           
protected  Logger m_logger
           
protected  Map m_preparedStatements
           
 
Constructor Summary
AbstractDatabaseHandler(String driver, String connection, String user, String password, boolean initialize)
          Constructor dor a data handler calling the underlaying database
 
Method Summary
protected abstract  void addPreparedStatements()
          Add all required prepares statements to the m_preparedStatements map.
 void commit()
          Commits the changed data to the database.
 void connect()
          Connects to the database specified in the constructor.
protected  void createAttributes(PreparedStatement ps, String uuid, IAttributeMap m)
           
protected abstract  void createTables()
          Creates the database table.
protected  void deleteAttributes(PreparedStatement ps, String uuid)
           
 void disconnect()
          Disconnects the current db and shuts it down.
protected abstract  IRuntime getRuntime()
           
protected  PreparedStatement getStatement(String id)
           
 boolean isConnected()
          Checks wether a connection is established or not.
protected  boolean isInitializing()
          Must return true, if the database tables should be created.
 boolean isKeepAlive()
           
 void rollback()
          Rolls back the db changed since the last commit.
 void setCommitCount(int c)
           
protected  void setInitializing(boolean init)
          Sets the initinal attribute of the database.
 void setKeepAlive(boolean keep)
           
protected  void updateAttributes(PreparedStatement ps, String uuid, IAttributeMap m)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_con

protected Connection m_con

m_preparedStatements

protected Map m_preparedStatements

m_logger

protected Logger m_logger

commit_count

protected int commit_count
Constructor Detail

AbstractDatabaseHandler

public AbstractDatabaseHandler(String driver,
                               String connection,
                               String user,
                               String password,
                               boolean initialize)
Constructor dor a data handler calling the underlaying database

Parameters:
driver - JDBC driver string
connection - connection String
user - user, if required
password - password, if user was set
initialize - indicates wether the db tables must be created or not.
Method Detail

getRuntime

protected abstract IRuntime getRuntime()

createTables

protected abstract void createTables()
                              throws SQLException
Creates the database table.

Throws:
SQLException

addPreparedStatements

protected abstract void addPreparedStatements()
                                       throws SQLException
Add all required prepares statements to the m_preparedStatements map. The key should be provided as a String.

Throws:
SQLException

isConnected

public boolean isConnected()
                    throws SQLException
Checks wether a connection is established or not.

Specified by:
isConnected in interface IDatabaseHandler
Returns:
true if connection exists.
Throws:
SQLException

connect

public void connect()
             throws SQLException,
                    ClassNotFoundException
Connects to the database specified in the constructor. Tables and prepared statements are created if teh initialize flag was set.

Specified by:
connect in interface IDatabaseHandler
Throws:
SQLException - is thrown, if any exception on the db level occurs.
ClassNotFoundException - is thrown, if the db driver could not be loaded.

isInitializing

protected boolean isInitializing()
Must return true, if the database tables should be created.

Returns:

setInitializing

protected void setInitializing(boolean init)
Sets the initinal attribute of the database. Required if keep-alive is disabled and multiple re-connects are expected.


getStatement

protected PreparedStatement getStatement(String id)

createAttributes

protected void createAttributes(PreparedStatement ps,
                                String uuid,
                                IAttributeMap m)
                         throws SQLException
Throws:
SQLException

deleteAttributes

protected void deleteAttributes(PreparedStatement ps,
                                String uuid)
                         throws SQLException
Throws:
SQLException

updateAttributes

protected void updateAttributes(PreparedStatement ps,
                                String uuid,
                                IAttributeMap m)
                         throws SQLException
Throws:
SQLException

commit

public void commit()
            throws SQLException
Commits the changed data to the database.

Specified by:
commit in interface IDatabaseHandler
Throws:
SQLException

rollback

public void rollback()
              throws SQLException
Rolls back the db changed since the last commit. Should be called if a SQLException was thrown anywhere.

Specified by:
rollback in interface IDatabaseHandler
Throws:
SQLException

disconnect

public void disconnect()
                throws SQLException
Disconnects the current db and shuts it down. The connection is set to null.

Specified by:
disconnect in interface IDatabaseHandler
Throws:
SQLException

setCommitCount

public void setCommitCount(int c)
Specified by:
setCommitCount in interface IDatabaseHandler

isKeepAlive

public boolean isKeepAlive()
Specified by:
isKeepAlive in interface IDatabaseHandler

setKeepAlive

public void setKeepAlive(boolean keep)
Specified by:
setKeepAlive in interface IDatabaseHandler


Copyright © 2010 by Thilo Brandt. All Rights Reserved.