What are RowMapper, ResultSetExtractor, bind variables and query types?

10,393

Q1: Theses interfaces are together with RowCallbackHandler frequently used by the JdbcTemplate when queering a database. Which interface you implement, how you implement it and which method you use in the JdbcTemplate depends on your database and what kind of query you would like to execute. From the Spring API doc and some additional comments:

RowMapper:

An interface used by JdbcTemplate for mapping rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of mapping each row to a result object

i.e. the RowMapper is commonly used to map objects when there is a one-to-one relationship between a row in the database and the resulting object.

ResultSetExtractor:

ResultSetExtractor object is typically stateless and thus reusable

Implementations of the ResultSetExtractor typically creates one object out of several rows, that is subsequently returned. It is stateless because the implementing class does not preserve any state between method calls.

RowCallbackHandler:

Implementations of this interface perform the actual work of processing each row [...] In contrast to a ResultSetExtractor, a RowCallbackHandler object is typically stateful: It keeps the result state within the object, to be available for later inspection.

The RowCallbackHandler is used for queries such as updating or deleting rows. Additionally, it is used when you need to track a state across the ResultSet, such as number of rows in the RowCountCallbackHandler.

Share:
10,393
user962206
Author by

user962206

Updated on June 04, 2022

Comments

  • user962206
    user962206 almost 2 years

    I know how to use JDBC Template and DAO, but I still have questions regarding it:

    1. What's the use of RowMapper and ResultSetExtractor?
    2. What is a bind variable?
    3. Are queries a type of List?