Spring resultsetextractor tutorial

ResultSetExtractor interface:
ResultSetExtractor is a callback interface used by JdbcTemplate’s query methods to process the ResultSet. It extract or fetch results from a ResultSet. However RowMapper is a better choice for processing the ResultSet.

Method of ResultSetExtractor interface:

public T extractData(ResultSet rs)throws SQLException,DataAccessException

Where T represents the return type.

Example

package com.w3schools.SpringDB;
 
import java.util.List;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
 
@SpringBootApplication
public class SpringDbApplication {
	private static ApplicationContext applicationContext;
    public static void main( String[] args ) {
    	applicationContext = SpringApplication.run(SpringDbApplication.class, args);
    	performDBOperation();
    }
 
    private static void performDBOperation(){
    	JDBCController jdbcController = (JDBCController) applicationContext.getBean("jDBCController");
    	List<users> userList = jdbcController.getUserList();
    	for(Users user : userList)
            System.out.println(user);
    }
}
package com.w3schools.SpringDB;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;
 
@Repository("jDBCController")
public class JDBCController {
	@Autowired
        JdbcTemplate jdbc;
 
	public List<users> getUserList() {
		return jdbc.query("select * from users",new ResultSetExtractor<list<users>>(){
		 @Override
	     public List<users> extractData(ResultSet rs) throws SQLException,
	            DataAccessException {
 
	        List<users> list=new ArrayList<users>();
	        while(rs.next()){
	        	Users e=new Users();
	        	e.setSeq(rs.getInt(1));
	        	e.setUsername(rs.getString(2));
	        	e.setPassword(rs.getString(3));
	        	list.add(e);
	        }
	        return list;
	        }
	    });
	}
}
package com.w3schools.SpringDB;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name= "USERS")
public class Users {
	private int seq;
	private String username;
	private String password;
 
	@Id
	@GeneratedValue
	@Column(name = "SEQ", unique = true, nullable = false)
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		this.seq = seq;
	}
 
	@Column(name = "USER_NAME")
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
 
	@Column(name = "PASSWORD")
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
 
        public String toString(){
	    return seq+" "+username+" "+password;
	}
}
spring.datasource.url=jdbc:mysql://localhost:3306/test_schema?useSSL=false
spring.datasource.username=root
spring.datasource.password=*******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Output

1 Jai jai@123
2 Vivek vivek@123
12 hkumar h123
15 Vishal v123

Related topics