01: /*
02: * $Author$
03: * $Id$
04: * This is free software, as software should be; you can redistribute
05: * it and/or modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08:
09: * See LICENSE.txt for the full license covering this software/program/code.
10: */
11:
12: package util;
13:
14: import java.util.*;
15: import java.sql.*;
16:
17: /**
18: * Class documentation.
19: * @author rahul kumar <rahul_kumar@yahoo.com>
20: * @see XXX
21: */
22:
23: public class ResultSetUtil {
24:
25: /** return a list containing one Map for each row in the resultset,
26: * each Map contains column_name and value (as string).
27: */
28:
29: public static List getMappedDataString(ResultSet rs)
30: throws java.sql.SQLException {
31:
32: ResultSetMetaData rsmd = rs.getMetaData();
33: int numCols = rsmd.getColumnCount();
34:
35: String label, value;
36:
37: List /*<Map>*/l = new ArrayList();
38: int i;
39: Map /*<String>,<String> */map;
40: while (rs.next()) {
41: map = new LinkedHashMap(numCols);
42: for (i = 1; i <= numCols; i++) {
43: label = rsmd.getColumnLabel(i).toString();
44: value = rs.getObject(i).toString();
45: map.put(label, value);
46: }
47: l.add(map);
48: }
49: return l;
50: }
51:
52: /** return a list containing one Map for each row in the resultset,
53: * each Map contains column_name and value (as Object).
54: * But will you know what to do with Object or should I check the
55: * type and then place in. Hope instanceof helps.
56: */
57:
58: public static List getMappedData(ResultSet rs)
59: throws java.sql.SQLException {
60:
61: ResultSetMetaData rsmd = rs.getMetaData();
62: int numCols = rsmd.getColumnCount();
63:
64: String label;
65: Object value;
66:
67: List /*<Map>*/l = new ArrayList();
68: int i;
69: Map /*<String>,<Object> */map;
70: while (rs.next()) {
71: map = new LinkedHashMap(numCols);
72: for (i = 1; i <= numCols; i++) {
73: label = rsmd.getColumnLabel(i).toString();
74: value = rs.getObject(i);
75: map.put(label, value);
76: }
77: l.add(map);
78: }
79: return l;
80: }
81:
82: static final String P = "ResultSetUtil"; // used in exception strings
83:
84: } // end of class
|