W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/18/2017

ResultSetExtractor In 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

  1. public T query(String sql,ResultSetExtractor<T> rse)  

ResultSetExtractor Interface

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:
  1. public T extractData(ResultSet rs)throws SQLException,DataAccessException  

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.

  1. create table employee(  
  2. id int(10),  
  3. name varchar(100),  
  4. salary int(10)  
  5. );  
Employee.java
This class contains 3 properties with constructors and setter and getters. It defines one extra method toString().
  1. package com.javatpoint;  
  2.   
  3. public class Employee {  
  4. private int id;  
  5. private String name;  
  6. private float salary;  
  7. //no-arg and parameterized constructors  
  8. //getters and setters  
  9.   
  10. public String toString(){  
  11.     return id+" "+name+" "+salary;  
  12. }  
  13. }  
EmployeeDao.java
It contains on property jdbcTemplate and one method getAllEmployees.
  1. package com.javatpoint;  
  2. import java.sql.ResultSet;  
  3. import java.sql.SQLException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import org.springframework.dao.DataAccessException;  
  7. import org.springframework.jdbc.core.JdbcTemplate;  
  8. import org.springframework.jdbc.core.ResultSetExtractor;  
  9.   
  10. public class EmployeeDao {  
  11. private JdbcTemplate template;  
  12.   
  13. public void setTemplate(JdbcTemplate template) {  
  14.     this.template = template;  
  15. }  
  16.   
  17. public List<Employee> getAllEmployees(){  
  18.  return template.query("select * from employee",new ResultSetExtractor<List<Employee>>(){  
  19.     @Override  
  20.      public List<Employee> extractData(ResultSet rs) throws SQLException,  
  21.             DataAccessException {  
  22.       
  23.         List<Employee> list=new ArrayList<Employee>();  
  24.         while(rs.next()){  
  25.         Employee e=new Employee();  
  26.         e.setId(rs.getInt(1));  
  27.         e.setName(rs.getString(2));  
  28.         e.setSalary(rs.getInt(3));  
  29.         list.add(e);  
  30.         }  
  31.         return list;  
  32.         }  
  33.     });  
  34.   }  
  35. }  
applicationContext.xml

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.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  8.   
  9. <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  10. <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  11. <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true" />  
  12. <property name="username" value="root" />  
  13. <property name="password" value="root" />  
  14. </bean>  
  15.   
  16. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  17. <property name="dataSource" ref="ds"></property>  
  18. </bean>  
  19.   
  20. <bean id="edao" class="com.javatpoint.EmployeeDao">  
  21. <property name="jdbcTemplate" ref="jdbcTemplate"></property>  
  22. </bean>  
  23.   
  24. </beans>  
Test.java
This class gets the bean from the applicationContext.xml file and calls the getAllEmployees() method of EmployeeDao class.
  1. package com.javatpoint;   
  2. import java.util.List;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. public class Test {  
  6.   
  7. public static void main(String[] args) {  
  8.     ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");  
  9.     EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");  
  10.     List<Employee> list=dao.getAllEmployees();  
  11.           
  12.     for(Employee e:list)  
  13.         System.out.println(e);         
  14.     }  
  15. }  

No comments:

Post a Comment

Note: only a member of this blog may post a comment.