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