How to use ResultSetExtractor
to retrieve data from database?
here I am using oracle 10g as back end. In case of searching an Employee details from Employee Table which one is better to use ResultSetExtractor
or RowMapper
?
Asked
Active
Viewed 2.2k times
7

Roman Cherepanov
- 1,639
- 2
- 24
- 44

Vineet Raj
- 85
- 1
- 1
- 7
2 Answers
0
One can also use closures (lambdas) as row mapping routine since java 8:
String sql = "select first_name, last_name from PERSON where id = ?";
public Person jdbcTemplate.query(sql,(rs)->{return new Person(rs.getString("first_name"), rs.getString("last_name"));}, int id);
First method param is your query, second - your mapper - Person(String, String) constructor is needed. "first_name", "last_name" are the db-column names. Third - the arg for the id, it's a vararg where you can put more params.

Picrochole
- 167
- 5
-1
A simpler example using ResultSetExtractor:
MyDataStructure result = jdbcTemplate.query(sql, new ResultSetExtractor<MyDataStructure>() {
@Override
public MyDataStructure extractData(final ResultSet rs) throws ... {
// collect data from rs here ...
return myData;
}
});

Matthias M
- 12,906
- 17
- 87
- 116
-
Was wrong with my question for voting me down? – Matthias M Feb 22 '22 at 08:20