| 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>Opening and Closing 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="Berkeley DB Collections Tutorial" /> |
| 10 |
<link rel="up" href="BasicProgram.html" title="Chapter 2. The Basic Program" /> |
| 11 |
<link rel="prev" href="openclasscatalog.html" title="Opening and Closing the Class Catalog" /> |
| 12 |
<link rel="next" href="createbindingscollections.html" title="Creating Bindings and Collections" /> |
| 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"> |
| 22 |
Opening and Closing Databases |
| 23 |
</th> |
| 24 |
</tr> |
| 25 |
<tr> |
| 26 |
<td width="20%" align="left"><a accesskey="p" href="openclasscatalog.html">Prev</a> </td> |
| 27 |
<th width="60%" align="center">Chapter 2. |
| 28 |
The Basic Program |
| 29 |
</th> |
| 30 |
<td width="20%" align="right"> <a accesskey="n" href="createbindingscollections.html">Next</a></td> |
| 31 |
</tr> |
| 32 |
</table> |
| 33 |
<hr /> |
| 34 |
</div> |
| 35 |
<div class="sect1" lang="en" xml:lang="en"> |
| 36 |
<div class="titlepage"> |
| 37 |
<div> |
| 38 |
<div> |
| 39 |
<h2 class="title" style="clear: both"><a id="opendatabases"></a> |
| 40 |
Opening and Closing Databases |
| 41 |
</h2> |
| 42 |
</div> |
| 43 |
</div> |
| 44 |
</div> |
| 45 |
<p> |
| 46 |
This section describes how to open and close the Part, Supplier |
| 47 |
and Shipment databases. A <span class="emphasis"><em>database</em></span> is a collection of |
| 48 |
records, each of which has a key and a value. The keys and values |
| 49 |
are stored in a selected format, which defines the syntax of the |
| 50 |
stored data. Two examples of formats are Java serialization format |
| 51 |
and tuple format. In a given database, all keys have the same |
| 52 |
format and all values have the same format. |
| 53 |
</p> |
| 54 |
<p> |
| 55 |
The <code class="classname">SampleDatabase</code> class is extended to open and close |
| 56 |
the three databases. The following additional class members are |
| 57 |
needed. |
| 58 |
</p> |
| 59 |
<a id="cb_sampledatabase3"></a> |
| 60 |
<pre class="programlisting">public class SampleDatabase |
| 61 |
{ |
| 62 |
... |
| 63 |
<strong class="userinput"><code> private static final String SUPPLIER_STORE = "supplier_store"; |
| 64 |
private static final String PART_STORE = "part_store"; |
| 65 |
private static final String SHIPMENT_STORE = "shipment_store";</code></strong> |
| 66 |
... |
| 67 |
<strong class="userinput"><code> private Database supplierDb; |
| 68 |
private Database partDb; |
| 69 |
private Database shipmentDb;</code></strong> |
| 70 |
... |
| 71 |
} </pre> |
| 72 |
<p> |
| 73 |
For each database there is a database name constant and a |
| 74 |
<code class="classname">Database</code> object. |
| 75 |
</p> |
| 76 |
<p> |
| 77 |
The following statements open the three databases by |
| 78 |
constructing a <code class="classname">Database</code> object. |
| 79 |
</p> |
| 80 |
<a id="cb_java_sampledatabase4"></a> |
| 81 |
<pre class="programlisting"> public SampleDatabase(String homeDirectory) |
| 82 |
throws DatabaseException, FileNotFoundException |
| 83 |
{ |
| 84 |
... |
| 85 |
DatabaseConfig dbConfig = new DatabaseConfig(); |
| 86 |
dbConfig.setTransactional(true); |
| 87 |
dbConfig.setAllowCreate(true); |
| 88 |
dbConfig.setType(DatabaseType.BTREE); |
| 89 |
... |
| 90 |
<strong class="userinput"><code> partDb = env.openDatabase(null, PART_STORE, null, dbConfig); |
| 91 |
supplierDb = env.openDatabase(null, SUPPLIER_STORE, null, |
| 92 |
dbConfig); |
| 93 |
shipmentDb = env.openDatabase(null, SHIPMENT_STORE, null, |
| 94 |
dbConfig); </code></strong> |
| 95 |
... |
| 96 |
} </pre> |
| 97 |
<p> |
| 98 |
The database configuration object that was used previously for |
| 99 |
opening the catalog database is reused for opening the three |
| 100 |
databases above. The databases are created if they don't already |
| 101 |
exist. The parameters of the <code class="methodname">openDatabase()</code> method were |
| 102 |
described earlier when the class catalog database was opened. |
| 103 |
</p> |
| 104 |
<p> |
| 105 |
The following statements close the three databases. |
| 106 |
</p> |
| 107 |
<a id="cb_close2"></a> |
| 108 |
<pre class="programlisting"> public void close() |
| 109 |
throws DatabaseException |
| 110 |
{ |
| 111 |
<strong class="userinput"><code> partDb.close(); |
| 112 |
supplierDb.close(); |
| 113 |
shipmentDb.close();</code></strong> |
| 114 |
javaCatalog.close(); |
| 115 |
env.close(); |
| 116 |
} </pre> |
| 117 |
<p> |
| 118 |
It is recommended that all databases, including the catalog database, is closed |
| 119 |
before closing the environment. |
| 120 |
</p> |
| 121 |
<p> |
| 122 |
The following getter methods return the databases for use by |
| 123 |
other classes in the example program. |
| 124 |
</p> |
| 125 |
<a id="cb_sampledatabase_getters"></a> |
| 126 |
<pre class="programlisting">public class SampleDatabase |
| 127 |
{ |
| 128 |
... |
| 129 |
<strong class="userinput"><code> public final Database getPartDatabase() |
| 130 |
{ |
| 131 |
return partDb; |
| 132 |
} |
| 133 |
|
| 134 |
public final Database getSupplierDatabase() |
| 135 |
{ |
| 136 |
return supplierDb; |
| 137 |
} |
| 138 |
|
| 139 |
public final Database getShipmentDatabase() |
| 140 |
{ |
| 141 |
return shipmentDb; |
| 142 |
}</code></strong> |
| 143 |
... |
| 144 |
}</pre> |
| 145 |
</div> |
| 146 |
<div class="navfooter"> |
| 147 |
<hr /> |
| 148 |
<table width="100%" summary="Navigation footer"> |
| 149 |
<tr> |
| 150 |
<td width="40%" align="left"><a accesskey="p" href="openclasscatalog.html">Prev</a> </td> |
| 151 |
<td width="20%" align="center"> |
| 152 |
<a accesskey="u" href="BasicProgram.html">Up</a> |
| 153 |
</td> |
| 154 |
<td width="40%" align="right"> <a accesskey="n" href="createbindingscollections.html">Next</a></td> |
| 155 |
</tr> |
| 156 |
<tr> |
| 157 |
<td width="40%" align="left" valign="top"> |
| 158 |
Opening and Closing the Class Catalog |
| 159 |
</td> |
| 160 |
<td width="20%" align="center"> |
| 161 |
<a accesskey="h" href="index.html">Home</a> |
| 162 |
</td> |
| 163 |
<td width="40%" align="right" valign="top"> |
| 164 |
Creating Bindings and Collections |
| 165 |
</td> |
| 166 |
</tr> |
| 167 |
</table> |
| 168 |
</div> |
| 169 |
</body> |
| 170 |
</html> |