01: package org.claros.commons.db.handler;
02:
03: import java.sql.ResultSet;
04: import java.sql.ResultSetMetaData;
05: import java.sql.SQLException;
06: import java.util.ArrayList;
07: import java.util.HashMap;
08: import java.util.List;
09:
10: import org.apache.commons.dbutils.ResultSetHandler;
11:
12: /**
13: * @author Umut Gokbayrak
14: *
15: */
16: public class ListResultSetHandler implements ResultSetHandler {
17: /* (non-Javadoc)
18: * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
19: */
20: public Object handle(ResultSet rs) throws SQLException {
21: List out = new ArrayList();
22:
23: String name = null;
24: Object value = null;
25: int cols = -1;
26: ResultSetMetaData meta = null;
27: HashMap map = null;
28: boolean hasItem = false;
29: while (rs.next()) {
30: hasItem = true;
31: meta = rs.getMetaData();
32: cols = meta.getColumnCount();
33: map = new HashMap(cols);
34:
35: for (int i = 0; i < cols; i++) {
36: name = meta.getColumnName(i + 1);
37: value = rs.getObject(i + 1);
38: map.put(name, value);
39: }
40: out.add(map);
41: }
42: if (!hasItem) {
43: return null;
44: }
45: return out;
46: }
47: }
|