KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > test > jdbcstub > ResultSetImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.test.jdbcstub;
21
22 import java.sql.SQLException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import org.netbeans.modules.j2ee.persistence.editor.completion.*;
28 import org.netbeans.test.stub.api.StubDelegate;
29
30 /**
31  *
32  * @author Andrei Badea
33  */

34 public class ResultSetImpl extends StubDelegate {
35     
36     private List JavaDoc/*<List<Object>>*/ columns;
37     private Map JavaDoc/*<String, List<Object>*/ names2iterators = new HashMap JavaDoc();
38     private Map JavaDoc/*<String, Object>*/ names2values; // current row values
39

40     public ResultSetImpl(List JavaDoc columns) {
41         this.columns = columns;
42
43         for (Iterator JavaDoc it = columns.iterator(); it.hasNext();) {
44             List JavaDoc column = (List JavaDoc)it.next();
45             Iterator JavaDoc columnIterator = column.iterator();
46             String JavaDoc columnName = columnIterator.next().toString();
47             names2iterators.put(columnName, columnIterator);
48         }
49     }
50
51     public boolean next() {
52         if (names2values != null) {
53             names2values.clear();
54         } else {
55             names2values = new HashMap JavaDoc();
56         }
57         
58         Iterator JavaDoc it = names2iterators.entrySet().iterator();
59         if (!it.hasNext()) {
60             return false;
61         }
62
63         while (it.hasNext()) {
64             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
65             String JavaDoc columnName = (String JavaDoc)entry.getKey();
66             Iterator JavaDoc columnIterator = (Iterator JavaDoc)entry.getValue();
67
68             if (!columnIterator.hasNext()) {
69                 return false;
70             }
71
72             Object JavaDoc value = columnIterator.next();
73             names2values.put(columnName, value);
74         }
75         
76         return true;
77     }
78     
79     public Object JavaDoc getObject(String JavaDoc columnName) throws SQLException JavaDoc {
80         if (names2values == null) {
81             throw new SQLException JavaDoc("The next() method has not been called yet");
82         }
83         if (!names2values.containsKey(columnName)) {
84             throw new SQLException JavaDoc("Unknown column name " + columnName + ".");
85         }
86         return names2values.get(columnName);
87     }
88     
89     public short getShort(String JavaDoc columnName) throws SQLException JavaDoc {
90         Object JavaDoc value = getObject(columnName);
91         if (value instanceof Short JavaDoc) {
92             return ((Short JavaDoc)value).shortValue();
93         } else {
94             throw new SQLException JavaDoc(value + "is not a short.");
95         }
96     }
97     
98     public int getInt(String JavaDoc columnName) throws SQLException JavaDoc {
99         Object JavaDoc value = getObject(columnName);
100         if (value instanceof Integer JavaDoc){
101             return ((Integer JavaDoc)value).intValue();
102         } else {
103             throw new SQLException JavaDoc(value + " is not an int.");
104         }
105     }
106     
107     public boolean getBoolean(String JavaDoc columnName) throws SQLException JavaDoc {
108         Object JavaDoc value = getObject(columnName);
109         if (value instanceof Boolean JavaDoc) {
110             return ((Boolean JavaDoc)value).booleanValue();
111         } else {
112             throw new SQLException JavaDoc(value + " is not a boolean.");
113         }
114     }
115
116     public String JavaDoc getString(String JavaDoc columnName) throws SQLException JavaDoc {
117         Object JavaDoc value = getObject(columnName);
118         return value != null ? value.toString() : null;
119     }
120     
121     public void close() {
122     }
123 }
124
Popular Tags