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>Database Example</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="DB.html" title="Chapter��7.��Databases" /> 11 <link rel="prev" href="CoreEnvUsage.html" title="Managing Databases in Environments" /> 12 <link rel="next" href="DBEntry.html" title="Chapter��8.��Database Records" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Database Example</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��7.��Databases</th> 23 <td width="20%" align="right">��<a accesskey="n" href="DBEntry.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="CoreJavaUsage"></a>Database Example</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 Throughout this book we will build a couple of applications that load 38 and retrieve inventory data from DB databases. While we are not yet ready to 39 begin reading from or writing to our databases, we can at least create 40 the class that we will use to manage our databases. 41 </p> 42 <p> 43 Note that subsequent examples in this book will build on this code to 44 perform the more interesting work of writing to and reading from the 45 databases. 46 </p> 47 <p> 48 Note that you can find the complete implementation of these functions 49 in: 50 </p> 51 <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_java/db/GettingStarted</pre> 52 <p> 53 where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you 54 placed your DB distribution. 55 </p> 56 <div class="example"> 57 <a id="MyDb"></a> 58 <p class="title"> 59 <b>Example 7.1 MyDbs Class</b> 60 </p> 61 <div class="example-contents"> 62 <p> 63 To manage our database open and close activities, we encapsulate them 64 in the <code class="classname">MyDbs</code> class. There are several good reasons 65 to do this, the most important being that we can ensure our databases are 66 closed by putting that activity in the <code class="classname">MyDbs</code> 67 class destructor. 68 </p> 69 <p> 70 To begin, we import some needed classes: 71 </p> 72 <a id="java_db12"></a> 73 <pre class="programlisting">// File: MyDbs.java 74package db.GettingStarted; 75 76import com.sleepycat.db.Database; 77import com.sleepycat.db.DatabaseConfig; 78import com.sleepycat.db.DatabaseException; 79import com.sleepycat.db.DatabaseType; 80 81import java.io.FileNotFoundException; </pre> 82 <p> 83 And then we write our class declaration and provided some necessary private data members: 84</p> 85 <a id="java_db13"></a> 86 <pre class="programlisting">public class MyDbs { 87 88 // The databases that our application uses 89 private Database vendorDb = null; 90 private Database inventoryDb = null; 91 92 private String vendordb = "VendorDB.db"; 93 private String inventorydb = "InventoryDB.db"; 94 95 // Our constructor does nothing 96 public MyDbs() {} </pre> 97 <p> 98 Next we need a <code class="methodname">setup()</code> method. This is where 99 we configure and open our databases. 100</p> 101 <a id="java_db14"></a> 102 <pre class="programlisting"> // The setup() method opens all our databases 103 // for us. 104 public void setup(String databasesHome) 105 throws DatabaseException { 106 107 DatabaseConfig myDbConfig = new DatabaseConfig(); 108 109 myDbConfig.setErrorStream(System.err); 110 myDbConfig.setErrorPrefix("MyDbs"); 111 myDbConfig.setType(DatabaseType.BTREE); 112 myDbConfig.setAllowCreate(true); 113 114 // Now open, or create and open, our databases 115 // Open the vendors and inventory databases 116 try { 117 vendordb = databasesHome + "/" + vendordb; 118 vendorDb = new Database(vendordb, 119 null, 120 myDbConfig); 121 122 inventorydb = databasesHome + "/" + inventorydb; 123 inventoryDb = new Database(inventorydb, 124 null, 125 myDbConfig); 126 } catch(FileNotFoundException fnfe) { 127 System.err.println("MyDbs: " + fnfe.toString()); 128 System.exit(-1); 129 } 130 } </pre> 131 <p> 132 Finally, we provide some getter methods, and our <code class="methodname">close()</code> method. 133</p> 134 <a id="java_db15"></a> 135 <pre class="programlisting"> // getter methods 136 public Database getVendorDB() { 137 return vendorDb; 138 } 139 140 public Database getInventoryDB() { 141 return inventoryDb; 142 } 143 144 // Close the databases 145 public void close() { 146 try { 147 if (vendorDb != null) { 148 vendorDb.close(); 149 } 150 151 if (inventoryDb != null) { 152 inventoryDb.close(); 153 } 154 } catch(DatabaseException dbe) { 155 System.err.println("Error closing MyDbs: " + 156 dbe.toString()); 157 System.exit(-1); 158 } 159 } 160} </pre> 161 </div> 162 </div> 163 <br class="example-break" /> 164 </div> 165 <div class="navfooter"> 166 <hr /> 167 <table width="100%" summary="Navigation footer"> 168 <tr> 169 <td width="40%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a>��</td> 170 <td width="20%" align="center"> 171 <a accesskey="u" href="DB.html">Up</a> 172 </td> 173 <td width="40%" align="right">��<a accesskey="n" href="DBEntry.html">Next</a></td> 174 </tr> 175 <tr> 176 <td width="40%" align="left" valign="top">Managing Databases in Environments��</td> 177 <td width="20%" align="center"> 178 <a accesskey="h" href="index.html">Home</a> 179 </td> 180 <td width="40%" align="right" valign="top">��Chapter��8.��Database Records</td> 181 </tr> 182 </table> 183 </div> 184 </body> 185</html> 186