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> 7 Opening and Closing Databases 8 </title> 9 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 10 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 11 <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" /> 12 <link rel="up" href="BasicProgram.html" title="Chapter 2. 		The Basic Program 	" /> 13 <link rel="previous" href="openclasscatalog.html" title=" 		Opening and Closing the Class Catalog 	" /> 14 <link rel="next" href="createbindingscollections.html" title=" 		Creating Bindings and Collections 	" /> 15 </head> 16 <body> 17 <div class="navheader"> 18 <table width="100%" summary="Navigation header"> 19 <tr> 20 <th colspan="3" align="center"> 21 Opening and Closing Databases 22 </th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="openclasscatalog.html">Prev</a> </td> 26 <th width="60%" align="center">Chapter 2. 27 The Basic Program 28 </th> 29 <td width="20%" align="right"> <a accesskey="n" href="createbindingscollections.html">Next</a></td> 30 </tr> 31 </table> 32 <hr /> 33 </div> 34 <div class="sect1" lang="en" xml:lang="en"> 35 <div class="titlepage"> 36 <div> 37 <div> 38 <h2 class="title" style="clear: both"><a id="opendatabases"></a> 39 Opening and Closing Databases 40 </h2> 41 </div> 42 </div> 43 <div></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 <tt class="classname">SampleDatabase</tt> 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<b class="userinput"><tt> 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";</tt></b> 66 ... 67<b class="userinput"><tt> private Database supplierDb; 68 private Database partDb; 69 private Database shipmentDb;</tt></b> 70 ... 71} </pre> 72 <p> 73 For each database there is a database name constant and a 74 <tt class="classname">Database</tt> object. 75</p> 76 <p> 77 The following statements open the three databases by 78 constructing a Database 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<b class="userinput"><tt> partDb = env.openDatabase(null, PART_STORE, null, dbConfig); 91 supplierDb = env.openDatabase(null, SUPPLIER_STORE, null, dbConfig); 92 shipmentDb = env.openDatabase(null, SHIPMENT_STORE, null, dbConfig);</tt></b> 93 ... 94 } </pre> 95 <p> 96 The database configuration object that was used previously for 97 opening the catalog database is reused for opening the three 98 databases above. The databases are created if they don't already 99 exist. The parameters of the <tt class="methodname">openDatabase()</tt> method were 100 described earlier when the class catalog database was opened. 101</p> 102 <p> 103 The following statements close the three databases. 104</p> 105 <a id="cb_close2"></a> 106 <pre class="programlisting"> public void close() 107 throws DatabaseException 108 { 109<b class="userinput"><tt> partDb.close(); 110 supplierDb.close(); 111 shipmentDb.close();</tt></b> 112 javaCatalog.close(); 113 env.close(); 114 } </pre> 115 <p> 116 All databases, including the catalog database, must be closed 117 before closing the environment. 118</p> 119 <p> 120 The following getter methods return the databases for use by 121 other classes in the example program. 122</p> 123 <a id="cb_sampledatabase_getters"></a> 124 <pre class="programlisting">public class SampleDatabase 125{ 126 ... 127<b class="userinput"><tt> public final Database getPartDatabase() 128 { 129 return partDb; 130 } 131 132 public final Database getSupplierDatabase() 133 { 134 return supplierDb; 135 } 136 137 public final Database getShipmentDatabase() 138 { 139 return shipmentDb; 140 }</tt></b> 141 ... 142}</pre> 143 </div> 144 <div class="navfooter"> 145 <hr /> 146 <table width="100%" summary="Navigation footer"> 147 <tr> 148 <td width="40%" align="left"><a accesskey="p" href="openclasscatalog.html">Prev</a> </td> 149 <td width="20%" align="center"> 150 <a accesskey="u" href="BasicProgram.html">Up</a> 151 </td> 152 <td width="40%" align="right"> <a accesskey="n" href="createbindingscollections.html">Next</a></td> 153 </tr> 154 <tr> 155 <td width="40%" align="left" valign="top"> 156 Opening and Closing the Class Catalog 157 </td> 158 <td width="20%" align="center"> 159 <a accesskey="h" href="index.html">Home</a> 160 </td> 161 <td width="40%" align="right" valign="top"> 162 Creating Bindings and Collections 163 </td> 164 </tr> 165 </table> 166 </div> 167 </body> 168</html> 169