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>Database Example</title>
7    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
9    <link rel="home" href="index.html" title="Getting Started with Berkeley DB" />
10    <link rel="up" href="DB.html" title="Chapter��7.��Databases" />
11    <link rel="previous" href="CoreEnvUsage.html" title="Managing Databases in Environments" />
12    <link rel="next" href="DBEntry.html" title="Chapter��8.��Database Records" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Database Example</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��7.��Databases</th>
23          <td width="20%" align="right">��<a accesskey="n" href="DBEntry.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="sect1" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title" style="clear: both"><a id="CoreJavaUsage"></a>Database Example</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <p>
38        Throughout this book we will build a couple of applications that load
39        and retrieve inventory data from DB databases. While we are not yet ready to
40        begin reading from or writing to our databases, we can at least create
41        the class that we will use to manage our databases.
42    </p>
43      <p>
44        Note that subsequent examples in this book will build on this code to
45        perform the more interesting work of writing to and reading from the
46        databases. 
47    </p>
48      <p>
49        Note that you can find the complete implementation of these functions
50        in:
51    </p>
52      <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_java/db/GettingStarted</pre>
53      <p>
54        where <tt class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></tt> is the location where you
55        placed your DB distribution.  
56    </p>
57      <div class="example">
58        <a id="MyDb"></a>
59        <p class="title">
60          <b>Example 7.1 MyDbs Class</b>
61        </p>
62        <p>
63            To manage our database open and close activities, we encapsulate them
64            in the <tt class="classname">MyDbs</tt> class. There are several good reasons
65            to do this, the most important being that we can ensure our databases are
66            closed by putting that activity in the <tt class="classname">MyDbs</tt>
67            class destructor.
68        </p>
69        <p>
70            To begin, we import some needed classes:
71        </p>
72        <a id="java_db12"></a>
73        <pre class="programlisting">// File: MyDbs.java
74package db.GettingStarted;
75
76import com.sleepycat.db.Database;
77import com.sleepycat.db.DatabaseConfig;
78import com.sleepycat.db.DatabaseException;
79import com.sleepycat.db.DatabaseType;
80
81import java.io.FileNotFoundException; </pre>
82        <p>
83    And then we write our class declaration and provided some necessary private data members:
84</p>
85        <a id="java_db13"></a>
86        <pre class="programlisting">public class MyDbs {
87
88    // The databases that our application uses
89    private Database vendorDb = null;
90    private Database inventoryDb = null;
91
92    private String vendordb = "VendorDB.db";
93    private String inventorydb = "InventoryDB.db";
94
95    // Our constructor does nothing
96    public MyDbs() {} </pre>
97        <p>
98    Next we need a <tt class="methodname">setup()</tt> method. This is where
99    we configure and open our databases.
100</p>
101        <a id="java_db14"></a>
102        <pre class="programlisting">    // The setup() method opens all our databases
103    // for us.
104    public void setup(String databasesHome)
105        throws DatabaseException {
106
107        DatabaseConfig myDbConfig = new DatabaseConfig();
108
109        myDbConfig.setErrorStream(System.err);
110        myDbConfig.setErrorPrefix("MyDbs");
111        myDbConfig.setType(DatabaseType.BTREE);
112        myDbConfig.setAllowCreate(true);
113
114        // Now open, or create and open, our databases
115        // Open the vendors and inventory databases
116        try {
117            vendordb = databasesHome + "/" + vendordb;
118            vendorDb = new Database(vendordb,
119                                    null,
120                                    myDbConfig);
121
122            inventorydb = databasesHome + "/" + inventorydb;
123            inventoryDb = new Database(inventorydb,
124                                        null,
125                                        myDbConfig);
126        } catch(FileNotFoundException fnfe) {
127            System.err.println("MyDbs: " + fnfe.toString());
128            System.exit(-1);
129        }
130    } </pre>
131        <p>
132    Finally, we provide some getter methods, and our <tt class="methodname">close()</tt> method.
133</p>
134        <a id="java_db15"></a>
135        <pre class="programlisting">   // getter methods
136    public Database getVendorDB() {
137        return vendorDb;
138    }
139
140    public Database getInventoryDB() {
141        return inventoryDb;
142    }
143
144    // Close the databases
145    public void close() {
146        try {
147            if (vendorDb != null) {
148                vendorDb.close();
149            }
150
151            if (inventoryDb != null) {
152                inventoryDb.close();
153            }
154        } catch(DatabaseException dbe) {
155            System.err.println("Error closing MyDbs: " +
156                                dbe.toString());
157            System.exit(-1);
158        }
159    }
160} </pre>
161      </div>
162    </div>
163    <div class="navfooter">
164      <hr />
165      <table width="100%" summary="Navigation footer">
166        <tr>
167          <td width="40%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a>��</td>
168          <td width="20%" align="center">
169            <a accesskey="u" href="DB.html">Up</a>
170          </td>
171          <td width="40%" align="right">��<a accesskey="n" href="DBEntry.html">Next</a></td>
172        </tr>
173        <tr>
174          <td width="40%" align="left" valign="top">Managing Databases in Environments��</td>
175          <td width="20%" align="center">
176            <a accesskey="h" href="index.html">Home</a>
177          </td>
178          <td width="40%" align="right" valign="top">��Chapter��8.��Database Records</td>
179        </tr>
180      </table>
181    </div>
182  </body>
183</html>
184