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.

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.

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

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

Transaction Management in JDBC

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

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

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


JdbcTemplate details part2

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

Package Hierarchy

Batch update

implement BatchPreparedStatementSetter or InterruptibleBatchPreparedStatement interfaces

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

represente batch update sql as a java class.

SimpleJdbc classes

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

enhanced jdbcTemplate with java5 functions.

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

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

JDBC Operations

considering each DB operation as object

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

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

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.

expects one row. each column is a map key.

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

expects one row as an instance

expects multiple rows. returns multiple objects. each row becomes one instance.


* 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

for insert,update, and delete statement


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.

Blog at WordPress.com.
Entries and comments feeds.