CodesJava

Easy learning with example program codes

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.codesjava.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.codesjava.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.codesjava.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

Please follow and like us:

Spring Tutorial



Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki