Hello all,
I'm wondering why DbUtils has no simple way to combine ResultSetHandlers.
In my code it is very common when I need to provide two methods: one retrieves List of items and another retrieve only one item.
Currently I use three methods in such case:
1) public Object get(int id) { ... } - retrieves one row and convert it into JavaBean with read method.
2) public List list() { ... } - retrieves many rows and convert them into List of JavaBeans with read method also.
3) private Object read(ResultSet rs) { ... } - converts current ResultSet row data into JavaBean object.
Now I'm moving to DbUtils library and want to use the same approach.
I plan:
1) implement custom ResultSetHandler to translate ResultSet row into JavaBean.
2) use ListHandler (sources bolow) to convert ResultSet into List with help from ResultSetHandler from point 1.
ListHandler ====================== package com.mikkri.tarot.db;
import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;
import org.apache.commons.dbutils.ResultSetHandler;
/**
* Class prepares List of objects generated by provided handler for each resultset row.
* @author Mikhail Krivoshein <[EMAIL PROTECTED]>
* @since 0.1
*/
public class ListHandler implements ResultSetHandler {
/** ResultSet row handler */
private final ResultSetHandler rowHandler;
/**
* Class constructor.
* @param rowHandler Handler called to generate object by resultset row.
* It is assumed that this handler won't scroll resultset.
*/
public ListHandler(ResultSetHandler rowHandler) {
this.rowHandler = rowHandler;
}
/**
* Method handles list.
*/
public Object handle(ResultSet rs) throws SQLException {
List result = new ArrayList();
while(rs.next()) {
Object o = rowHandler.handle(rs);
result.add(o);
}
return result;
}
} ======================
If I misunderstand something, please, give me advise how to achiev my goals in a better way.
Best regards, Mikhail Krivoshein
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
