Sophie

Sophie

distrib > Mandriva > 2007.0 > x86_64 > media > main-release > by-pkgid > a4c98df40e78f6c892308fd6841f950a > files > 839

lib64db4.2-devel-4.2.52-11mdv2007.0.x86_64.rpm

<!--$Id: write.so,v 1.3 2003/10/14 16:52:00 gburd Exp $-->
<!--Copyright 1997-2003 by Sleepycat Software, Inc.-->
<!--All rights reserved.-->
<!--See the file LICENSE for redistribution information.-->
<html>
<head>
<title>Berkeley DB Reference Guide: Adding database items</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
</head>
<body bgcolor=white>
<a name="2"><!--meow--></a>
<table width="100%"><tr valign=top>
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Java API Tutorial - Basic</dl></h3></td>
<td align=right><a href="../bdb_basic/transact.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb_basic/read.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h3 align=center>Adding database items</h3>
<p>Adding (as well as updating, removing, and deleting) information in the
database is accomplished via the standard Java collections API.  In the
example, the 
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html#put">Map.put</a>
 method is used to add objects.
All standard Java methods for modifying a collection may be used with the
Java API.</p>
<hr size=1 noshade>
<p>The <b>PopulateDatabase.doWork</b> method calls private methods for
adding objects to each of the three database stores.  It is called via the
<a href="../../java/com/sleepycat/bdb/TransactionRunner.html">TransactionRunner</a>
 class and was outlined in
the previous section.</p>
<blockquote><pre>
import java.util.Map;
...
public class Sample
{
    ...
    private SampleViews views;
    ...
    private class PopulateDatabase implements TransactionWorker
    {
        public void doWork()
            throws Exception
        {
<b>            addSuppliers();
            addParts();
            addShipments();
</b>        }
    }
<p>
<b>    private void addSuppliers()
    {
    }
<p>
    private void addParts()
    {
    }
<p>
    private void addShipments()
    {
    }
</b>}
</pre></blockquote>
<hr size=1 noshade>
<p>The <b>addSuppliers</b>, <b>addParts</b> and <b>addShipments</b>
methods add objects to the Suppliers, Parts and Shipments stores.  The
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html">Map</a>
 for each store is obtained from the
<b>SampleViews</b> object.</p>
<blockquote><pre>
    private void addSuppliers()
    {
<b>        Map suppliers = views.getSupplierMap();
        if (suppliers.isEmpty())
        {
            System.out.println("Adding Suppliers");
            suppliers.put(new SupplierKey("S1"),
                          new SupplierValue("Smith", 20, "London"));
            suppliers.put(new SupplierKey("S2"),
                          new SupplierValue("Jones", 10, "Paris"));
            suppliers.put(new SupplierKey("S3"),
                          new SupplierValue("Blake", 30, "Paris"));
            suppliers.put(new SupplierKey("S4"),
                          new SupplierValue("Clark", 20, "London"));
            suppliers.put(new SupplierKey("S5"),
                          new SupplierValue("Adams", 30, "Athens"));
        }
</b>    }
<p>
    private void addParts()
    {
<b>        Map parts = views.getPartMap();
        if (parts.isEmpty())
        {
            System.out.println("Adding Parts");
            parts.put(new PartKey("P1"),
                      new PartValue("Nut", "Red",
                                    new Weight(12.0, Weight.GRAMS),
                                    "London"));
            parts.put(new PartKey("P2"),
                      new PartValue("Bolt", "Green",
                                    new Weight(17.0, Weight.GRAMS),
                                    "Paris"));
            parts.put(new PartKey("P3"),
                      new PartValue("Screw", "Blue",
                                    new Weight(17.0, Weight.GRAMS),
                                    "Rome"));
            parts.put(new PartKey("P4"),
                      new PartValue("Screw", "Red",
                                    new Weight(14.0, Weight.GRAMS),
                                    "London"));
            parts.put(new PartKey("P5"),
                      new PartValue("Cam", "Blue",
                                    new Weight(12.0, Weight.GRAMS),
                                    "Paris"));
            parts.put(new PartKey("P6"),
                      new PartValue("Cog", "Red",
                                    new Weight(19.0, Weight.GRAMS),
                                    "London"));
        }
</b>    }
<p>
    private void addShipments()
    {
<b>        Map shipments = views.getShipmentMap();
        if (shipments.isEmpty())
        {
            System.out.println("Adding Shipments");
            shipments.put(new ShipmentKey("P1", "S1"),
                          new ShipmentValue(300));
            shipments.put(new ShipmentKey("P2", "S1"),
                          new ShipmentValue(200));
            shipments.put(new ShipmentKey("P3", "S1"),
                          new ShipmentValue(400));
            shipments.put(new ShipmentKey("P4", "S1"),
                          new ShipmentValue(200));
            shipments.put(new ShipmentKey("P5", "S1"),
                          new ShipmentValue(100));
            shipments.put(new ShipmentKey("P6", "S1"),
                          new ShipmentValue(100));
            shipments.put(new ShipmentKey("P1", "S2"),
                          new ShipmentValue(300));
            shipments.put(new ShipmentKey("P2", "S2"),
                          new ShipmentValue(400));
            shipments.put(new ShipmentKey("P2", "S3"),
                          new ShipmentValue(200));
            shipments.put(new ShipmentKey("P2", "S4"),
                          new ShipmentValue(200));
            shipments.put(new ShipmentKey("P4", "S4"),
                          new ShipmentValue(300));
            shipments.put(new ShipmentKey("P5", "S4"),
                          new ShipmentValue(400));
        }
</b>    }
</pre></blockquote>
<p>The key and value classes used above were defined in the
<a href="keyvalue.html">Defining serialized key and value classes</a> section.</p>
<p>In each method above, objects are added only if the map is not empty.
This is a simple way of allowing the example program to be run repeatedly.  In
real-life applications another technique -- checking the
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html#containsKey">Map.containsKey</a>
 method, for example -- might be
used.</p>
<table width="100%"><tr><td><br></td><td align=right><a href="../bdb_basic/transact.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../bdb_basic/read.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p><font size=1><a href="../../sleepycat/legal.html">Copyright (c) 1996-2003</a> <a href="http://www.sleepycat.com">Sleepycat Software, Inc.</a> - All rights reserved.</font>
</body>
</html>