Class DatabaseConfiguration.AbstractJdbcOperation<T>
- java.lang.Object
-
- org.apache.commons.configuration2.DatabaseConfiguration.AbstractJdbcOperation<T>
-
- Type Parameters:
T
- the type of the results produced by a JDBC operation
- Enclosing class:
- DatabaseConfiguration
private abstract class DatabaseConfiguration.AbstractJdbcOperation<T> extends java.lang.Object
An internally used helper class for simplifying database access through plain JDBC. This class provides a simple framework for creating and executing a JDBC statement. It especially takes care of proper handling of JDBC resources even in case of an error.
-
-
Field Summary
Fields Modifier and Type Field Description private java.sql.Connection
connection
Stores the connection.private EventType<? extends ConfigurationErrorEvent>
errorEventType
The type of the event to send in case of an error.private java.lang.String
errorPropertyName
The property configurationName for an error event.private java.lang.Object
errorPropertyValue
The property value for an error event.private EventType<?>
operationEventType
The type of the operation which caused an error.private java.sql.PreparedStatement
preparedStatement
Stores the statement.private java.sql.ResultSet
resultSet
Stores the result set.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractJdbcOperation(EventType<? extends ConfigurationErrorEvent> errEvType, EventType<?> opType, java.lang.String errPropName, java.lang.Object errPropVal)
Creates a new instance ofJdbcOperation
and initializes the properties related to the error event.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected java.sql.PreparedStatement
createStatement(java.lang.String sql, boolean nameCol)
Creates aPreparedStatement
object for executing the specified SQL statement.T
execute()
Executes this operation.protected java.sql.Connection
getConnection()
Gets the current connection.protected java.sql.PreparedStatement
initStatement(java.lang.String sql, boolean nameCol, java.lang.Object... params)
Creates an initializes aPreparedStatement
object for executing an SQL statement.protected java.sql.ResultSet
openResultSet(java.lang.String sql, boolean nameCol, java.lang.Object... params)
Creates aPreparedStatement
for a query, initializes it and executes it.protected abstract T
performOperation()
Performs the JDBC operation.
-
-
-
Field Detail
-
connection
private java.sql.Connection connection
Stores the connection.
-
preparedStatement
private java.sql.PreparedStatement preparedStatement
Stores the statement.
-
resultSet
private java.sql.ResultSet resultSet
Stores the result set.
-
errorEventType
private final EventType<? extends ConfigurationErrorEvent> errorEventType
The type of the event to send in case of an error.
-
operationEventType
private final EventType<?> operationEventType
The type of the operation which caused an error.
-
errorPropertyName
private final java.lang.String errorPropertyName
The property configurationName for an error event.
-
errorPropertyValue
private final java.lang.Object errorPropertyValue
The property value for an error event.
-
-
Constructor Detail
-
AbstractJdbcOperation
protected AbstractJdbcOperation(EventType<? extends ConfigurationErrorEvent> errEvType, EventType<?> opType, java.lang.String errPropName, java.lang.Object errPropVal)
Creates a new instance ofJdbcOperation
and initializes the properties related to the error event.- Parameters:
errEvType
- the type of the error eventopType
- the operation event typeerrPropName
- the property configurationName for the error eventerrPropVal
- the property value for the error event
-
-
Method Detail
-
createStatement
protected java.sql.PreparedStatement createStatement(java.lang.String sql, boolean nameCol) throws java.sql.SQLException
Creates aPreparedStatement
object for executing the specified SQL statement.- Parameters:
sql
- the statement to be executednameCol
- a flag whether the configurationName column should be taken into account- Returns:
- the prepared statement object
- Throws:
java.sql.SQLException
- if an SQL error occurs
-
execute
public T execute()
Executes this operation. This method obtains a database connection and then delegates toperformOperation()
. Afterwards it performs the necessary clean up. Exceptions that are thrown during the JDBC operation are caught and transformed into configuration error events.- Returns:
- the result of the operation
-
getConnection
protected java.sql.Connection getConnection()
Gets the current connection. This method can be called whileexecute()
is running. It returns null otherwise.- Returns:
- the current connection
-
initStatement
protected java.sql.PreparedStatement initStatement(java.lang.String sql, boolean nameCol, java.lang.Object... params) throws java.sql.SQLException
Creates an initializes aPreparedStatement
object for executing an SQL statement. This method first callscreateStatement()
for creating the statement and then initializes the statement's parameters.- Parameters:
sql
- the statement to be executednameCol
- a flag whether the configurationName column should be taken into accountparams
- the parameters for the statement- Returns:
- the initialized statement object
- Throws:
java.sql.SQLException
- if an SQL error occurs
-
openResultSet
protected java.sql.ResultSet openResultSet(java.lang.String sql, boolean nameCol, java.lang.Object... params) throws java.sql.SQLException
Creates aPreparedStatement
for a query, initializes it and executes it. The resultingResultSet
is returned.- Parameters:
sql
- the statement to be executednameCol
- a flag whether the configurationName column should be taken into accountparams
- the parameters for the statement- Returns:
- the
ResultSet
produced by the query - Throws:
java.sql.SQLException
- if an SQL error occurs
-
performOperation
protected abstract T performOperation() throws java.sql.SQLException
Performs the JDBC operation. This method is called byexecute()
after this object has been fully initialized. Here the actual JDBC logic has to be placed.- Returns:
- the result of the operation
- Throws:
java.sql.SQLException
- if an SQL error occurs
-
-