|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.jfw.database.StoredProcedureGateway
public abstract class StoredProcedureGateway
Used for execute stored procedures.
Example of subclass
public class MySP extends StoredProcedureGateway
{
//implement one of the constructors present in StoredProcedureGateway
public MySP(HashMap inputParams)
{
super(inputParams);
}
//set stored procedure to use
protected void setStoredProcedure()
{
setStoredProcedure("{call package.test(?)}");
}
//add the input parameters here
protected void setInput() throws DatabaseException
{
try
{
CallableStatement callable = getCallableStatement();
callable.setInt(1, Integer.valueOf((String)inputParams.get("id")).intValue());
}
catch (Throwable t)
{
JFWLogger.error(className, "setInput", "", "JFW", t.toString(), t);
throw new DatabaseException(t.getMessage());
}
}
//add the output parameters here
protected void setOutput() throws DatabaseException
{
try
{
CallableStatement callable = getCallableStatement();
callable.registerOutParameter(2, java.sql.Types.INTEGER);
}
catch (Throwable t)
{
JFWLogger.error(className, "setOutput", "", "JFW", t.toString(), t);
throw new DatabaseException(t.getMessage());
}
}
//execute stored procedure
public void myExecute(HashMap outputData) throws DatabaseException
{
boolean commit = false;
try
{
execute(outputData, false);
commit = true;
return outputData;
}
catch(DatabaseException dbe)
{
throw dbe;
}
catch(Throwable t)
{
JFWLogger.error(className, "myExecute", "", "JFW", t.toString(), t);
throw new DatabaseException(t.getMessage());
}
finally
{
close(commit);
}
}
}
An exampe of the caller class
HashMap outputData = new HashMap();
HashMap inputParams = new HashMap();
//... add input params if needed
MySP mySP = new MySP(inputParams);
mySP.myExecute(outputData);
Field Summary | |
---|---|
protected java.lang.String |
className
Used for logging. |
protected java.util.HashMap |
inputParams
Contiene i parametri di input. |
protected Profiler |
profiler
Utilizzato per il profiling. |
static java.lang.String |
RESULT_KEY
The result of stored procedure execution. |
Constructor Summary | |
---|---|
StoredProcedureGateway(java.util.HashMap inputParams)
Use the default db resource from the jfw.properties. |
|
StoredProcedureGateway(java.util.HashMap inputParams,
java.sql.Connection connection)
Use the connection object gived. |
|
StoredProcedureGateway(java.util.HashMap inputParams,
java.lang.String resourceName)
Use the parameter resourceName as db resource. |
|
StoredProcedureGateway(java.util.HashMap inputParams,
Transaction transaction)
Use the Transaction for get the Connection to use. |
Method Summary | |
---|---|
protected void |
close()
Close the result set, the callable statement. |
protected void |
close(boolean doCommit)
Call method commit() or rollback() and method close() . |
protected boolean |
commit()
Commit the stored procedure execution. |
protected java.lang.Object |
exec(boolean isQuery)
Execute the stored procedure. |
protected void |
execute(java.util.HashMap outputdata)
Call method execute(java.util.HashMap, boolean) . |
protected void |
execute(java.util.HashMap outputdata,
boolean isQuery)
Call method exec(boolean) . |
void |
finalize()
Call super.finalize() and method close() . |
protected void |
getAllData(java.sql.ResultSet resultSet,
java.util.HashMap outputData)
Call the other method getAllData(java.sql.ResultSet, java.util.HashMap, boolean) with default value false
for parameter escape |
protected void |
getAllData(java.sql.ResultSet resultSet,
java.util.HashMap outputData,
boolean escape)
Read the result set returned by the stpred procedure and save the data in a HashMap. |
protected java.sql.CallableStatement |
getCallableStatement()
Return attribute callableStatement . |
protected java.sql.Connection |
getConnection()
Return attribute connection . |
protected java.lang.String |
getInputParam(java.lang.String inputParam)
Call other method getInputParam(java.lang.String, java.lang.Object) with default value null
for parameter defaultValue |
protected java.lang.String |
getInputParam(java.lang.String inputParam,
java.lang.Object defaultValue)
Return the toString() of the object associate with key inputParam from inputParams . |
protected java.lang.String |
getStoredProcedure()
Return attribute storedProcedure . |
protected void |
initialize()
Call methods to initialize resources if init is false. |
protected boolean |
isClosed()
Return attribute closed . |
abstract void |
myExecute(java.util.HashMap outputdata)
The method called by the other classes for execute the stored procedure. |
protected boolean |
rollback()
Rollback the stored procedure execution. |
protected void |
setInput()
Default implementation if no input parameters must be set. |
protected void |
setOutput()
Default implementation if no output parameters must be set. |
protected abstract void |
setStoredProcedure()
Set attribute storedProcedure . |
protected void |
setStoredProcedure(java.lang.String storedProcedure)
Set attribute storedProcedure . |
java.lang.String |
toString()
Return data relative of stored procedure, input parameters and database resource. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected final java.lang.String className
public static final java.lang.String RESULT_KEY
execute(java.util.HashMap, boolean)
. The associated value
is a Boolean object.
protected Profiler profiler
protected java.util.HashMap inputParams
Constructor Detail |
---|
public StoredProcedureGateway(java.util.HashMap inputParams)
inputParams
- the input parameters used by the stored procedure.public StoredProcedureGateway(java.util.HashMap inputParams, java.lang.String resourceName)
inputParams
- the input parameters used by the stored procedure.resourceName
- the name of a resource as specified in the jfw.properties.public StoredProcedureGateway(java.util.HashMap inputParams, Transaction transaction)
Transaction
for get the Connection to use.
inputParams
- the input parameters used by the stored procedure.transaction
- a Transaction
object.public StoredProcedureGateway(java.util.HashMap inputParams, java.sql.Connection connection)
inputParams
- the input parameters used by the stored procedure.connection
- the Connection object to use.Method Detail |
---|
protected void initialize() throws DatabaseException
init
is false.
setStoredProcedure(java.lang.String)
setConnection()
, if connection
is null
callableStatement
setInput()
setOutput()
init
value at true
DatabaseException
protected final java.sql.CallableStatement getCallableStatement()
callableStatement
.
protected java.sql.Connection getConnection() throws DatabaseException
connection
.
DatabaseException
protected boolean commit()
protected boolean rollback()
protected void close()
isTransaction
is false then rollback and release
the connection, if not nothing is done on the connection.
Set value of attribute isClosed()
at true.
protected void close(boolean doCommit)
commit()
or rollback()
and method close()
.
doCommit
- if true call method commit()
, else
call method rollback()
.protected final boolean isClosed()
closed
.
closed
.protected java.lang.Object exec(boolean isQuery) throws DatabaseException
isQuery
- true if the stored procedure return a result set,
false in other cases.
DatabaseException
protected void execute(java.util.HashMap outputdata, boolean isQuery) throws DatabaseException
exec(boolean)
. If a ResultSet object is returned by exec(boolean)
then the method getAllData(java.sql.ResultSet, java.util.HashMap, boolean)
is called and is put in outputdata,
at key RESULT_KEY
, a Boolean object with value true.
If a Boolean object is returned then the object is putted in the
outputdata with key RESULT_KEY
.
outputdata
- contains the data returned by the stored procedure and
other informations relative the database interrogation.isQuery
- true if the stored procedure return a result set,
false in other cases.
DatabaseException
protected void execute(java.util.HashMap outputdata) throws DatabaseException
execute(java.util.HashMap, boolean)
.
The default value for parameter isQuery is true.
DatabaseException
protected void getAllData(java.sql.ResultSet resultSet, java.util.HashMap outputData, boolean escape) throws DatabaseException
ReadDbData.result2Hash(java.sql.ResultSet, java.util.HashMap, java.lang.String, boolean, java.lang.String, int, int)
for read
and save data.
resultSet
- the result set to read.escape
- the escape param for method ReadDbData.result2Hash(java.sql.ResultSet, java.util.HashMap, java.lang.String, boolean, java.lang.String, int, int)
.
DatabaseException
protected void getAllData(java.sql.ResultSet resultSet, java.util.HashMap outputData) throws DatabaseException
getAllData(java.sql.ResultSet, java.util.HashMap, boolean)
with default value false
for parameter escape
DatabaseException
protected java.lang.String getInputParam(java.lang.String inputParam, java.lang.Object defaultValue)
inputParams
.
inputParam
- the key to read.
protected java.lang.String getInputParam(java.lang.String inputParam)
getInputParam(java.lang.String, java.lang.Object)
with default value null
for parameter defaultValue
protected void setInput() throws DatabaseException
DatabaseException
protected void setOutput() throws DatabaseException
DatabaseException
protected java.lang.String getStoredProcedure()
storedProcedure
.
storedProcedure
.protected void setStoredProcedure(java.lang.String storedProcedure)
storedProcedure
.
storedProcedure
- the stored procedure, example: package.test(?).public java.lang.String toString()
toString
in class java.lang.Object
public void finalize() throws java.lang.Throwable
close()
.
finalize
in class java.lang.Object
java.lang.Throwable
protected abstract void setStoredProcedure()
storedProcedure
. Must be implement by subclass.
public abstract void myExecute(java.util.HashMap outputdata) throws java.lang.Exception
execute(java.util.HashMap, boolean)
or exec(boolean)
must be called for
execute the stored procedure and get the data.
outputdata
- contains the data returned by the stored procedure and
other informations relative the database interrogation.
java.lang.Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |