Debian Codegraph @

docs/collections/tutorial/retrievingdatabaseitems.html

raw
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Retrieving Database Items</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Berkeley DB Collections Tutorial" />
10 <link rel="up" href="BasicProgram.html" title="Chapter 2.  The Basic Program" />
11 <link rel="prev" href="addingdatabaseitems.html" title="Adding Database Items" />
12 <link rel="next" href="handlingexceptions.html" title="Handling Exceptions" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">
22 Retrieving Database Items
23 </th>
24 </tr>
25 <tr>
26 <td width="20%" align="left"><a accesskey="p" href="addingdatabaseitems.html">Prev</a> </td>
27 <th width="60%" align="center">Chapter 2. 
28 The Basic Program
29 </th>
30 <td width="20%" align="right"> <a accesskey="n" href="handlingexceptions.html">Next</a></td>
31 </tr>
32 </table>
33 <hr />
34 </div>
35 <div class="sect1" lang="en" xml:lang="en">
36 <div class="titlepage">
37 <div>
38 <div>
39 <h2 class="title" style="clear: both"><a id="retrievingdatabaseitems"></a>
40 Retrieving Database Items
41 </h2>
42 </div>
43 </div>
44 </div>
45 <p>
46 Retrieving information from the database is accomplished via the
47 standard Java collections API. In the example, the
48 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Set.html#iterator()" target="_top">Set.iterator</a>
49
50 method is used to iterate all
51 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.Entry.html" target="_top">Map.Entry</a>
52
53 objects for each store. All standard Java methods for retrieving
54 objects from a collection may be used with the DB Java Collections API.
55 </p>
56 <p>
57 The <code class="methodname">PrintDatabase.doWork()</code> method calls
58 <code class="methodname">printEntries()</code>
59 to print the map entries for each database store. It is called via
60 the
61 <a class="ulink" href="../../java/com/sleepycat/collections/TransactionRunner.html" target="_top">TransactionRunner</a>
62
63 class and was outlined in the previous section.
64 </p>
65 <a id="cb_printdatabase"></a>
66 <pre class="programlisting">import java.util.Iterator;
67 ...
68 public class Sample
69 {
70 ...
71 private SampleViews views;
72 ...
73 private class PrintDatabase implements TransactionWorker
74 {
75 public void doWork()
76 throws Exception
77 {
78 <strong class="userinput"><code> printEntries("Parts",
79 views.getPartEntrySet().iterator());
80 printEntries("Suppliers",
81 views.getSupplierEntrySet().iterator());
82 printEntries("Shipments",
83 views.getShipmentEntrySet().iterator());</code></strong>
84 }
85 }
86 ...
87
88 <strong class="userinput"><code> private void printEntries(String label, Iterator iterator)
89 {
90 }</code></strong>
91 ...
92 } </pre>
93 <p>
94 The
95 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Set.html" target="_top">Set</a>
96
97 of
98 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.Entry.html" target="_top">Map.Entry</a>
99
100 objects for each store is obtained from the <code class="classname">SampleViews</code>
101 object. This set can also be obtained by calling the
102 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#entrySet()" target="_top">Map.entrySet</a>
103
104 method of a stored map.
105 </p>
106 <p>
107 The <code class="methodname">printEntries()</code> prints the map entries for any stored
108 map. The
109 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#toString()" target="_top">Object.toString</a>
110
111 method of each key and value is called to
112 obtain a printable representation of each object.
113 </p>
114 <a id="cb_printentries"></a>
115 <pre class="programlisting"> private void printEntries(String label, Iterator iterator)
116 {
117 <strong class="userinput"><code> System.out.println("\n--- " + label + " ---");
118 while (iterator.hasNext())
119 {
120 Map.Entry entry = (Map.Entry) iterator.next();
121 System.out.println(entry.getKey().toString());
122 System.out.println(entry.getValue().toString());
123 }</code></strong>
124 } </pre>
125 <p>
126 This is one of a small number of behavioral differences between
127 standard Java collections and stored collections. For a complete
128 list see
129 <a class="xref" href="UsingStoredCollections.html" title="Using Stored Collections">
130 Using Stored Collections
131 </a>.
132 </p>
133 <p>
134 The output of the example program is shown below.
135 </p>
136 <pre class="programlisting">Adding Suppliers
137 Adding Parts
138 Adding Shipments
139
140 --- Parts ---
141 PartKey: number=P1
142 PartData: name=Nut color=Red weight=[12.0 grams] city=London
143 PartKey: number=P2
144 PartData: name=Bolt color=Green weight=[17.0 grams] city=Paris
145 PartKey: number=P3
146 PartData: name=Screw color=Blue weight=[17.0 grams] city=Rome
147 PartKey: number=P4
148 PartData: name=Screw color=Red weight=[14.0 grams] city=London
149 PartKey: number=P5
150 PartData: name=Cam color=Blue weight=[12.0 grams] city=Paris
151 PartKey: number=P6
152 PartData: name=Cog color=Red weight=[19.0 grams] city=London
153
154 --- Suppliers ---
155 SupplierKey: number=S1
156 SupplierData: name=Smith status=20 city=London
157 SupplierKey: number=S2
158 SupplierData: name=Jones status=10 city=Paris
159 SupplierKey: number=S3
160 SupplierData: name=Blake status=30 city=Paris
161 SupplierKey: number=S4
162 SupplierData: name=Clark status=20 city=London
163 SupplierKey: number=S5
164 SupplierData: name=Adams status=30 city=Athens
165
166 --- Shipments ---
167 ShipmentKey: supplier=S1 part=P1
168 ShipmentData: quantity=300
169 ShipmentKey: supplier=S2 part=P1
170 ShipmentData: quantity=300
171 ShipmentKey: supplier=S1 part=P2
172 ShipmentData: quantity=200
173 ShipmentKey: supplier=S2 part=P2
174 ShipmentData: quantity=400
175 ShipmentKey: supplier=S3 part=P2
176 ShipmentData: quantity=200
177 ShipmentKey: supplier=S4 part=P2
178 ShipmentData: quantity=200
179 ShipmentKey: supplier=S1 part=P3
180 ShipmentData: quantity=400
181 ShipmentKey: supplier=S1 part=P4
182 ShipmentData: quantity=200
183 ShipmentKey: supplier=S4 part=P4
184 ShipmentData: quantity=300
185 ShipmentKey: supplier=S1 part=P5
186 ShipmentData: quantity=100
187 ShipmentKey: supplier=S4 part=P5
188 ShipmentData: quantity=400
189 ShipmentKey: supplier=S1 part=P6
190 ShipmentData: quantity=100 </pre>
191 </div>
192 <div class="navfooter">
193 <hr />
194 <table width="100%" summary="Navigation footer">
195 <tr>
196 <td width="40%" align="left"><a accesskey="p" href="addingdatabaseitems.html">Prev</a> </td>
197 <td width="20%" align="center">
198 <a accesskey="u" href="BasicProgram.html">Up</a>
199 </td>
200 <td width="40%" align="right"> <a accesskey="n" href="handlingexceptions.html">Next</a></td>
201 </tr>
202 <tr>
203 <td width="40%" align="left" valign="top">
204 Adding Database Items
205  </td>
206 <td width="20%" align="center">
207 <a accesskey="h" href="index.html">Home</a>
208 </td>
209 <td width="40%" align="right" valign="top"> 
210 Handling Exceptions
211 </td>
212 </tr>
213 </table>
214 </div>
215 </body>
216 </html>