org.stanwood.media.database
Class HSQLDatabase

java.lang.Object
  extended by org.stanwood.media.database.AbstractGenericDatabase
      extended by org.stanwood.media.database.HSQLDatabase
All Implemented Interfaces:
IDatabase

public class HSQLDatabase
extends AbstractGenericDatabase
implements IDatabase

This is a database manager class were the database is connected directly via TCP/IP to to a memory only database called HSQLDB. This is mainly used with JUnit tests.


Constructor Summary
HSQLDatabase(java.lang.String host, java.lang.String username, java.lang.String password, java.lang.String database)
          Used to create a MYSQL database controller class.
 
Method Summary
 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.
 java.sql.Connection createConnection()
          This is used to get a connection to the database to the memory only database
 void createTestDatabase()
          Used to create the test database.
 boolean dropTable(java.sql.Connection connection, java.lang.String tableName)
          This is called to delete a table from the database.
 void executeSQL(java.sql.Connection connection, java.lang.String sql)
          This is used to execute a simple SQL statement on the database.
 java.sql.PreparedStatement getStatement(java.sql.Connection connection, java.lang.String sql)
          This is used to create a PreparedStatement from the give SQL.
 void init()
          This is used to setup the database manager class, it should be called after creating a database manager class.
static java.lang.String translateSQL(java.lang.String sql)
          This will translate mysql sql into HSQLDB sql
 
Methods inherited from class org.stanwood.media.database.AbstractGenericDatabase
closeConnection, commitTransactionConnection, createTransactionConnection, dropTable, executeSQL, executeUpdate, executeUpdate, getStatement, insertIntoTable, insertIntoTable, rollbackTransactionConnection
 
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
closeConnection, commitTransactionConnection, createTransactionConnection, dropTable, executeSQL, executeUpdate, executeUpdate, getStatement, insertIntoTable, insertIntoTable, rollbackTransactionConnection
 

Constructor Detail

HSQLDatabase

public HSQLDatabase(java.lang.String host,
                    java.lang.String username,
                    java.lang.String password,
                    java.lang.String database)
Used to create a MYSQL database controller class.

Parameters:
host - The database host
username - The name of the user used to access the database
password - The name of the password used to access the database
database - The name of the database to connect to
Method Detail

init

public void init()
          throws UnableToConnectToDatabaseException
This is used to setup the database manager class, it should be called after creating a database manager class.

Specified by:
init in interface IDatabase
Throws:
UnableToConnectToDatabaseException

createConnection

public java.sql.Connection createConnection()
                                     throws java.sql.SQLException
This is used to get a connection to the database to the memory only database

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

createTestDatabase

public void createTestDatabase()
Used to create the test database.


dropTable

public boolean dropTable(java.sql.Connection connection,
                         java.lang.String tableName)
This is called to delete a table from the database. It uses the connection passed in.

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

getStatement

public java.sql.PreparedStatement getStatement(java.sql.Connection connection,
                                               java.lang.String sql)
                                        throws java.sql.SQLException
This is used to create a PreparedStatement from the give SQL. This is useful as different implementations of the interface can translate the SQL so that it is under stood by the database. The SQL should be in the format of MySQL SQL. Because this is not a MySQL database, This will attempt to translate the SQL into something that can be understood by HSQLDB.

Specified by:
getStatement in interface IDatabase
Parameters:
connection - A connection to the database
sql - The statements sql
Returns:
A Prepared Statement
Throws:
java.sql.SQLException - Thrown if their is a problem creating the statement
See Also:
PreparedStatement

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
Overrides:
closeDatabaseResources in class AbstractGenericDatabase
Parameters:
connection - The connection to close
stmt - The statement to close
rs - The result set to close

translateSQL

public static java.lang.String translateSQL(java.lang.String sql)
This will translate mysql sql into HSQLDB sql

Parameters:
sql - The mysql sql
Returns:
the HSQLDB sql

executeSQL

public void executeSQL(java.sql.Connection connection,
                       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:
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
Throws:
java.sql.SQLException - Thrown if their is a problem talking to the database