Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

RowMapper


Like ResultSetExtractor, we can use RowMapper interface to fetch the records from the database using query() method of JdbcTemplate class. In the execute of we need to pass the instance of RowMapper now.

Syntax of query method using RowMapper

  1. public T query(String sql,RowMapper<T> rm)

RowMapper Interface

RowMapper interface allows to map a row of the relations with the instance of user-defined class. It iterates the ResultSet internally and adds it into the collection. So we don’t need to write a lot of code to fetch the records as ResultSetExtractor.

Advantage of RowMapper over ResultSetExtractor

RowMapper saves a lot of code becuase it internally adds the data of ResultSet into the collection.

Method of RowMapper interface

It defines only one method mapRow that accepts ResultSet instance and int as the parameter list. Syntax of the method is given below:

  1. public T mapRow(ResultSet rs, int rowNumber)throws SQLException

Example of RowMapper 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 number(10),
  3. name varchar2(100),
  4. salary number(10)
  5. );

Employee.java

This class contains 3 properties with constructors and setter and getters and one extra method toString().

  1. package com.Kreationnext;
  2. public class Employee {
  3. private int id;
  4. private String name;
  5. private float salary;
  6. //no-arg and parameterized constructors
  7. //getters and setters
  8. public String toString(){
  9.     return id+” “+name+” “+salary;
  10. }
  11. }

EmployeeDao.java

It contains on property jdbcTemplate and one method getAllEmployeesRowMapper.

  1. package com.Kreationnext;
  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. import org.springframework.jdbc.core.RowMapper;
  10. public class EmployeeDao {
  11. private JdbcTemplate template;
  12. public void setTemplate(JdbcTemplate template) {
  13.     this.template = template;
  14. }
  15. public List<Employee> getAllEmployeesRowMapper(){
  16.  return template.query(“select * from employee”,new RowMapper<Employee>(){
  17.     @Override
  18.     public Employee mapRow(ResultSet rs, int rownumber) throws SQLException {
  19.         Employee e=new Employee();
  20.         e.setId(rs.getInt(1));
  21.         e.setName(rs.getString(2));
  22.         e.setSalary(rs.getInt(3));
  23.         return e;
  24.     }
  25.     });
  26. }
  27. }

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. <bean id=“ds” class=“org.springframework.jdbc.datasource.DriverManagerDataSource”>
  9. <property name=“driverClassName” value=“oracle.jdbc.driver.OracleDriver” />
  10. <property name=“url” value=“jdbc:oracle:thin:@localhost:1521:xe” />
  11. <property name=“username” value=“system” />
  12. <property name=“password” value=“oracle” />
  13. </bean>
  14. <bean id=“jdbcTemplate” class=“org.springframework.jdbc.core.JdbcTemplate”>
  15. <property name=“dataSource” ref=“ds”></property>
  16. </bean>
  17. <bean id=“edao” class=“com.Kreationnext.EmployeeDao”>
  18. <property name=“jdbcTemplate” ref=“jdbcTemplate”></property>
  19. </bean>
  20. </beans>

Test.java

This class gets the bean from the applicationContext.xml file and calls the getAllEmployeesRowMapper() method of EmployeeDao class.

  1. package com.Kreationnext;
  2. import java.util.List;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class Test {
  6. public static void main(String[] args) {
  7.     ApplicationContext ctx=new ClassPathXmlApplicationContext(“applicationContext.xml”);
  8.     EmployeeDao dao=(EmployeeDao)ctx.getBean(“edao”);
  9.     List<Employee> list=dao.getAllEmployeesRowMapper();
  10.     for(Employee e:list)
  11.         System.out.println(e);
  12. }
  13. }

Comments

comments