ResultSetExtractor Example | Fetching Records by Spring JdbcTemplate
We can easily fetch the records from the database using query() method of JdbcTemplate class where we need to pass the instance of ResultSetExtractor.
Syntax of query method using ResultSetExtractor
ResultSetExtractor interface can be used to fetch records from the database. It accepts a ResultSet and returns the list.
Method of ResultSetExtractor interface
It defines only one method extractData that accepts ResultSet instance as a parameter. Syntax of the method is given below:
Example of ResultSetExtractor Interface to show all the records of the table
We are assuming that you have created the following table inside the Oracle10g database.
This class contains 3 properties with constructors and setter and getters. It defines one extra method toString().
It contains on property jdbcTemplate and one method getAllEmployees.
The DriverManagerDataSource is used to contain the information about the database such as driver class name, connnection URL, username and password.
There are a property named datasource in the JdbcTemplate class of DriverManagerDataSource type. So, we need to provide the reference of DriverManagerDataSource object in the JdbcTemplate class for the datasource property.
Here, we are using the JdbcTemplate object in the EmployeeDao class, so we are passing it by the setter method but you can use constructor also.
This class gets the bean from the applicationContext.xml file and calls the getAllEmployees() method of EmployeeDao class.