Debian Codegraph @

docs/collections/tutorial/addingdatabaseitems.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>Adding 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="usingtransactions.html" title="Using Transactions" />
12 <link rel="next" href="retrievingdatabaseitems.html" title="Retrieving Database Items" />
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 Adding Database Items
23 </th>
24 </tr>
25 <tr>
26 <td width="20%" align="left"><a accesskey="p" href="usingtransactions.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="retrievingdatabaseitems.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="addingdatabaseitems"></a>
40 Adding Database Items
41 </h2>
42 </div>
43 </div>
44 </div>
45 <p>
46 Adding (as well as updating, removing, and deleting) information
47 in the database is accomplished via the standard Java collections
48 API. In the example, the
49 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#put(K, V)" target="_top">Map.put</a>
50
51 method is used to add objects. All standard Java methods for
52 modifying a collection may be used with the DB Java Collections API.
53 </p>
54 <p>
55 The <code class="methodname">PopulateDatabase.doWork()</code> method calls private methods
56 for adding objects to each of the three database stores. It is
57 called via the
58 <a class="ulink" href="../../java/com/sleepycat/collections/TransactionRunner.html" target="_top">TransactionRunner</a>
59
60 class and was outlined in the previous section.
61 </p>
62 <a id="cb_populatedatabase"></a>
63 <pre class="programlisting">import java.util.Map;
64 import com.sleepycat.collections.TransactionWorker;
65 ...
66 public class Sample
67 {
68 ...
69 private SampleViews views;
70 ...
71 private class PopulateDatabase implements TransactionWorker
72 {
73 public void doWork()
74 throws Exception
75 {
76 <strong class="userinput"><code> addSuppliers();
77 addParts();
78 addShipments();</code></strong>
79 }
80 }
81 ...
82
83 <strong class="userinput"><code> private void addSuppliers()
84 {
85 }
86
87 private void addParts()
88 {
89 }
90
91 private void addShipments()
92 {
93 }</code></strong>
94 } </pre>
95 <p>
96 The <code class="methodname">addSuppliers()</code>, <code class="methodname">addParts()</code> and <code class="methodname">addShipments()</code>
97 methods add objects to the Suppliers, Parts and Shipments stores.
98 The
99 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html" target="_top">Map</a>
100
101 for each store is obtained from the <code class="classname">SampleViews</code> object.
102 </p>
103 <a id="cb_addsuppliers"></a>
104 <pre class="programlisting"> private void addSuppliers()
105 {
106 <strong class="userinput"><code> Map suppliers = views.getSupplierMap();
107 if (suppliers.isEmpty())
108 {
109 System.out.println("Adding Suppliers");
110 suppliers.put(new SupplierKey("S1"),
111 new SupplierData("Smith", 20, "London"));
112 suppliers.put(new SupplierKey("S2"),
113 new SupplierData("Jones", 10, "Paris"));
114 suppliers.put(new SupplierKey("S3"),
115 new SupplierData("Blake", 30, "Paris"));
116 suppliers.put(new SupplierKey("S4"),
117 new SupplierData("Clark", 20, "London"));
118 suppliers.put(new SupplierKey("S5"),
119 new SupplierData("Adams", 30, "Athens"));
120 }</code></strong>
121 }
122
123 private void addParts()
124 {
125 <strong class="userinput"><code> Map parts = views.getPartMap();
126 if (parts.isEmpty())
127 {
128 System.out.println("Adding Parts");
129 parts.put(new PartKey("P1"),
130 new PartData("Nut", "Red",
131 new Weight(12.0, Weight.GRAMS),
132 "London"));
133 parts.put(new PartKey("P2"),
134 new PartData("Bolt", "Green",
135 new Weight(17.0, Weight.GRAMS),
136 "Paris"));
137 parts.put(new PartKey("P3"),
138 new PartData("Screw", "Blue",
139 new Weight(17.0, Weight.GRAMS),
140 "Rome"));
141 parts.put(new PartKey("P4"),
142 new PartData("Screw", "Red",
143 new Weight(14.0, Weight.GRAMS),
144 "London"));
145 parts.put(new PartKey("P5"),
146 new PartData("Cam", "Blue",
147 new Weight(12.0, Weight.GRAMS),
148 "Paris"));
149 parts.put(new PartKey("P6"),
150 new PartData("Cog", "Red",
151 new Weight(19.0, Weight.GRAMS),
152 "London"));
153 }</code></strong>
154 }
155
156 private void addShipments()
157 {
158 <strong class="userinput"><code> Map shipments = views.getShipmentMap();
159 if (shipments.isEmpty())
160 {
161 System.out.println("Adding Shipments");
162 shipments.put(new ShipmentKey("P1", "S1"),
163 new ShipmentData(300));
164 shipments.put(new ShipmentKey("P2", "S1"),
165 new ShipmentData(200));
166 shipments.put(new ShipmentKey("P3", "S1"),
167 new ShipmentData(400));
168 shipments.put(new ShipmentKey("P4", "S1"),
169 new ShipmentData(200));
170 shipments.put(new ShipmentKey("P5", "S1"),
171 new ShipmentData(100));
172 shipments.put(new ShipmentKey("P6", "S1"),
173 new ShipmentData(100));
174 shipments.put(new ShipmentKey("P1", "S2"),
175 new ShipmentData(300));
176 shipments.put(new ShipmentKey("P2", "S2"),
177 new ShipmentData(400));
178 shipments.put(new ShipmentKey("P2", "S3"),
179 new ShipmentData(200));
180 shipments.put(new ShipmentKey("P2", "S4"),
181 new ShipmentData(200));
182 shipments.put(new ShipmentKey("P4", "S4"),
183 new ShipmentData(300));
184 shipments.put(new ShipmentKey("P5", "S4"),
185 new ShipmentData(400));
186 }</code></strong>
187 }
188 }</pre>
189 <p>
190 The key and value classes used above were defined in the
191 <a class="xref" href="BasicProgram.html#keyandvalueclasses" title="Defining Serialized Key and Value Classes">
192 Defining Serialized Key and Value Classes
193 </a>.
194 </p>
195 <p>
196 In each method above, objects are added only if the map is not
197 empty. This is a simple way of allowing the example program to be
198 run repeatedly. In real-life applications another technique —
199 checking the
200 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#containsKey(java.lang.Object)" target="_top">Map.containsKey</a>
201
202 method, for example — might be used.
203 </p>
204 </div>
205 <div class="navfooter">
206 <hr />
207 <table width="100%" summary="Navigation footer">
208 <tr>
209 <td width="40%" align="left"><a accesskey="p" href="usingtransactions.html">Prev</a> </td>
210 <td width="20%" align="center">
211 <a accesskey="u" href="BasicProgram.html">Up</a>
212 </td>
213 <td width="40%" align="right"> <a accesskey="n" href="retrievingdatabaseitems.html">Next</a></td>
214 </tr>
215 <tr>
216 <td width="40%" align="left" valign="top">
217 Using Transactions
218  </td>
219 <td width="20%" align="center">
220 <a accesskey="h" href="index.html">Home</a>
221 </td>
222 <td width="40%" align="right" valign="top"> 
223 Retrieving Database Items
224 </td>
225 </tr>
226 </table>
227 </div>
228 </body>
229 </html>