This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Spring and the fly likes explanation on RowMapper's mapRow method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Spring
Bookmark "explanation on RowMapper Watch "explanation on RowMapper New topic
Author

explanation on RowMapper's mapRow method

Abdul Rahman
Ranch Hand

Joined: Jul 04, 2008
Posts: 63
Hi,

I'm unable to comprehend the method mapRow(ResultSet rs, int rownum) in the RowMapper interface. I tried to go through the API but i'm still not clear on this.

Can anyone please explain what this method does and what's it used for.

Thanks


Abdul
SCJP5, SCWCD5, [scwcd wall of fame]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I tried to post this yesterday


So JDBC queries return ResultSet objects. But your application has Domain objects like Order or Item. You need to convert the ResultSet into a Domain object. This normally tedious pain in the arse work, that is error proned, and in different queries might have duplicate code.

By using a RowMapper you can centralize that code in one class.

So when you run a Query that returns rows into a ResultSet and you pass in a RowMapper to the JdbcTemplate, then the template will do the work of looping through the resultset and passing it row by row to the mapRow method. The return of the mapRow will be an Object of the type that you convert the row to. In the mapRow method you write the code like

Order order = new Order();
order.setOrderDate(rs.getDate(0));
etc.

I forget the exact api method call on result set to get the field value of a date, but that is the general idea.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: explanation on RowMapper's mapRow method