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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
|
<?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>Database Usage Example</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="Getting Started with Berkeley DB" />
<link rel="up" href="DBEntry.html" title="Chapter 8. Database Records" />
<link rel="prev" href="bindAPI.html" title="Using the BIND APIs" />
<link rel="next" href="Cursors.html" title="Chapter 9. Using Cursors" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 12.1.6.1</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Database Usage Example</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="bindAPI.html">Prev</a> </td>
<th width="60%" align="center">Chapter 8. Database Records</th>
<td width="20%" align="right"> <a accesskey="n" href="Cursors.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="dbtJavaUsage"></a>Database Usage Example</h2>
</div>
</div>
</div>
<p>
In <a class="xref" href="CoreJavaUsage.html#MyDb" title="Example 7.1 MyDbs Class">MyDbs Class</a> we created
a class that opens and closes databases for us.
We now make use of that class to load inventory data into
two databases that we will use for our inventory system.
</p>
<p>
Again, remember that you can find the complete implementation for these functions
in:
</p>
<pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples/java/db/GettingStarted</pre>
<p>
where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you
placed your DB distribution.
</p>
<p>Note that in this example, we are going to save two types of
information. First there are a series of inventory records that identify
information about some food items (fruits, vegetables, and desserts).
These records identify particulars about each item such as the vendor that
the item can be obtained from, how much the vendor has in stock, the price
per unit, and so forth.</p>
<p>
We also want to manage vendor contact information, such as the
vendor's address and phone number, the sales representative's name
and his phone number, and so forth.
</p>
<div class="example">
<a id="inventoryjava"></a>
<p class="title">
<b>Example 8.1 Inventory.java</b>
</p>
<div class="example-contents">
<p>
All Inventory data is encapsulated in an instance of the following
class. Note that because this class is not serializable, we need a
custom tuple binding in order to place it on a <code class="classname">DatabaseEntry</code>
object. Because the <code class="classname">TupleInput</code> and
<code class="classname">TupleOutput</code> classes used by custom tuple bindings
support Java numerical types and not Java numerical classes, we use
<code class="literal">int</code> and <code class="literal">float</code> here instead of the
corresponding <code class="classname">Integer</code> and <code class="classname">Float</code>
classes.
</p>
<a id="java_dbt16"></a>
<pre class="programlisting">// File Inventory.java
package db.GettingStarted;
public class Inventory {
private String sku;
private String itemName;
private String category;
private String vendor;
private int vendorInventory;
private float vendorPrice;
public void setSku(String data) {
sku = data;
}
public void setItemName(String data) {
itemName = data;
}
public void setCategory(String data) {
category = data;
}
public void setVendorInventory(int data) {
vendorInventory = data;
}
public void setVendor(String data) {
vendor = data;
}
public void setVendorPrice(float data) {
vendorPrice = data;
}
public String getSku() { return sku; }
public String getItemName() { return itemName; }
public String getCategory() { return category; }
public int getVendorInventory() { return vendorInventory; }
public String getVendor() { return vendor; }
public float getVendorPrice() { return vendorPrice; }
} </pre>
</div>
</div>
<br class="example-break" />
<div class="example">
<a id="vendorjava"></a>
<p class="title">
<b>Example 8.2 Vendor.java</b>
</p>
<div class="example-contents">
<p>
The data for vendor records are stored in instances of the following
class. Notice that we are using serialization with this class for no
other reason than to demonstrate serializing a class instance.
</p>
<a id="java_dbt17"></a>
<pre class="programlisting">// File Vendor.java
package db.GettingStarted;
import java.io.Serializable;
public class Vendor implements Serializable {
private String repName;
private String address;
private String city;
private String state;
private String zipcode;
private String bizPhoneNumber;
private String repPhoneNumber;
private String vendor;
public void setRepName(String data) {
repName = data;
}
public void setAddress(String data) {
address = data;
}
public void setCity(String data) {
city = data;
}
public void setState(String data) {
state = data;
}
public void setZipcode(String data) {
zipcode = data;
}
public void setBusinessPhoneNumber(String data) {
bizPhoneNumber = data;
}
public void setRepPhoneNumber(String data) {
repPhoneNumber = data;
}
public void setVendorName(String data) {
vendor = data;
}
...
// Corresponding getter methods omitted for brevity.
// See examples/je/gettingStarted/Vendor.java
// for a complete implementation of this class.
} </pre>
</div>
</div>
<br class="example-break" />
<p>
Because we will not be using serialization to convert our
<code class="classname">Inventory</code> objects to a <code class="classname">DatabaseEntry</code>
object, we need a custom tuple binding:
</p>
<div class="example">
<a id="InventoryJavaBinding"></a>
<p class="title">
<b>Example 8.3 InventoryBinding.java</b>
</p>
<div class="example-contents">
<a id="java_dbt18"></a>
<pre class="programlisting">// File InventoryBinding.java
package db.GettingStarted;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;
public class InventoryBinding extends TupleBinding {
// Implement this abstract method. Used to convert
// a DatabaseEntry to an Inventory object.
public Object entryToObject(TupleInput ti) {
String sku = ti.readString();
String itemName = ti.readString();
String category = ti.readString();
String vendor = ti.readString();
int vendorInventory = ti.readInt();
float vendorPrice = ti.readFloat();
Inventory inventory = new Inventory();
inventory.setSku(sku);
inventory.setItemName(itemName);
inventory.setCategory(category);
inventory.setVendor(vendor);
inventory.setVendorInventory(vendorInventory);
inventory.setVendorPrice(vendorPrice);
return inventory;
}
// Implement this abstract method. Used to convert a
// Inventory object to a DatabaseEntry object.
public void objectToEntry(Object object, TupleOutput to) {
Inventory inventory = (Inventory)object;
to.writeString(inventory.getSku());
to.writeString(inventory.getItemName());
to.writeString(inventory.getCategory());
to.writeString(inventory.getVendor());
to.writeInt(inventory.getVendorInventory());
to.writeFloat(inventory.getVendorPrice());
}
} </pre>
</div>
</div>
<br class="example-break" />
<p>
In order to store the data identified above, we write the
<code class="classname">ExampleDatabaseLoad</code> application. This application
loads the inventory and vendor databases for you.
</p>
<p>
Inventory information is stored in a <code class="classname">Database</code>
dedicated for that purpose. The key for each such record is a product
SKU. The inventory data stored in this database are objects of the
<code class="classname">Inventory</code> class (see <a class="xref" href="dbtJavaUsage.html#inventoryjava" title="Example 8.1 Inventory.java">Inventory.java</a> for more information).
<code class="classname">ExampleDatabaseLoad</code> loads the inventory database
as follows:
</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>
Reads the inventory data from a flat text file prepared in
advance for this purpose.
</p>
</li>
<li>
<p>
Uses <code class="classname">java.lang.String</code> to create a key based
on the item's SKU.
</p>
</li>
<li>
<p>Uses an <code class="classname">Inventory</code> class instance for the
record data. This object is stored on a <code class="classname">DatabaseEntry</code>
object using <code class="classname">InventoryBinding</code>, a custom tuple
binding that we implemented above.</p>
</li>
<li>
<p>Saves each record to the inventory database.</p>
</li>
</ol>
</div>
<p>Vendor information is also stored in a <code class="classname">Database</code>
dedicated for that purpose. The vendor data stored in this database are objects of the
<code class="classname">Vendor</code> class (see <a class="xref" href="dbtJavaUsage.html#vendorjava" title="Example 8.2 Vendor.java">Vendor.java</a> for more information). To load this
<code class="classname">Database</code>, <code class="classname">ExampleDatabaseLoad</code>
does the following:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Reads the vendor data from a flat text file prepared in advance
for this purpose.</p>
</li>
<li>
<p>Uses the vendor's name as the record's key.</p>
</li>
<li>
<p>Uses a <code class="classname">Vendor</code> class instance for the
record data. This object is stored on a <code class="classname">DatabaseEntry</code>
object using <code class="classname">com.sleepycat.bind.serial.SerialBinding</code>.</p>
</li>
</ol>
</div>
<div class="example">
<a id="dbsStoredClass"></a>
<p class="title">
<b>Example 8.4 Stored Class Catalog Management with MyDbs</b>
</p>
<div class="example-contents">
<p>
Before we can write <code class="classname">ExampleDatabaseLoad</code>, we need to update
<code class="filename">MyDbs.java</code> to support the class catalogs that we need for this application.
</p>
<p>
To do this, we start by importing an additional class to support stored class catalogs:
</p>
<a id="java_dbt19"></a>
<pre class="programlisting">// File: MyDbs.java
package db.GettingStarted;
<strong class="userinput"><code>import com.sleepycat.bind.serial.StoredClassCatalog;</code></strong>
import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.DatabaseType;
import java.io.FileNotFoundException; </pre>
<p>
We also need to add two additional private data members to this class. One
supports the database used for the class catalog, and the other is used as a
handle for the class catalog itself.
</p>
<a id="java_dbt20"></a>
<pre class="programlisting">public class MyDbs {
// The databases that our application uses
private Database vendorDb = null;
private Database inventoryDb = null;
<strong class="userinput"><code>private Database classCatalogDb = null;
// Needed for object serialization
private StoredClassCatalog classCatalog;</code></strong>
private String vendordb = "VendorDB.db";
private String inventorydb = "InventoryDB.db";
<strong class="userinput"><code>private String classcatalogdb = "ClassCatalogDB.db";</code></strong>
// Our constructor does nothing
public MyDbs() {} </pre>
<p>
Next we need to update the <code class="methodname">MyDbs.setup()</code> method
to open the class catalog database and create the class catalog.
</p>
<a id="java_dbt21"></a>
<pre class="programlisting"> // The setup() method opens all our databases
// for us.
public void setup(String databasesHome)
throws DatabaseException {
DatabaseConfig myDbConfig = new DatabaseConfig();
...
// Database configuration omitted for brevity
...
// Now open, or create and open, our databases
// Open the vendors and inventory databases
try {
vendordb = databasesHome + "/" + vendordb;
vendorDb = new Database(vendordb,
null,
myDbConfig);
inventorydb = databasesHome + "/" + inventorydb;
inventoryDb = new Database(inventorydb,
null,
myDbConfig);
<strong class="userinput"><code>// Open the class catalog db. This is used to
// optimize class serialization.
classcatalogdb = databasesHome + "/" + classcatalogdb;
classCatalogDb = new Database(classcatalogdb,
null,
myDbConfig); </code></strong>
} catch(FileNotFoundException fnfe) {
System.err.println("MyDbs: " + fnfe.toString());
System.exit(-1);
}
} </pre>
<p>
Finally we need a getter method to return the class catalog. Note that we do not provide a getter for
the catalog database itself – our application has no need for that.
</p>
<p>
We also update our <code class="methodname">close()</code> to close our class catalog.
</p>
<a id="java_dbt22"></a>
<pre class="programlisting"> // getter methods
public Database getVendorDB() {
return vendorDb;
}
public Database getInventoryDB() {
return inventoryDb;
}
<strong class="userinput"><code>public StoredClassCatalog getClassCatalog() {
return classCatalog;
}</code></strong>
</pre>
<p>
Finally, we need our <code class="methodname">close()</code> method:
</p>
<a id="java_dbt23"></a>
<pre class="programlisting">
// Close the databases
public void close() {
try {
if (vendorDb != null) {
vendorDb.close();
}
if (inventoryDb != null) {
inventoryDb.close();
}
<strong class="userinput"><code>if (classCatalogDb != null) {
classCatalogDb.close();
}</code></strong>
} catch(DatabaseException dbe) {
System.err.println("Error closing MyDbs: " +
dbe.toString());
System.exit(-1);
}
}
} </pre>
</div>
</div>
<br class="example-break" />
<p>
So far we have identified the data that we want to store in our
databases and how we will convert that data in and out of
<code class="classname">DatabaseEntry</code> objects for database storage. We
have also updated <code class="classname">MyDbs</code> to manage our databases
for us. Now we write <code class="classname">ExampleDatabaseLoad</code> to
actually put the inventory and vendor data into their respective
databases. Because of the work that we have done so far, this
application is actually fairly simple to write.
</p>
<div class="example">
<a id="EDL"></a>
<p class="title">
<b>Example 8.5 ExampleDatabaseLoad.java</b>
</p>
<div class="example-contents">
<p>First we need the usual series of import statements:</p>
<a id="java_dbt24"></a>
<pre class="programlisting">// File: ExampleDatabaseLoad.java
package db.GettingStarted;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException; </pre>
<p>Next comes the class declaration and the private data members that
we need for this class. Most of these are setting up default values for
the program.</p>
<p>Note that two <code class="classname">DatabaseEntry</code> objects are
instantiated here. We will reuse these for every database operation that
this program performs. Also a <code class="classname">MyDbEnv</code> object is
instantiated here. We can do this because its constructor never throws
an exception. See <a class="xref" href="dbtJavaUsage.html#dbsStoredClass" title="Example 8.4 Stored Class Catalog Management with MyDbs">Stored Class Catalog Management with MyDbs</a> for
its implementation details.</p>
<p>Finally, the <code class="filename">inventory.txt</code> and
<code class="filename">vendors.txt</code> file can be found in the GettingStarted
examples directory along with the classes described in this extended
example.</p>
<a id="java_dbt25"></a>
<pre class="programlisting">public class ExampleDatabaseLoad {
private static String myDbsPath = "./";
private static File inventoryFile = new File("./inventory.txt");
private static File vendorsFile = new File("./vendors.txt");
// DatabaseEntries used for loading records
private static DatabaseEntry theKey = new DatabaseEntry();
private static DatabaseEntry theData = new DatabaseEntry();
// Encapsulates the databases.
private static MyDbs myDbs = new MyDbs(); </pre>
<p>
Next comes the <code class="methodname">usage()</code> and
<code class="methodname">main()</code> methods. Notice the exception handling
in the <code class="methodname">main()</code> method. This is the only place in the application where we
catch exceptions. For this reason, we must catch
<code class="classname">DatabaseException</code> which is thrown by the
<code class="literal">com.sleepycat.db.*</code> classes.
</p>
<p>Also notice the call to <code class="methodname">MyDbs.close()</code>
in the <code class="literal">finally</code> block. This is the only place in the
application where <code class="methodname">MyDbs.close()</code> is called.
<code class="methodname">MyDbs.close()</code> is responsible for closing
all open <code class="classname">Database</code>
handles for you.</p>
<a id="java_dbt26"></a>
<pre class="programlisting"> private static void usage() {
System.out.println("ExampleDatabaseLoad [-h <database home>]");
System.out.println(" [-i <inventory file>]");
System.out.println(" [-v <vendors file>]");
System.exit(-1);
}
public static void main(String args[]) {
ExampleDatabaseLoad edl = new ExampleDatabaseLoad();
try {
edl.run(args);
} catch (DatabaseException dbe) {
System.err.println("ExampleDatabaseLoad: " + dbe.toString());
dbe.printStackTrace();
} catch (Exception e) {
System.out.println("Exception: " + e.toString());
e.printStackTrace();
} finally {
myDbs.close();
}
System.out.println("All done.");
} </pre>
<p>Next we write the <code class="methodname">ExampleDatabaseLoad.run()</code>
method. This method is responsible for initializing all objects.
Because our environment and databases are all opened using the
<code class="methodname">MyDbs.setup()</code> method, <code class="methodname">ExampleDatabaseLoad.run()</code>
method is only responsible for calling <code class="methodname">MyDbs.setup()</code> and then calling
the <code class="classname">ExampleDatabaseLoad</code> methods that actually load the databases.
</p>
<a id="java_dbt27"></a>
<pre class="programlisting"> private void run(String args[]) throws DatabaseException {
// Parse the arguments list
parseArgs(args);
myDbs.setup(myDbsPath); // path to the environment home
System.out.println("loading vendors db.");
loadVendorsDb();
System.out.println("loading inventory db.");
loadInventoryDb();
} </pre>
<p>This next method loads the vendor database. This method
uses serialization to convert the <code class="classname">Vendor</code> object
to a <code class="classname">DatabaseEntry</code> object.</p>
<a id="java_dbt28"></a>
<pre class="programlisting"> private void loadVendorsDb()
throws DatabaseException {
// loadFile opens a flat-text file that contains our data
// and loads it into a list for us to work with. The integer
// parameter represents the number of fields expected in the
// file.
List vendors = loadFile(vendorsFile, 8);
// Now load the data into the database. The vendor's name is the
// key, and the data is a Vendor class object.
// Need a serial binding for the data
EntryBinding dataBinding =
new SerialBinding(myDbs.getClassCatalog(), Vendor.class);
for (int i = 0; i < vendors.size(); i++) {
String[] sArray = (String[])vendors.get(i);
Vendor theVendor = new Vendor();
theVendor.setVendorName(sArray[0]);
theVendor.setAddress(sArray[1]);
theVendor.setCity(sArray[2]);
theVendor.setState(sArray[3]);
theVendor.setZipcode(sArray[4]);
theVendor.setBusinessPhoneNumber(sArray[5]);
theVendor.setRepName(sArray[6]);
theVendor.setRepPhoneNumber(sArray[7]);
// The key is the vendor's name.
// ASSUMES THE VENDOR'S NAME IS UNIQUE!
String vendorName = theVendor.getVendorName();
try {
theKey = new DatabaseEntry(vendorName.getBytes("UTF-8"));
} catch (IOException willNeverOccur) {}
// Convert the Vendor object to a DatabaseEntry object
// using our SerialBinding
dataBinding.objectToEntry(theVendor, theData);
// Put it in the database.
myDbs.getVendorDB().put(null, theKey, theData);
}
} </pre>
<p>Now load the inventory database. This method uses our
custom tuple binding (see <a class="xref" href="dbtJavaUsage.html#InventoryJavaBinding" title="Example 8.3 InventoryBinding.java">InventoryBinding.java</a>) to convert the <code class="classname">Inventory</code>
object to a <code class="classname">DatabaseEntry</code> object.</p>
<a id="java_dbt29"></a>
<pre class="programlisting"> private void loadInventoryDb()
throws DatabaseException {
// loadFile opens a flat-text file that contains our data
// and loads it into a list for us to work with. The integer
// parameter represents the number of fields expected in the
// file.
List inventoryArray = loadFile(inventoryFile, 6);
// Now load the data into the database. The item's sku is the
// key, and the data is an Inventory class object.
// Need a tuple binding for the Inventory class.
TupleBinding inventoryBinding = new InventoryBinding();
for (int i = 0; i < inventoryArray.size(); i++) {
String[] sArray = (String[])inventoryArray.get(i);
String sku = sArray[1];
try {
theKey = new DatabaseEntry(sku.getBytes("UTF-8"));
} catch (IOException willNeverOccur) {}
Inventory theInventory = new Inventory();
theInventory.setItemName(sArray[0]);
theInventory.setSku(sArray[1]);
Float price = new Float(sArray[2]);
theInventory.setVendorPrice(price.floatValue());
Integer vInventory = new Integer(sArray[3]);
theInventory.setVendorInventory(vInventory.intValue());
theInventory.setCategory(sArray[4]);
theInventory.setVendor(sArray[5]);
// Place the Vendor object on the DatabaseEntry object using
// our the tuple binding we implemented in
// InventoryBinding.java
inventoryBinding.objectToEntry(theInventory, theData);
// Put it in the database. Note that this causes our
// secondary database to be automatically updated for us.
myDbs.getInventoryDB().put(null, theKey, theData);
}
}</pre>
<p>
The remainder of this application provides utility methods to
read a flat text file into an array of strings and parse the
command line options:
</p>
<a id="java_dbt30"></a>
<pre class="programlisting"> private static void parseArgs(String args[]) {
// Implementation omitted for brevity.
}
private List loadFile(File theFile, int numFields) {
List records = new ArrayList();
// Implementation omitted for brevity.
return records;
}
protected ExampleDatabaseLoad() {}
} </pre>
<p>
From the perspective of this document, these
things are relatively uninteresting. You can see how they are
implemented by looking at <code class="filename">ExampleDatabaseLoad.java</code>
in:
</p>
<pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples/java/db/GettingStarted</pre>
<p>
where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you
placed your DB distribution.
</p>
</div>
</div>
<br class="example-break" />
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="bindAPI.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="DBEntry.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="Cursors.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Using the BIND APIs </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Chapter 9. Using Cursors</td>
</tr>
</table>
</div>
</body>
</html>
|