org.stanwood.media.database
Class AbstractGenericDatabase

java.lang.Object
  extended by org.stanwood.media.database.AbstractGenericDatabase
All Implemented Interfaces:
IDatabase
Direct Known Subclasses:
HSQLDatabase, MysqlDatabase

public abstract class AbstractGenericDatabase
extends java.lang.Object
implements IDatabase

This class contains methods for database connections that are generic for all database implementations.


Constructor Summary
AbstractGenericDatabase()
           
 
Method Summary
 void closeConnection(java.sql.Connection connection)
          This is used to close a connection.
 void closeDatabaseResources(java.sql.Connection connection, java.sql.PreparedStatement stmt, java.sql.ResultSet rs)
          This is used to make sure that all DB resources are closed.
 void commitTransactionConnection(java.sql.Connection connection)
          This is used to commit a connection to the database
 java.sql.Connection createTransactionConnection()
          This is used to get a non-auto-committing connection to the database from a datasource
 boolean dropTable(java.lang.String tableName)
          This is called to delete a table from the database It creates it's own connection too the DB
 void executeSQL(java.lang.String sql)
          This is used to execute a simple SQL statement on the database.
 long executeUpdate(java.sql.Connection connection, java.lang.String sql, java.lang.Object[] params)
          This is used to execute an update statement that takes parameters.
 long executeUpdate(java.lang.String sql, java.lang.Object[] params)
          This is used to execute an update statement that takes parameters.
 java.sql.PreparedStatement getStatement(java.sql.Connection connection, java.lang.String sql, java.lang.Object[] params)
          This is used to create a PreparedStatement from the given SQL.
 long insertIntoTable(java.sql.Connection connection, java.lang.String tableName, java.util.List<Field> fields)
          This is used to insert table row into a table.
 long insertIntoTable(java.lang.String tableName, java.util.List<Field> fields)
          This is used to insert table row into a table.
 void rollbackTransactionConnection(java.sql.Connection connection)
          This is used to rollback all statements pushed through this connection since the last commit or save from the database
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.stanwood.media.database.IDatabase
createConnection, dropTable, executeSQL, getStatement, init
 

Constructor Detail

AbstractGenericDatabase

public AbstractGenericDatabase()
Method Detail

createTransactionConnection

public java.sql.Connection createTransactionConnection()
                                                throws java.sql.SQLException
This is used to get a non-auto-committing connection to the database from a datasource

Specified by:
createTransactionConnection in interface IDatabase
Returns:
a connection to the database
Throws:
java.sql.SQLException - Thrown if their is a problem getting the connection to the database
java.sql.SQLException - Thrown if their is a problem talking to the database

commitTransactionConnection

public void commitTransactionConnection(java.sql.Connection connection)
                                 throws java.sql.SQLException
This is used to commit a connection to the database

Specified by:
commitTransactionConnection in interface IDatabase
Parameters:
connection - This connection is committed
Throws:
java.sql.SQLException - Thrown if there is a problem getting the connection to the database

rollbackTransactionConnection

public void rollbackTransactionConnection(java.sql.Connection connection)
This is used to rollback all statements pushed through this connection since the last commit or save from the database

Specified by:
rollbackTransactionConnection in interface IDatabase
Parameters:
connection - This connection is rolled back

closeDatabaseResources

public void closeDatabaseResources(java.sql.Connection connection,
                                   java.sql.PreparedStatement stmt,
                                   java.sql.ResultSet rs)
This is used to make sure that all DB resources are closed. If any of the parameters are null, then they an attempt to close them is not made.

Specified by:
closeDatabaseResources in interface IDatabase
Parameters:
connection - the connection to close
stmt - the statement to close
rs - the result set to close

closeConnection

public void closeConnection(java.sql.Connection connection)
                     throws java.sql.SQLException
This is used to close a connection. This is done in the database interface so that connection closure can be logged from a central location and open connection count can be kept

Specified by:
closeConnection in interface IDatabase
Parameters:
connection - The connection to close
Throws:
java.sql.SQLException - Thrown if their is a problem talking to the database

dropTable

public boolean dropTable(java.lang.String tableName)
This is called to delete a table from the database It creates it's own connection too the DB

Specified by:
dropTable in interface IDatabase
Parameters:
tableName - The table to delete
Returns:
True if it was successful, otherwise false;

executeSQL

public void executeSQL(java.lang.String sql)
                throws java.sql.SQLException
This is used to execute a simple SQL statement on the database.

Specified by:
executeSQL in interface IDatabase
Parameters:
sql - the SQL to execute on the database
Throws:
java.sql.SQLException - Thrown if their is a problem talking to the database

getStatement

public java.sql.PreparedStatement getStatement(java.sql.Connection connection,
                                               java.lang.String sql,
                                               java.lang.Object[] params)
                                        throws java.sql.SQLException
This is used to create a PreparedStatement from the given SQL. The SQL should contain ? were the params should be inserted.

Specified by:
getStatement in interface IDatabase
Parameters:
connection - a connection to the database
sql - the statements SQL
params - the parameters to place into the statement
Returns:
a Prepared Statement
Throws:
java.sql.SQLException - thrown if their is a problem creating the statement
See Also:
PreparedStatement

executeUpdate

public long executeUpdate(java.lang.String sql,
                          java.lang.Object[] params)
                   throws java.sql.SQLException
This is used to execute an update statement that takes parameters. The SQL should contain ? were the parameters should be inserted.

Specified by:
executeUpdate in interface IDatabase
Parameters:
sql - the SQL to execute on the database
params - the parameters to insert into the SQL statement, replacing ?.
Returns:
if a key was generated, then it is returned, otherwise -1
Throws:
java.sql.SQLException - Thrown if their is a problem talking to the database

executeUpdate

public long executeUpdate(java.sql.Connection connection,
                          java.lang.String sql,
                          java.lang.Object[] params)
                   throws java.sql.SQLException
This is used to execute an update statement that takes parameters. The SQL should contain ? were the parameters should be inserted.

Specified by:
executeUpdate in interface IDatabase
Parameters:
connection - a connection to be re-used, useful for running a series of updates as a transaction
sql - The SQL to execute on the database
params - the parameters to insert into the SQL statement, replacing ?.
Returns:
if a key was generated, then it is returned, otherwise -1
Throws:
java.sql.SQLException - Thrown if their is a problem talking to the database

insertIntoTable

public long insertIntoTable(java.sql.Connection connection,
                            java.lang.String tableName,
                            java.util.List<Field> fields)
                     throws java.sql.SQLException
This is used to insert table row into a table. The table row is made up from fields.

Specified by:
insertIntoTable in interface IDatabase
Parameters:
connection - a connection to be re-used, useful for running a series
tableName - The name of the table
fields - The fields of the table that are to be inserted.
Returns:
If a key was generated, then it is pass here, otherwise -1
Throws:
java.sql.SQLException - Thrown if their is a problem talking to the database

insertIntoTable

public long insertIntoTable(java.lang.String tableName,
                            java.util.List<Field> fields)
                     throws java.sql.SQLException
This is used to insert table row into a table. The table row is made up from fields.

Specified by:
insertIntoTable in interface IDatabase
Parameters:
tableName - The name of the table
fields - The fields of the table that are to be inserted.
Returns:
If a key was generated, then it is pass here, otherwise -1
Throws:
java.sql.SQLException - Thrown if their is a problem talking to the database