Class JdbcTemplate
java.lang.Object
org.wso2.carbon.database.utils.jdbc.JdbcTemplate
A utility class to call JDBC with lambda expressions. This class simplifies the usage of JDBC and helps to avoid
common errors.
Using this class we can execute insert, select, update and delete queries by providing necessary parameters to the
methods. Transaction support is also available with this implementation for the query executions. All the SQL
operations performed by this class are logged at the debug level.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> intexecuteBatchDelete(String query, QueryFilter queryFilter) Executes the jdbc batch delete query.<T> intexecuteBatchInsert(String query, QueryFilter queryFilter, T bean) Executes the jdbc insert/update query.int[]executeBatchUpdateWithAffectedRows(String query, QueryFilter queryFilter) Executes the jdbc batch update query and returns the results.<T> intexecuteInsert(String query, QueryFilter queryFilter, T bean, boolean fetchInsertedId) Executes the jdbc insert/update query.<T> intexecuteInsert(String query, QueryFilter queryFilter, T bean, boolean fetchInsertedId, String autoGenerateIdColumnName) Executes the jdbc insert/update query depends on provided auto generated column name.<T> List<T> executeQuery(String query, RowMapper<T> rowMapper) Executes a query on JDBC and return the result as a list of domain objects.<T> List<T> executeQuery(String query, RowMapper<T> rowMapper, QueryFilter queryFilter) Executes a query on JDBC and return the result as a list of domain objects.intexecuteUpdate(String query) Executes the jdbc update query and returns the result as updated id integer.voidexecuteUpdate(String query, QueryFilter queryFilter) Executes the jdbc update query and returns nothing.intexecuteUpdateWithAffectedRows(String query, QueryFilter queryFilter) Executes the jdbc update query and returns the number of affected rows.<T> TfetchSingleRecord(String query, RowMapper<T> rowMapper, QueryFilter queryFilter) Executes a query on JDBC and return the result as a domain object.Retrieve database product name from the data source.<T,E extends Exception>
TwithTransaction(ExecuteCallable<T> callable) Provides the transaction support for the JDBC executions of the template.
-
Constructor Details
-
JdbcTemplate
-
-
Method Details
-
withTransaction
public <T,E extends Exception> T withTransaction(ExecuteCallable<T> callable) throws TransactionException, E Provides the transaction support for the JDBC executions of the template.- Parameters:
callable- the SQL query execution method call.- Returns:
- result set of domain objects of required type.
- Throws:
TransactionExceptionE
-
executeQuery
Executes a query on JDBC and return the result as a list of domain objects.- Parameters:
query- the SQL query with the parameter placeholders.rowMapper- Row mapper functional interface.- Returns:
- List of domain objects of required type.
- Throws:
DataAccessException- See Also:
-
executeQuery
public <T> List<T> executeQuery(String query, RowMapper<T> rowMapper, QueryFilter queryFilter) throws DataAccessException Executes a query on JDBC and return the result as a list of domain objects.- Parameters:
query- the SQL query with the parameter placeholders.rowMapper- Row mapper functional interface.queryFilter- parameters for the SQL query parameter replacement.- Returns:
- List of domain objects of required type.
- Throws:
DataAccessException
-
fetchSingleRecord
public <T> T fetchSingleRecord(String query, RowMapper<T> rowMapper, QueryFilter queryFilter) throws DataAccessException Executes a query on JDBC and return the result as a domain object.- Parameters:
query- the SQL query with the parameter placeholders.rowMapper- Row mapper functional interface.queryFilter- parameters for the SQL query parameter replacement.- Returns:
- domain object of required type.
- Throws:
DataAccessException
-
executeUpdate
Executes the jdbc update query and returns nothing.- Parameters:
query- SQL query with the parameter placeholders.queryFilter- parameters for the SQL query parameter replacement.- Throws:
DataAccessException
-
executeUpdateWithAffectedRows
public int executeUpdateWithAffectedRows(String query, QueryFilter queryFilter) throws DataAccessException Executes the jdbc update query and returns the number of affected rows.- Parameters:
query- SQL query with the parameter placeholders.queryFilter- parameters for the SQL query parameter replacement.- Returns:
- number of affected rows.
- Throws:
DataAccessException
-
executeUpdate
Executes the jdbc update query and returns the result as updated id integer.- Parameters:
query- the SQL query with the parameter placeholders.- Returns:
- the updated id.
- Throws:
DataAccessException
-
executeInsert
public <T> int executeInsert(String query, QueryFilter queryFilter, T bean, boolean fetchInsertedId) throws DataAccessException Executes the jdbc insert/update query.- Type Parameters:
T- return type of the object.- Parameters:
query- The SQL for insert/update.queryFilter- Query filter to prepared statement parameter binding.bean- the Domain object to be inserted/updated.- Throws:
DataAccessException
-
executeInsert
public <T> int executeInsert(String query, QueryFilter queryFilter, T bean, boolean fetchInsertedId, String autoGenerateIdColumnName) throws DataAccessException Executes the jdbc insert/update query depends on provided auto generated column name.- Type Parameters:
T- Return type of the object.- Parameters:
query- The SQL for insert/update.queryFilter- Query filter to prepared statement parameter binding.bean- The Domain object to be inserted/updated.fetchInsertedId- Fetch inserted ID.autoGenerateIdColumnName- Name of auto generate ID column.- Returns:
- Throws:
DataAccessException
-
executeBatchInsert
public <T> int executeBatchInsert(String query, QueryFilter queryFilter, T bean) throws DataAccessException Executes the jdbc insert/update query.- Parameters:
query- The SQL for insert/update.queryFilter- Query filter to prepared statement parameter binding.bean- the Domain object to be inserted/updated.- Throws:
DataAccessException
-
executeBatchUpdateWithAffectedRows
public int[] executeBatchUpdateWithAffectedRows(String query, QueryFilter queryFilter) throws DataAccessException Executes the jdbc batch update query and returns the results.- Parameters:
query- The SQL for updatequeryFilter- Query filter to prepared statement parameter binding.- Returns:
- the results of the batch update.
- Throws:
DataAccessException
-
executeBatchDelete
Executes the jdbc batch delete query.- Parameters:
query- The SQL for deletequeryFilter- Query filter to prepared statement parameter binding.- Throws:
DataAccessException
-
getDriverName
- Returns:
- the driver name of the connection provided by the datasource.
- Throws:
DataAccessException
-
getDatabaseProductName
Retrieve database product name from the data source.- Returns:
- database product name from the data source.
- Throws:
DataAccessException- if an error occurs while retrieving metadata from data source.
-