org.ifeth.sehr.lib
Class DAOJdbc

java.lang.Object
  extended byorg.ifeth.sehr.lib.DAOJdbc
All Implemented Interfaces:
DAOJdbcInterface

public class DAOJdbc
extends java.lang.Object
implements DAOJdbcInterface

This is a data access object (DAO) for accessing the SEHR databases by standard JDBC/SQL without using he iBatis layer.

This DAO object implements some often used sql methods and seals them to a single call command. To use this layer first establish a FBDataSource object. After inittialization of this object you can define PreparedStatements or just plain SQL queries to be executed.
Note: This DAO can also use pooled connections for the datasource. To activate a pooled connection use setPooledConnections(boolean). Pooled connections are handy to get more control i.e. on RAS connections!

Version:
$Id: DAOJdbc.java,v 1.1 2005/01/16 12:15:56 hansjhaase Exp $
Author:
hansjhaase

Field Summary
 
Fields inherited from interface org.ifeth.sehr.lib.DAOJdbcInterface
CON_CANCLOSE, CON_CLOSEDENIED, CON_COMMITREQUIRED, STS_ACTIVE, STS_CLOSED, STS_NOTFOUND, STS_SQLERROR
 
Constructor Summary
DAOJdbc()
          Parameterless constructor.
DAOJdbc(java.sql.Connection con)
          Constructor for a single connection DAO object to use it with an already establish connction handle.
DAOJdbc(SEHRDataSource fbds)
          Constructor with given FBDataSource object.
 
Method Summary
 void addPreparedStatement(java.lang.String psname, java.lang.String sql)
          Add a named and init prepared statement for a single connection (id 0).
 void addPreparedStatement(java.lang.String psname, java.lang.String sql, int cidx)
          Add a named and init prepared statement for a given connection.
 void canClose(boolean allow)
          Allow or disallow closing of connection #0 (single connection).
 int checkConnection()
          Check connection.
 int checkConnection(int idx)
          Check connection.
 void close()
          Use this method to close the single connection.
 java.sql.Connection connect()
          This method established a single connection to the given datasource on instantiation of this object.
 int connectToPool()
          This method established a new connection using the pool.
 int countConnections()
          Get number of current handled connections
 boolean executePrepStmnt(java.sql.PreparedStatement stmnt)
          Execute a prepared statement as SQL query.
 boolean executeQuery(java.lang.String sql)
          Execute a plain SQL query for single connection.
 boolean executeQuery(java.lang.String sql, int conidx)
          Execute a plain SQL query using given connection.
 void forceClosePooledConnections()
          Use this method with care!
 void freeConnection(int cidx)
          Use this method to put a connection back to the pool
 java.sql.Connection getConnection()
          get connection handle for a single connection
 java.sql.Connection getConnection(int cidx)
          get connection handle for a given connection by 'index'
 java.sql.PreparedStatement getPreparedStatement(java.lang.String psname)
          Retrieve a named statement for single connection from hash to use it by executePrepStmnt(PreparedStatement)
 java.sql.PreparedStatement getPreparedStatement(java.lang.String psname, int idx)
          Retrieve a named statement for given connection by id to use it by executePrepStmnt(PreparedStatement)
 java.sql.ResultSet getResultSet()
          Get ResultSet of executed query.
static void main(java.lang.String[] args)
          For testing and study purposes...
 void setConnection(java.sql.Connection con)
          Set a connection handle (The fbds is not required then)
 void setPooledConnections(boolean b)
          Method to activate or deactivate pooled connections.
 void setSEHRDataSource(SEHRDataSource fbds)
          Set the FBDataSource handle for which a connection can be initialized.
 void setStatus(int cidx, int statuscode)
          Set status to handle connection.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DAOJdbc

public DAOJdbc()
Parameterless constructor.

Parameterless constructor. Set the SEHRDataSource by setSEHRDataSource(SEHRDataSource) and check the returncode if a connection is available.


DAOJdbc

public DAOJdbc(SEHRDataSource fbds)
Constructor with given FBDataSource object.


DAOJdbc

public DAOJdbc(java.sql.Connection con)
Constructor for a single connection DAO object to use it with an already establish connction handle.

Method Detail

connect

public java.sql.Connection connect()
                            throws java.sql.SQLException
This method established a single connection to the given datasource on instantiation of this object.

Throws:
java.sql.SQLException

connectToPool

public int connectToPool()
                  throws java.sql.SQLException
This method established a new connection using the pool.

Note: Use setPooledConnections(boolean)first to initiate a pooled connection.

Throws:
java.sql.SQLException

setPooledConnections

public void setPooledConnections(boolean b)
                          throws java.sql.SQLException
Method to activate or deactivate pooled connections.

To keep data integrity on open transactions there is a small workaraound to be shure we can deactive our pooled cons
Note: To force closing first use 'forceClosePooledConnections()' first (at your own risc!!) then call this method.

Throws:
java.sql.SQLException

forceClosePooledConnections

public void forceClosePooledConnections()
Use this method with care! Beware of running transactions


close

public void close()
           throws java.sql.SQLException
Use this method to close the single connection.

Use freeConnection(int)to close a pooled connection.

Throws:
java.sql.SQLException

freeConnection

public void freeConnection(int cidx)
                    throws java.sql.SQLException
Use this method to put a connection back to the pool

Throws:
java.sql.SQLException

canClose

public void canClose(boolean allow)
Allow or disallow closing of connection #0 (single connection).

This is a helper method to indicate if a connection is in use or not, e.g. for a period of time, and an application is allowed to close RAS connections.


addPreparedStatement

public void addPreparedStatement(java.lang.String psname,
                                 java.lang.String sql)
                          throws java.sql.SQLException
Add a named and init prepared statement for a single connection (id 0).

Throws:
java.sql.SQLException
See Also:
addPreparedStatement(String, String, int)

addPreparedStatement

public void addPreparedStatement(java.lang.String psname,
                                 java.lang.String sql,
                                 int cidx)
                          throws java.sql.SQLException
Add a named and init prepared statement for a given connection.

Use PreparedStatements for frequently used SQL queries. These statements are first prepared by the engine and then stored as objects. To assign parameters just extract the statement from hash by getPreparedStatement(String, int), assign the parameter and call method executePrepStmnt(PreparedStatement)

Throws:
java.sql.SQLException

getPreparedStatement

public java.sql.PreparedStatement getPreparedStatement(java.lang.String psname)
                                                throws java.lang.Exception
Retrieve a named statement for single connection from hash to use it by executePrepStmnt(PreparedStatement)

Throws:
java.lang.Exception

getPreparedStatement

public java.sql.PreparedStatement getPreparedStatement(java.lang.String psname,
                                                       int idx)
                                                throws java.lang.Exception
Retrieve a named statement for given connection by id to use it by executePrepStmnt(PreparedStatement)

Throws:
java.lang.Exception

executePrepStmnt

public boolean executePrepStmnt(java.sql.PreparedStatement stmnt)
                         throws java.lang.Exception
Execute a prepared statement as SQL query.

If a recordset is available the method returns 'true'. To access the resultset use getResultSet().

Throws:
java.lang.Exception

executeQuery

public boolean executeQuery(java.lang.String sql)
                     throws java.lang.Exception
Execute a plain SQL query for single connection.

Throws:
java.lang.Exception

executeQuery

public boolean executeQuery(java.lang.String sql,
                            int conidx)
                     throws java.lang.Exception
Execute a plain SQL query using given connection.

If a recordset is available the method returns 'true'. To access the resultset use getResultSet().

Throws:
java.lang.Exception

setStatus

public void setStatus(int cidx,
                      int statuscode)
               throws java.sql.SQLException
Set status to handle connection.

Statuscodes (use DAOJdbcInterface):

  0 = can close
  1 = don't close/not allowed
  2 = commit required (manupulated records)
      (if using autocommit this code has no effect)
 

Throws:
java.sql.SQLException

getResultSet

public java.sql.ResultSet getResultSet()
Get ResultSet of executed query.


setConnection

public void setConnection(java.sql.Connection con)
Set a connection handle (The fbds is not required then)


getConnection

public java.sql.Connection getConnection()
get connection handle for a single connection


getConnection

public java.sql.Connection getConnection(int cidx)
get connection handle for a given connection by 'index'


countConnections

public int countConnections()
Get number of current handled connections


checkConnection

public int checkConnection()
Check connection.

See Also:
checkConnection(int)

checkConnection

public int checkConnection(int idx)
Check connection.

Use this together with a timer/thread to check connection periodically.

Returns:
statuscode

Statuscodes:

     -1 = SQL Exception(broken?)
      0 = ok
      1 = closed
      2 = none found for id (already closed/pool out of sync?)
  
 


setSEHRDataSource

public void setSEHRDataSource(SEHRDataSource fbds)
                       throws java.lang.Exception
Set the FBDataSource handle for which a connection can be initialized.

Parameters:
fbds - FBDataSource object
Throws:
java.lang.Exception

main

public static void main(java.lang.String[] args)
For testing and study purposes...