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>Chapter��7.��Databases</title>
7    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9    <link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
10    <link rel="up" href="baseapi.html" title="Part��II.��Programming with the Base API" />
11    <link rel="prev" href="baseapi.html" title="Part��II.��Programming with the Base API" />
12    <link rel="next" href="coredbclose.html" title="Closing Databases" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Chapter��7.��Databases</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="baseapi.html">Prev</a>��</td>
22          <th width="60%" align="center">Part��II.��Programming with the Base API</th>
23          <td width="20%" align="right">��<a accesskey="n" href="coredbclose.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="chapter" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title"><a id="DB"></a>Chapter��7.��Databases</h2>
33          </div>
34        </div>
35      </div>
36      <div class="toc">
37        <p>
38          <b>Table of Contents</b>
39        </p>
40        <dl>
41          <dt>
42            <span class="sect1">
43              <a href="DB.html#DBOpen">Opening Databases</a>
44            </span>
45          </dt>
46          <dt>
47            <span class="sect1">
48              <a href="coredbclose.html">Closing Databases</a>
49            </span>
50          </dt>
51          <dt>
52            <span class="sect1">
53              <a href="DBConfig.html">Database Properties</a>
54            </span>
55          </dt>
56          <dt>
57            <span class="sect1">
58              <a href="DBAdmin.html">Administrative Methods</a>
59            </span>
60          </dt>
61          <dt>
62            <span class="sect1">
63              <a href="dbErrorReporting.html">Error Reporting Functions</a>
64            </span>
65          </dt>
66          <dt>
67            <span class="sect1">
68              <a href="CoreEnvUsage.html">Managing Databases in Environments</a>
69            </span>
70          </dt>
71          <dt>
72            <span class="sect1">
73              <a href="CoreJavaUsage.html">Database Example</a>
74            </span>
75          </dt>
76        </dl>
77      </div>
78      <p>In Berkeley DB, a database is a collection of <span class="emphasis"><em>records</em></span>. Records,
79  in turn, consist of key/data pairings.
80  </p>
81      <p>
82	Conceptually, you can think of a 
83		<code class="classname">Database</code>
84		 
85	as containing a two-column table where column 1 contains a key and column 2
86	contains data.  Both the key and the data are managed using 
87		<code class="classname">DatabaseEntry</code> 
88		
89		
90		<span>class instances</span>
91		
92	(see <a class="xref" href="DBEntry.html" title="Chapter��8.��Database Records">Database Records</a> for details on this 
93	    <span>class</span>
94	    ).
95	So, fundamentally, using a DB 
96		<code class="classname">Database</code> 
97		 
98	involves putting, getting, and deleting database records, which in turns involves efficiently 
99	managing information 
100		<span>encapsulated by </span>
101		
102		
103		<code class="classname">DatabaseEntry</code> 
104		
105		
106		
107		<span>objects.</span>
108		
109	The next several chapters of this book are dedicated to those activities.
110  </p>
111      <p>
112        Also, note that in the previous section of this book, <a class="xref" href="dpl.html" title="Part��I.��Programming with the Direct Persistence Layer">Programming with the Direct Persistence Layer</a>, 
113        we described the DPL The DPL handles all database management
114        for you, including creating all primary and secondary databases as is
115        required by your application. That said, if you are using the DPL
116        you can access the underlying database for a given index if
117        necessary. See the Javadoc for the DPL for more information.
118  </p>
119      <div class="sect1" lang="en" xml:lang="en">
120        <div class="titlepage">
121          <div>
122            <div>
123              <h2 class="title" style="clear: both"><a id="DBOpen"></a>Opening Databases</h2>
124            </div>
125          </div>
126        </div>
127        <p>
128        You open a database by instantiating a <code class="classname">Database</code>
129        object.
130    </p>
131        <p>
132		Note that by default, DB does not create databases if they do not already exist. 
133		To override this behavior, set the <a class="link" href="DBConfig.html" title="Database Properties">creation property</a> to true.
134	</p>
135        <p>
136        The following code fragment illustrates a database open:
137        
138    </p>
139        <a id="java_db1"></a>
140        <pre class="programlisting">package db.GettingStarted;
141
142import com.sleepycat.db.DatabaseException;
143import com.sleepycat.db.Database;
144import com.sleepycat.db.DatabaseConfig;
145
146import java.io.FileNotFoundException;
147...
148
149Database myDatabase = null;
150
151...
152
153try {
154    // Open the database. Create it if it does not already exist.
155    DatabaseConfig dbConfig = new DatabaseConfig();
156    dbConfig.setAllowCreate(true);
157    myDatabase = new Database ("sampleDatabase.db",
158                               null, 
159                               dbConfig); 
160} catch (DatabaseException dbe) {
161    // Exception handling goes here
162} catch (FileNotFoundException fnfe) {
163    // Exception handling goes here
164}</pre>
165      </div>
166    </div>
167    <div class="navfooter">
168      <hr />
169      <table width="100%" summary="Navigation footer">
170        <tr>
171          <td width="40%" align="left"><a accesskey="p" href="baseapi.html">Prev</a>��</td>
172          <td width="20%" align="center">
173            <a accesskey="u" href="baseapi.html">Up</a>
174          </td>
175          <td width="40%" align="right">��<a accesskey="n" href="coredbclose.html">Next</a></td>
176        </tr>
177        <tr>
178          <td width="40%" align="left" valign="top">Part��II.��Programming with the Base API��</td>
179          <td width="20%" align="center">
180            <a accesskey="h" href="index.html">Home</a>
181          </td>
182          <td width="40%" align="right" valign="top">��Closing Databases</td>
183        </tr>
184      </table>
185    </div>
186  </body>
187</html>
188