• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/ap/gpl/timemachine/db-4.7.25.NC/docs/collections/tutorial/
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		Adding 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.��&#10;&#9;&#9;The Basic Program&#10;&#9;" />
13    <link rel="previous" href="usingtransactions.html" title="&#10;&#9;&#9;Using Transactions&#10;&#9;" />
14    <link rel="next" href="retrievingdatabaseitems.html" title="&#10;&#9;&#9;Retrieving Database Items&#10;&#9;" />
15  </head>
16  <body>
17    <div class="navheader">
18      <table width="100%" summary="Navigation header">
19        <tr>
20          <th colspan="3" align="center">
21		Adding Database Items
22	</th>
23        </tr>
24        <tr>
25          <td width="20%" align="left"><a accesskey="p" href="usingtransactions.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="retrievingdatabaseitems.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="addingdatabaseitems"></a>
39		Adding Database Items
40	</h2>
41          </div>
42        </div>
43        <div></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 href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#put" 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 <tt class="methodname">PopulateDatabase.doWork()</tt> method calls private methods
56	for adding objects to each of the three database stores. It is
57	called via the 
58    <a 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;
64import com.sleepycat.collections.TransactionWorker;
65...
66public 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<b class="userinput"><tt>            addSuppliers();
77            addParts();
78            addShipments();</tt></b>
79        }
80    }
81    ...
82
83<b class="userinput"><tt>    private void addSuppliers()
84    {
85    }
86
87    private void addParts()
88    {
89    }
90
91    private void addShipments()
92    {
93    }</tt></b>
94} </pre>
95      <p>
96    The <tt class="methodname">addSuppliers()</tt>, <tt class="methodname">addParts()</tt> and <tt class="methodname">addShipments()</tt>
97	methods add objects to the Suppliers, Parts and Shipments stores.
98	The 
99    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html" target="_top">Map</a>
100    
101	for each store is obtained from the <tt class="classname">SampleViews</tt> object.
102</p>
103      <a id="cb_addsuppliers"></a>
104      <pre class="programlisting">    private void addSuppliers()
105    {
106<b class="userinput"><tt>        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        }</tt></b>
121    }
122
123    private void addParts()
124    {
125<b class="userinput"><tt>        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        }</tt></b>
154    }
155
156    private void addShipments()
157    {
158<b class="userinput"><tt>        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        }</tt></b>
187    } 
188}</pre>
189      <p>
190    The key and value classes used above were defined in the
191    <a href="BasicProgram.html#keyandvalueclasses">
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 href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#containsKey" 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>
230