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> 7 Retrieving Database Items 8 </title> 9 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 10 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 11 <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" /> 12 <link rel="up" href="BasicProgram.html" title="Chapter 2. 		The Basic Program 	" /> 13 <link rel="previous" href="addingdatabaseitems.html" title=" 		Adding Database Items 	" /> 14 <link rel="next" href="handlingexceptions.html" title=" 		Handling Exceptions 	" /> 15 </head> 16 <body> 17 <div class="navheader"> 18 <table width="100%" summary="Navigation header"> 19 <tr> 20 <th colspan="3" align="center"> 21 Retrieving Database Items 22 </th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="addingdatabaseitems.html">Prev</a> </td> 26 <th width="60%" align="center">Chapter 2. 27 The Basic Program 28 </th> 29 <td width="20%" align="right"> <a accesskey="n" href="handlingexceptions.html">Next</a></td> 30 </tr> 31 </table> 32 <hr /> 33 </div> 34 <div class="sect1" lang="en" xml:lang="en"> 35 <div class="titlepage"> 36 <div> 37 <div> 38 <h2 class="title" style="clear: both"><a id="retrievingdatabaseitems"></a> 39 Retrieving Database Items 40 </h2> 41 </div> 42 </div> 43 <div></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 href="http://java.sun.com/j2se/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 href="http://java.sun.com/j2se/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 <tt class="methodname">PrintDatabase.doWork()</tt> method calls 58 <tt class="methodname">printEntries()</tt> 59 to print the map entries for each database store. It is called via 60 the 61 <a 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... 68public 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<b class="userinput"><tt> printEntries("Parts", 79 views.getPartEntrySet().iterator()); 80 printEntries("Suppliers", 81 views.getSupplierEntrySet().iterator()); 82 printEntries("Shipments", 83 views.getShipmentEntrySet().iterator());</tt></b> 84 } 85 } 86 ... 87 88<b class="userinput"><tt> private void printEntries(String label, Iterator iterator) 89 { 90 }</tt></b> 91 ... 92} </pre> 93 <p> 94 The 95 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html" target="_top">Set</a> 96 97 of 98 <a href="http://java.sun.com/j2se/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 <tt class="classname">SampleViews</tt> 101 object. This set can also be obtained by calling the 102 <a href="http://java.sun.com/j2se/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 <tt class="methodname">printEntries()</tt> prints the map entries for any stored 108 map. The 109 <a href="http://java.sun.com/j2se/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<b class="userinput"><tt> 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 }</tt></b> 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 href="UsingStoredCollections.html"> 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 137Adding Parts 138Adding Shipments 139 140--- Parts --- 141PartKey: number=P1 142PartData: name=Nut color=Red weight=[12.0 grams] city=London 143PartKey: number=P2 144PartData: name=Bolt color=Green weight=[17.0 grams] city=Paris 145PartKey: number=P3 146PartData: name=Screw color=Blue weight=[17.0 grams] city=Rome 147PartKey: number=P4 148PartData: name=Screw color=Red weight=[14.0 grams] city=London 149PartKey: number=P5 150PartData: name=Cam color=Blue weight=[12.0 grams] city=Paris 151PartKey: number=P6 152PartData: name=Cog color=Red weight=[19.0 grams] city=London 153 154--- Suppliers --- 155SupplierKey: number=S1 156SupplierData: name=Smith status=20 city=London 157SupplierKey: number=S2 158SupplierData: name=Jones status=10 city=Paris 159SupplierKey: number=S3 160SupplierData: name=Blake status=30 city=Paris 161SupplierKey: number=S4 162SupplierData: name=Clark status=20 city=London 163SupplierKey: number=S5 164SupplierData: name=Adams status=30 city=Athens 165 166--- Shipments --- 167ShipmentKey: supplier=S1 part=P1 168ShipmentData: quantity=300 169ShipmentKey: supplier=S2 part=P1 170ShipmentData: quantity=300 171ShipmentKey: supplier=S1 part=P2 172ShipmentData: quantity=200 173ShipmentKey: supplier=S2 part=P2 174ShipmentData: quantity=400 175ShipmentKey: supplier=S3 part=P2 176ShipmentData: quantity=200 177ShipmentKey: supplier=S4 part=P2 178ShipmentData: quantity=200 179ShipmentKey: supplier=S1 part=P3 180ShipmentData: quantity=400 181ShipmentKey: supplier=S1 part=P4 182ShipmentData: quantity=200 183ShipmentKey: supplier=S4 part=P4 184ShipmentData: quantity=300 185ShipmentKey: supplier=S1 part=P5 186ShipmentData: quantity=100 187ShipmentKey: supplier=S4 part=P5 188ShipmentData: quantity=400 189ShipmentKey: supplier=S1 part=P6 190ShipmentData: 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> 217