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
- 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:
- 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.
- create table employee(
- id int(10),
- name varchar(100),
- salary int(10)
- );
Employee.java
This class contains 3 properties with constructors and setter and getters. It defines one extra method toString().
- package com.javatpoint;
-
- public class Employee {
- private int id;
- private String name;
- private float salary;
-
-
-
- public String toString(){
- return id+" "+name+" "+salary;
- }
- }
EmployeeDao.java
It contains on property jdbcTemplate and one method getAllEmployees.
- package com.javatpoint;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.dao.DataAccessException;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.ResultSetExtractor;
-
- public class EmployeeDao {
- private JdbcTemplate template;
-
- public void setTemplate(JdbcTemplate template) {
- this.template = template;
- }
-
- public List<Employee> getAllEmployees(){
- return template.query("select * from employee",new ResultSetExtractor<List<Employee>>(){
- @Override
- public List<Employee> extractData(ResultSet rs) throws SQLException,
- DataAccessException {
-
- List<Employee> list=new ArrayList<Employee>();
- while(rs.next()){
- Employee e=new Employee();
- e.setId(rs.getInt(1));
- e.setName(rs.getString(2));
- e.setSalary(rs.getInt(3));
- list.add(e);
- }
- return list;
- }
- });
- }
- }
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.
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http:
- http:
-
- <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- </bean>
-
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="ds"></property>
- </bean>
-
- <bean id="edao" class="com.javatpoint.EmployeeDao">
- <property name="jdbcTemplate" ref="jdbcTemplate"></property>
- </bean>
-
- </beans>
Test.java
This class gets the bean from the applicationContext.xml file and calls the getAllEmployees() method of EmployeeDao class.
- package com.javatpoint;
- import java.util.List;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Test {
-
- public static void main(String[] args) {
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
- EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");
- List<Employee> list=dao.getAllEmployees();
-
- for(Employee e:list)
- System.out.println(e);
- }
- }
No comments:
Post a Comment
Note: only a member of this blog may post a comment.