KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > jdbc > ResultSetHashMap


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18
19 package org.apache.beehive.controls.system.jdbc;
20
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.ResultSetMetaData JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 /**
27  * The ResultSetHashMap class extends a standard HashMap and
28  * populates it with data derived from a JDBC ResultSet.
29  * <p/>
30  * Note: the keys are treated case-insensitively, and therefore requests
31  * made on the map are case-insensitive. Any direct access to the keys
32  * will yield uppercase keys.
33  * <p/>
34  * Note: only the row associated with the current cursor position
35  * is used.
36  */

37 public class ResultSetHashMap extends HashMap JavaDoc<String JavaDoc, Object JavaDoc> {
38
39     ResultSetHashMap() {
40         super();
41     }
42
43     ResultSetHashMap(int size) {
44         super(size);
45     }
46
47     /**
48      * This constructor is optimized for being called in a loop.
49      * Preserve the upper case column list for performance.
50      */

51     ResultSetHashMap(ResultSet JavaDoc rs, String JavaDoc[] keys) throws SQLException JavaDoc {
52         super();
53         assert keys.length == rs.getMetaData().getColumnCount() + 1;
54
55         for (int i = 1; i < keys.length; i++) {
56             assert keys[i].equals(keys[i].toUpperCase());
57             super.put(keys[i], rs.getObject(i));
58         }
59     }
60
61
62     ResultSetHashMap(ResultSet JavaDoc rs) throws SQLException JavaDoc {
63         super();
64         ResultSetMetaData JavaDoc md = rs.getMetaData();
65         for (int i = 1; i <= md.getColumnCount(); i++) {
66             super.put(md.getColumnName(i).toUpperCase(), rs.getObject(i));
67         }
68     }
69
70
71     public boolean containsKey(String JavaDoc key) {
72         return super.containsKey(key.toUpperCase());
73     }
74
75
76     public Object JavaDoc get(String JavaDoc key) {
77         return super.get(key.toUpperCase());
78     }
79
80
81     public Object JavaDoc put(String JavaDoc key, Object JavaDoc value) {
82         return super.put(key.toUpperCase(), value);
83     }
84
85
86     public Object JavaDoc remove(String JavaDoc key) {
87         return super.remove(key.toUpperCase());
88     }
89 }
90
91
Popular Tags