it.mokabyte.pattern.dao
Class TheDao

java.lang.Object
  |
  +--it.mokabyte.pattern.dao.TheDao

public class TheDao
extends java.lang.Object

This is an Helper class implementing the Data Access Object (DAO) pattern to simplify database access to the MOKA_ACCOUNT table. Table is :

 ACCOUNT
    ID       (VARCHAR(255))
    NAME     (VARCHAR(255))
    ADDRESS  (VARCHAR(255))
    BALANCE  (DOUBLEPRECISION)
 


Field Summary
private  it.mokabyte.pattern.dao.DataSource dataSource
          The datasource for this helper dao
 
Constructor Summary
TheDao(java.lang.String dataSourceName)
          Retrieve the datasource from JNDI using the given name.
 
Method Summary
 boolean accountExists(AccountPK key)
           
private  void closeResources(java.sql.Connection con, java.sql.PreparedStatement stmt, java.sql.ResultSet rslt)
          Close all Database-Resources passed in.
 int deleteAccount(AccountPK pk)
          Delete a row from the ACCOUNT table.
 java.util.Collection getAllAccounts()
          Retrieve all Accounts.
 int insertAccount(AccountPK pk, java.lang.String name, java.lang.String address, double balance)
          Insert a row into the MOKA_ACCOUNT table.
 AccountOM readAccount(AccountPK pk)
          Get a user from MOKA_ACCOUNT table.
 int updateAccount(AccountPK key, java.lang.String name, java.lang.String address, double balance)
          Update a row from the MOKA_ACCOUNT table.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

dataSource

private it.mokabyte.pattern.dao.DataSource dataSource
The datasource for this helper dao
Constructor Detail

TheDao

public TheDao(java.lang.String dataSourceName)
       throws javax.naming.NamingException
Retrieve the datasource from JNDI using the given name.
Parameters:
dataSourceName - the JNDI name of the DataSource to look up
Throws:
javax.naming.NamingException - thrown for errors with JNDI
Method Detail

insertAccount

public int insertAccount(AccountPK pk,
                         java.lang.String name,
                         java.lang.String address,
                         double balance)
                  throws java.sql.SQLException
Insert a row into the MOKA_ACCOUNT table. Use the sql: INSERT INTO MOKA_ACCOUNT (ID, NAME, ADDRESS, BALANCE) VALUES (id,name,address,balance)
Parameters:
id - for the account
name - the account user name
address - the account user address
balance - the initial balance for this account
Returns:
the number of rows inserted
Throws:
java.sql.SQLException - thrown by the Driver to indicate a database error

deleteAccount

public int deleteAccount(AccountPK pk)
                  throws java.sql.SQLException
Delete a row from the ACCOUNT table. Use the sql: DELETE FROM MOKA_ACCOUNT WHERE ID = 'ID'
Parameters:
the - primary key
Returns:
the number of rows deleted
Throws:
java.sql.SQLException - thrown by the Driver to indicate a database error

readAccount

public AccountOM readAccount(AccountPK pk)
                      throws java.sql.SQLException
Get a user from MOKA_ACCOUNT table. Use the sql: SELECT NAME,ADDRESS,BALANCE FROM MOKA_ACCOUNT WHERE ID = 'ID'
Parameters:
the - primary key
Returns:
the ObjectModel of the Account
Throws:
java.sql.SQLException - thrown by the Driver to indicate a database error

updateAccount

public int updateAccount(AccountPK key,
                         java.lang.String name,
                         java.lang.String address,
                         double balance)
                  throws java.sql.SQLException
Update a row from the MOKA_ACCOUNT table. Use the sql: UPDATE MOKA_ACCOUNT SET BALANCE,NAME,ADDRESS = ... WHERE ID =
Parameters:
key - the primary key
name - user name
address - user address
balance - user balance
Returns:
the number of rows updated
Throws:
java.sql.SQLException - thrown by the Driver to indicate a database error

accountExists

public boolean accountExists(AccountPK key)
                      throws java.sql.SQLException
Parameters:
key - the Primary Key for this account - it contains the custID and acctType
Returns:
Whether the indicated account exists
Throws:
java.sql.SQLException - thrown by the Driver to indicate a database error

getAllAccounts

public java.util.Collection getAllAccounts()
                                    throws java.sql.SQLException
Retrieve all Accounts. Use the sql: SELECT ID FROM MOKA_ACCOUNT
Returns:
a Collection of ID - all accounts in the DB
Throws:
java.sql.SQLException - thrown by the Driver to indicate a database error

closeResources

private void closeResources(java.sql.Connection con,
                            java.sql.PreparedStatement stmt,
                            java.sql.ResultSet rslt)
Close all Database-Resources passed in. null is an acceptable value for all arguments.
Parameters:
con - The Connection to close
stmt - The PreparedStatement to close
rslt - The ResultSet to close