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