KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > adapter > jdbc > util > ResultSetMapGenerator


1 /**
2  * Copyright (c) 2003 held jointly by the individual authors.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; with out even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * > http://www.gnu.org/copyleft/lesser.html
19  * > http://www.opensource.org/licenses/lgpl-license.php
20  */

21 package net.mlw.vlh.adapter.jdbc.util;
22
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.ResultSetMetaData JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * @author Matthew L. Wilson
35  * @version $Revision: 1.3 $ $Date: 2005/08/19 16:06:29 $
36  */

37 public class ResultSetMapGenerator
38 {
39
40    /**
41     * Logger for this class
42     */

43    private static final Log LOGGER = LogFactory.getLog(ResultSetMapGenerator.class);
44
45    private ResultSet JavaDoc result;
46
47    private String JavaDoc[] names;
48
49    public ResultSetMapGenerator(ResultSet JavaDoc result, boolean useName, boolean lowerCase) throws SQLException JavaDoc
50    {
51       this.result = result;
52       ResultSetMetaData JavaDoc metadata = result.getMetaData();
53
54       int columnCount = metadata.getColumnCount();
55       names = new String JavaDoc[columnCount];
56       for (int i = 0; i < columnCount; i++)
57       {
58          names[i] = (useName) ? metadata.getColumnName(i + 1) : metadata.getColumnLabel(i + 1);
59          if (names[i] == null || names[i].length() == 0)
60          {
61             names[i] = (useName) ? metadata.getColumnLabel(i + 1) : metadata.getColumnName(i + 1);
62          }
63
64          if (lowerCase)
65          {
66             names[i] = names[i].toLowerCase();
67          }
68       }
69       LOGGER.debug(names);
70    }
71
72    /**
73     * @see net.sf.cglib.beans.BeanGenerator#create()
74     */

75    public Map JavaDoc generateMap() throws SQLException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc, IllegalAccessException JavaDoc
76    {
77       Map JavaDoc map = new HashMap JavaDoc();
78       for (int i = 0; i < names.length; i++)
79       {
80          Object JavaDoc value = result.getObject(i + 1);
81          map.put(names[i], value);
82          if (LOGGER.isDebugEnabled())
83          {
84             LOGGER.debug(names[i] + " - " + value);
85          }
86       }
87
88       return map;
89    }
90
91    /**
92     * @return Returns the result.
93     */

94    public ResultSet JavaDoc getResultSet()
95    {
96       return result;
97    }
98
99    /**
100     * <p>Loads and returns the <code>Class</code> of the given name.
101     * By default, a load from the thread context class loader is attempted.
102     * If there is no such class loader, the class loader used to load this
103     * class will be utilized.</p>
104     *
105     * @exception SQLException if an exception was thrown trying to load
106     * the specified class
107     */

108    protected Class JavaDoc loadClass(String JavaDoc className) throws SQLException JavaDoc
109    {
110       try
111       {
112          ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
113          if (cl == null)
114          {
115             cl = this.getClass().getClassLoader();
116          }
117          return (cl.loadClass(className));
118       }
119       catch (Exception JavaDoc e)
120       {
121          throw new SQLException JavaDoc("Cannot load column class '" + className + "': " + e);
122       }
123    }
124
125    public static class Bean
126    {
127
128    }
129 }
Popular Tags