JdbcTemplate details part3

May 12, 2008 at 7:52 pm | Posted in Uncategorized | Leave a comment
Tags: ,

Error translation

Translate java.sql.SqlException to Spring’s data access exception hierarchy.

DataAccessException
Root cause for all dao access exceptions. unchecked exception.
direct parent class is NestedRuntimeException

Native jdbc resource

When you need to work on native jdbc connections instead of spring wrapped connections, providing NativeJdbcExtractor to jdbcTemplate allows you to retrieve native connections. Then, you can cast the connection to the vendor specific connection object.

SingleConnectionDataSource, DriverManagerDataSource

Datasource for testing.

DriverManagerDataSource
Create new connection every time.
useful for test outside of container, based on what API says.

SingleConnectionDataSource
Sub class of DriverManagerDataSource.
Always use the same connection, and not for multi threaded environement.

Transaction Management in JDBC

DatasourceUtils
Utility class to retrieve a connection from spring transaction managers(thread bounded connection).

TransactionAwareDataSourceProxy
Let your datasource participate spring transaction management. reference my other post.

DataSourceTransactionManager
Implementation of PlatformTransactionManager. So, use this as a transaction manager if application solely use datasource. Bind connection to current thread.

Advertisements

JdbcTemplate details part2

May 9, 2008 at 7:46 am | Posted in Uncategorized | Leave a comment
Tags: ,

Package Hierarchy

Batch update

JdbcTemplate#batchUpdate()
implement BatchPreparedStatementSetter or InterruptibleBatchPreparedStatement interfaces

For SimpleJdbcTemplate
you can also use value maps, bean property based objects, or SqlParameterSources
see SqlParameterSourceUtils

BatchSqlUpdate
represente batch update sql as a java class.

SimpleJdbc classes

simplified jdbc operation classes using database metadata(retrieved from JDBC driver).

SimpleJdbcTemplate
enhanced jdbcTemplate with java5 functions.

SimpleJdbcInsert
by giving table name(withTableName()), column names, and values, you can execute insert operation.

SimpleJdbcCall
simple execution for stored procedure. in/out parameters can be given as Map, or SqlParmaeterSource.

JDBC Operations

considering each DB operation as object

SqlQuery
abstract class that encapsulate all sql operations. This class defines a lot of execution methods. When you implement this class, need to provide rowMapper.

SqlUpdate
represent update operation as an object.
StoredProcedure: abstract class that you can extend for your stored procedure.

SqlFunction
expect single result

JdbcTemplate details part1

May 5, 2008 at 7:41 pm | Posted in Uncategorized | Leave a comment
Tags: ,

JdbcTemplate methods

Data Retrieval

queryForInt(), queryForLong()
expects unique value.

queryForMap()
expects one row. each column is a map key.

queryForList()
expects multiple rows. each entry of list is a map, and the map uses columns as keys.

queryForObject()
expects one row as an instance

query()
expects multiple rows. returns multiple objects. each row becomes one instance.

classes

* PreparedStatementCreator
create prepared statement and bind values

* PreparedStatementSetter
bind values to prepared statement when you want to separate creation of prepared statement binding of values.

* ResultSetExtractor
process entire resultset and return one object. recommended to access resultset stateless

* RowCallbackHandler
process entire resultset statefull. Object which implement this interface will usually used after jdbctemplate call.

* RowMapper
process each result row and construct an object.

Data Manipulation

update()
for insert,update, and delete statement

etc.

execute()
for DDL

JdbcTemplate extensions

* NamedParameterJdbcTemplate
Support named parameter (‘:id’,’:name’), use SqlParameterSource or Map to bind values to named parameters.

* SqlParameterSource
interface that represents keys, values, and sqlTypes for named parameters.

* SimpleJdbcTemplate
use Java5 functions such as vargs and generic.

Create a free website or blog at WordPress.com.
Entries and comments feeds.