Source Code Cross Referenced for ResultSetUtils.java in  » Web-Framework » jWebApp » jpersist » utils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » jWebApp » jpersist.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.  
003:         * All Rights Reserved.
004:         *
005:         * This file is part of JPersist.
006:         *
007:         * JPersist is free software; you can redistribute it and/or modify it under 
008:         * the terms of the GNU General Public License (Version 2) as published by 
009:         * the Free Software Foundation.
010:         *
011:         * JPersist is distributed in the hope that it will be useful, but WITHOUT 
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
013:         * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
014:         * for more details.
015:         *
016:         * You should have received a copy of the GNU General Public License 
017:         * along with JPersist; if not, write to the Free Software Foundation, 
018:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019:         */package jpersist.utils;
020:
021:        import java.sql.ResultSet;
022:        import java.sql.ResultSetMetaData;
023:        import java.sql.SQLException;
024:        import java.util.Collection;
025:        import java.util.Map;
026:        import jpersist.*;
027:
028:        /**
029:         * This class provides several static utility methods for retrieving data from a result set.
030:         */
031:
032:        @SuppressWarnings("unchecked")
033:        // working to complete a Java 1.5 version
034:        public class ResultSetUtils {
035:            /**
036:             * Returns an array of strings representing the column names in the result set.
037:             *
038:             * @param resultSet the result set
039:             *
040:             * @return an array of strings
041:             */
042:
043:            public static String[] getColumnNames(ResultSet resultSet)
044:                    throws JPersistException, SQLException {
045:                String names[] = null;
046:
047:                if (resultSet != null) {
048:                    names = new String[resultSet.getMetaData().getColumnCount()];
049:
050:                    for (int i = 0; i < names.length; i++)
051:                        names[i] = resultSet.getMetaData().getColumnName(i + 1);
052:                }
053:
054:                return names;
055:            }
056:
057:            /**
058:             * Returns a map of column name and value pairs from the current row of the result set.
059:             * 
060:             * @param resultSet a valid result set
061:             * @param map the map to load with column name and value pairs
062:             *
063:             * @return the map that was passed in
064:             */
065:
066:            public static Map getRowDataMap(ResultSet resultSet, Map map)
067:                    throws JPersistException, SQLException {
068:                if (resultSet != null)
069:                    for (int i = 0; i < resultSet.getMetaData()
070:                            .getColumnCount(); i++)
071:                        map.put(resultSet.getMetaData().getColumnName(i + 1),
072:                                resultSet.getObject(i + 1));
073:
074:                return map;
075:            }
076:
077:            /**
078:             * Returns a collection of the current rows data.
079:             *
080:             * @param resultSet a valid result set
081:             * @param c the collection to load with the current rows data
082:             *
083:             * @return the collection that was passed in
084:             */
085:
086:            public static Collection getRowDataCollection(ResultSet resultSet,
087:                    Collection c) throws JPersistException, SQLException {
088:                if (resultSet != null)
089:                    for (int i = 0; i < resultSet.getMetaData()
090:                            .getColumnCount(); i++)
091:                        c.add(resultSet.getObject(i + 1));
092:
093:                return c;
094:            }
095:
096:            /**
097:             * Returns an array of objects obtained from the current rows data.
098:             *
099:             * @param resultSet a valid result set
100:             *
101:             * @return an array of objects
102:             */
103:
104:            public static Object[] getRowDataObjects(ResultSet resultSet)
105:                    throws JPersistException, SQLException {
106:                Object objects[] = null;
107:
108:                if (resultSet != null) {
109:                    objects = new Object[resultSet.getMetaData()
110:                            .getColumnCount()];
111:
112:                    for (int i = 0; i < objects.length; i++)
113:                        objects[i] = resultSet.getObject(i + 1);
114:                }
115:
116:                return objects;
117:            }
118:
119:            /**
120:             * Loads a map with key and value pairs from the first and second column of each row in the result set.
121:             *
122:             * @param resultSet a valid result set
123:             * @param map the map to load with key and value pairs
124:             *
125:             * @return the map that was passed in
126:             */
127:
128:            public static Map loadMap(ResultSet resultSet, Map map)
129:                    throws JPersistException, SQLException {
130:                return loadMap(resultSet, map, 1, 2);
131:            }
132:
133:            /**
134:             * Loads a map with key and value pairs from keyColumn and valueColumn of each row in the result set.
135:             *
136:             * @param resultSet a valid result set
137:             * @param map the map to load with keyColumn and valueColumn
138:             * @param keyColumn the key column
139:             * @param valueColumn the value column
140:             *
141:             * @return the map that was passed in
142:             */
143:
144:            public static Map loadMap(ResultSet resultSet, Map map,
145:                    int keyColumn, int valueColumn) throws JPersistException,
146:                    SQLException {
147:                if (resultSet != null)
148:                    while (resultSet.next())
149:                        map.put(resultSet.getObject(keyColumn), resultSet
150:                                .getObject(valueColumn));
151:
152:                return map;
153:            }
154:
155:            /**
156:             * Loads a collection with row data from the result set.  If the rows have more than one value, 
157:             * then a collection of collections (of the same type) is created, otherwise a collection of an 
158:             * object is created.
159:             *
160:             * @param resultSet a valid result set
161:             * @param c the collection to load with the result set data
162:             *
163:             * @return the collection that was passed in
164:             */
165:
166:            public static Collection loadCollection(ResultSet resultSet,
167:                    Collection c) throws JPersistException, SQLException,
168:                    InstantiationException, IllegalAccessException {
169:                return loadCollection(resultSet, c, true);
170:            }
171:
172:            /**
173:             * Loads a collection with row data from the result set.  If the rows have more than one value, 
174:             * then a collection of collections (of the same type) is created, otherwise depending on the 
175:             * value of singleObject, a collection of a given object is created.
176:             *
177:             * @param resultSet a valid result set
178:             * @param c the collection to load with the result set data
179:             * @param singleObject if true and a row has a single column then an object will be added to the collection. 
180:             *                     If false then a collection of the row data is added to the collection passed in.
181:             *
182:             * @return the collection that was passed in
183:             */
184:
185:            public static Collection loadCollection(ResultSet resultSet,
186:                    Collection c, boolean singleObject)
187:                    throws JPersistException, SQLException,
188:                    InstantiationException, IllegalAccessException {
189:                if (resultSet != null) {
190:                    ResultSetMetaData metaData = resultSet.getMetaData();
191:                    int columnCount = metaData.getColumnCount();
192:
193:                    while (resultSet.next()) {
194:                        if (columnCount == 1 && singleObject)
195:                            c.add(resultSet.getObject(1));
196:                        else {
197:                            Collection r = (Collection) c.getClass()
198:                                    .newInstance();
199:
200:                            for (int i = 0; i < columnCount; i++)
201:                                r.add(resultSet.getObject(i + 1));
202:
203:                            c.add(r);
204:                        }
205:                    }
206:                }
207:
208:                return c;
209:            }
210:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.