File: BasicProgram.html

package info (click to toggle)
db5.3 5.3.28%2Bdfsg1-0.8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 158,400 kB
  • sloc: ansic: 448,406; java: 111,824; tcl: 80,544; sh: 44,326; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,077; javascript: 1,998; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (454 lines) | stat: -rw-r--r-- 13,551 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Chapter 2.  The Basic Program</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
    <link rel="start" href="index.html" title="Berkeley DB Collections Tutorial" />
    <link rel="up" href="index.html" title="Berkeley DB Collections Tutorial" />
    <link rel="prev" href="tutorialintroduction.html" title="Tutorial Introduction" />
    <link rel="next" href="opendbenvironment.html" title="Opening and Closing the Database Environment" />
  </head>
  <body>
    <div xmlns="" class="navheader">
      <div class="libver">
        <p>Library Version 11.2.5.3</p>
      </div>
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Chapter 2. 
		The Basic Program
	</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="tutorialintroduction.html">Prev</a> </td>
          <th width="60%" align="center"> </th>
          <td width="20%" align="right"> <a accesskey="n" href="opendbenvironment.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="chapter" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title"><a id="BasicProgram"></a>Chapter 2. 
		The Basic Program
	</h2>
          </div>
        </div>
      </div>
      <div class="toc">
        <p>
          <b>Table of Contents</b>
        </p>
        <dl>
          <dt>
            <span class="sect1">
              <a href="BasicProgram.html#keyandvalueclasses">
		Defining Serialized Key and Value Classes
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="opendbenvironment.html">
		Opening and Closing the Database Environment
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="openclasscatalog.html">
		Opening and Closing the Class Catalog
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="opendatabases.html">
		Opening and Closing Databases
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="createbindingscollections.html">
		Creating Bindings and Collections
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="implementingmain.html">
		Implementing the Main Program
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="usingtransactions.html">
		Using Transactions
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="addingdatabaseitems.html">
		Adding Database Items
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="retrievingdatabaseitems.html">
		Retrieving Database Items
	</a>
            </span>
          </dt>
          <dt>
            <span class="sect1">
              <a href="handlingexceptions.html">
		Handling Exceptions
	</a>
            </span>
          </dt>
        </dl>
      </div>
      <p>
    The Basic example is a minimal implementation of the shipment
	program. It writes and reads the part, supplier and shipment
	databases.
</p>
      <p>
    The complete source of the final version of the example program
	is included in the Berkeley DB distribution.
</p>
      <div class="sect1" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h2 class="title" style="clear: both"><a id="keyandvalueclasses"></a>
		Defining Serialized Key and Value Classes
	</h2>
            </div>
          </div>
        </div>
        <p>
    The key and value classes for each type of shipment record —
	Parts, Suppliers and Shipments — are defined as ordinary Java
	classes. In this example the serialized form of the key and value
	objects is stored directly in the database. Therefore these classes
	must implement the standard Java java.io.Serializable interface. A
	compact form of Java serialization is used that does not duplicate
	the class description in each record. Instead the class
	descriptions are stored in the class catalog store, which is
	described in the next section. But in all other respects, standard
	Java serialization is used.
</p>
        <p>
    An important point is that instances of these classes are passed
	and returned by value, not by reference, when they are stored and
	retrieved from the database. This means that changing a key or
	value object does not automatically change the database. The object
	must be explicitly stored in the database after changing it. To
	emphasize this point the key and value classes defined here have no
	field setter methods. Setter methods can be defined, but it is
	important to remember that calling a setter method will not cause
	the change to be stored in the database. How to store and retrieve
	objects in the database will be described later.
</p>
        <p>
    Each key and value class contains a toString method that is used
	to output the contents of the object in the example program. This
	is meant for illustration only and is not required for database
	objects in general.
</p>
        <p>
    Notice that the key and value classes defined below do not
	contain any references to <code class="literal">com.sleepycat</code> packages. An
	important characteristic of these classes is that they are
	independent of the database. Therefore, they may be easily used in
	other contexts and may be defined in a way that is compatible with
	other tools and libraries.
</p>
        <p>
    The <code class="classname">PartKey</code> class contains only the Part's Number field.
</p>
        <p>
    Note that <code class="classname">PartKey</code> (as well as <code class="classname">SupplierKey</code> below)
	contain only a single String field. Instead of defining a specific
	class for each type of key, the String class by itself could have
	been used. Specific key classes were used to illustrate strong
	typing and for consistency in the example. The use of a plain
	String as an index key is illustrated in the next example program.
	It is up to the developer to use either primitive Java classes such
	as String and Integer, or strongly typed classes. When
	there is the possibility that fields will be added later to a key
	or value, a specific class should be used.
    
</p>
        <a id="cb_partkey"></a>
        <pre class="programlisting"><strong class="userinput"><code>import java.io.Serializable;

public class PartKey implements Serializable
{
    private String number;

    public PartKey(String number) {
        this.number = number;
    }

    public final String getNumber() {
        return number;
    }

    public String toString() {
        return "[PartKey: number=" + number + ']';
    }
}</code></strong> </pre>
        <p>
    The <code class="classname">PartData</code> class contains the Part's Name, Color,
	Weight and City fields.
</p>
        <a id="cb_partdata"></a>
        <pre class="programlisting"><strong class="userinput"><code>import java.io.Serializable;

public class PartData implements Serializable
{
    private String name;
    private String color;
    private Weight weight;
    private String city;

    public PartData(String name, String color, Weight weight, String city)
    {
        this.name = name;
        this.color = color;
        this.weight = weight;
        this.city = city;
    }

    public final String getName()
    {
        return name;
    }

    public final String getColor()
    {
        return color;
    }

    public final Weight getWeight()
    {
        return weight;
    }

    public final String getCity()
    {
        return city;
    }

    public String toString()
    {
        return "[PartData: name=" + name +
               " color=" + color +
               " weight=" + weight +
               " city=" + city + ']';
    }
}</code></strong> </pre>
        <p>
    The <code class="classname">Weight</code> class is also defined here, and is used as the
	type of the Part's Weight field. Just as in standard Java
	serialization, nothing special is needed to store nested objects as
	long as they are all Serializable.
</p>
        <a id="cb_weight"></a>
        <pre class="programlisting"><strong class="userinput"><code>import java.io.Serializable;

public class Weight implements Serializable
{
    public final static String GRAMS = "grams";
    public final static String OUNCES = "ounces";

    private double amount;
    private String units;

    public Weight(double amount, String units)
    {
        this.amount = amount;
        this.units = units;
    }

    public final double getAmount()
    {
        return amount;
    }

    public final String getUnits()
    {
        return units;
    }

    public String toString()
    {
        return "[" + amount + ' ' + units + ']';
    }
}</code></strong> </pre>
        <p>
    The <code class="classname">SupplierKey</code> class contains the Supplier's Number
	field.
</p>
        <a id="cb_supplierkey"></a>
        <pre class="programlisting"><strong class="userinput"><code>import java.io.Serializable;

public class SupplierKey implements Serializable
{
    private String number;

    public SupplierKey(String number)
    {
        this.number = number;
    }

    public final String getNumber()
    {
        return number;
    }

    public String toString()
    {
        return "[SupplierKey: number=" + number + ']';
    }
}</code></strong> </pre>
        <p>
    The <code class="classname">SupplierData</code> class contains the Supplier's Name,
	Status and City fields.
</p>
        <a id="cb_supplierdata"></a>
        <pre class="programlisting"><strong class="userinput"><code>import java.io.Serializable;

public class SupplierData implements Serializable
{
    private String name;
    private int status;
    private String city;

    public SupplierData(String name, int status, String city)
    {
        this.name = name;
        this.status = status;
        this.city = city;
    }

    public final String getName()
    {
        return name;
    }

    public final int getStatus()
    {
        return status;
    }

    public final String getCity()
    {
        return city;
    }

    public String toString()
    {
        return "[SupplierData: name=" + name +
               " status=" + status +
               " city=" + city + ']';
    }
}</code></strong>
	</pre>
        <p>
    The <code class="classname">ShipmentKey</code> class contains the keys of both the Part
	and Supplier.
</p>
        <a id="cb_shipmentkey"></a>
        <pre class="programlisting"><strong class="userinput"><code>import java.io.Serializable;

public class ShipmentKey implements Serializable
{
    private String partNumber;
    private String supplierNumber;

    public ShipmentKey(String partNumber, String supplierNumber)
    {
        this.partNumber = partNumber;
        this.supplierNumber = supplierNumber;
    }

    public final String getPartNumber()
    {
        return partNumber;
    }

    public final String getSupplierNumber()
    {
        return supplierNumber;
    }

    public String toString()
    {
        return "[ShipmentKey: supplier=" + supplierNumber +
                " part=" + partNumber + ']';
    }
}</code></strong> </pre>
        <p>
    The <code class="classname">ShipmentData</code> class contains only the Shipment's
	Quantity field. Like <code class="classname">PartKey</code> and <code class="classname">SupplierKey</code>,
	<code class="classname">ShipmentData</code> contains only a single primitive field.
	Therefore the Integer class could have been used instead of
	defining a specific value class.
</p>
        <a id="cb_shipmentdata"></a>
        <pre class="programlisting"><strong class="userinput"><code>import java.io.Serializable;

public class ShipmentData implements Serializable
{
    private int quantity;

    public ShipmentData(int quantity)
    {
        this.quantity = quantity;
    }

    public final int getQuantity()
    {
        return quantity;
    }

    public String toString()
    {
        return "[ShipmentData: quantity=" + quantity + ']';
    }
}</code></strong> </pre>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="tutorialintroduction.html">Prev</a> </td>
          <td width="20%" align="center"> </td>
          <td width="40%" align="right"> <a accesskey="n" href="opendbenvironment.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">Tutorial Introduction </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> 
		Opening and Closing the Database Environment
	</td>
        </tr>
      </table>
    </div>
  </body>
</html>