je/docs/GettingStartedGuide/dbUsage.html

204 lines
8.3 KiB
HTML
Raw Permalink Normal View History

2021-06-06 17:46:45 +00:00
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Database Example</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Getting Started with Berkeley DB Java Edition" />
<link rel="up" href="databases.html" title="Chapter 7. Databases" />
<link rel="prev" href="DBAdmin.html" title="Administrative Methods" />
<link rel="next" href="DBEntry.html" title="Chapter 8. Database Records" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 12.2.7.5</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Database Example</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="DBAdmin.html">Prev</a> </td>
<th width="60%" align="center">Chapter 7. Databases</th>
<td width="20%" align="right"> <a accesskey="n" href="DBEntry.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="dbUsage"></a>Database Example</h2>
</div>
</div>
</div>
<p>In <a class="xref" href="dbenvUsageExample.html" title="Database Environment Management Example">Database Environment Management Example</a> we created a class that manages an
<code class="classname">Environment</code>. We now extend that class to allow it
to open and manage multiple databases. Again, remember that you can find
this class in:
</p>
<pre class="programlisting"><span class="emphasis"><em>JE_HOME</em></span>/je/gettingStarted/MyDbEnv.java</pre>
<p>
where <code class="literal"><span class="emphasis"><em>JE_HOME</em></span></code> is the location where you
placed your JE distribution.
</p>
<div class="example">
<a id="mydbenvWDatabase"></a>
<p class="title">
<b>Example 7.1 Database Management with MyDbEnv</b>
</p>
<div class="example-contents">
<p>First, we need to import a few additional classes, and setup some global variables to support databases.
The databases that we are configuring and creating here are used by applications developed in examples later in this guide.</p>
<a id="je_db10"></a>
<pre class="programlisting">// File MyDbEnv.java
package je.gettingStarted;
import com.sleepycat.je.DatabaseException;
<strong class="userinput"><code>import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.Database; </code></strong>
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.Environment;
import java.io.File;
public class MyDbEnv {
private Environment myEnv;
<strong class="userinput"><code>private Database vendorDb;
private Database inventoryDb;</code></strong>
public MyDbEnv() {} </pre>
<p>
Next we need to update the <code class="methodname">MyDbEnv.setup()</code> method to instantiate a
<code class="classname">DatabaseConfig</code> object. We also need to set some properties on that object. These property
values are determined by the value of the <code class="literal">readOnly</code> parameter. We want our databases to be
read-only if the environment is also read-only. We also want to allow our databases to be created if the databases
are not read-only.
</p>
<a id="je_db11"></a>
<pre class="programlisting"> public void setup(File envHome, boolean readOnly)
throws DatabaseException {
// Instantiate an environment <strong class="userinput"><code>and database</code></strong> configuration object
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
<strong class="userinput"><code>DatabaseConfig myDbConfig = new DatabaseConfig();</code></strong>
// Configure the environment <strong class="userinput"><code>and databases</code></strong> for the read-only
// state as identified by the readOnly parameter on this
// method call.
myEnvConfig.setReadOnly(readOnly);
<strong class="userinput"><code>myDbConfig.setReadOnly(readOnly);</code></strong>
// If the environment is opened for write, then we want to be
// able to create the environment <strong class="userinput"><code>and databases</code></strong> if
// they do not exist.
myEnvConfig.setAllowCreate(!readOnly);
<strong class="userinput"><code>myDbConfig.setAllowCreate(!readOnly);</code></strong>
// Instantiate the Environment. This opens it and also possibly
// creates it.
myEnv = new Environment(envHome, myEnvConfig);
<strong class="userinput"><code>// Now create and open our databases.
vendorDb = myEnv.openDatabase(null,
"VendorDB",
myDbConfig);
inventoryDb = myEnv.openDatabase(null,
"InventoryDB",
myDbConfig);</code></strong>
} </pre>
<p>
Next we need some additional getter methods used to return our database handles.
</p>
<a id="je_db12"></a>
<pre class="programlisting"> // Getter methods
public Environment getEnvironment() {
return myEnv;
}
<strong class="userinput"><code>public Database getVendorDB() {
return vendorDb;
}
public Database getInventoryDB() {
return inventoryDb;
}</code></strong> </pre>
<p>
Finally, we need to update the <code class="methodname">MyDbEnv.close()</code> method to close our databases.
</p>
<a id="je_db13"></a>
<pre class="programlisting"> // Close the environment
public void close() {
if (myEnv != null) {
try {
<strong class="userinput"><code>vendorDb.close();
inventoryDb.close();</code></strong>
myEnv.close();
} catch(DatabaseException dbe) {
System.err.println("Error closing MyDbEnv: " +
dbe.toString());
System.exit(-1);
}
}
}
}</pre>
<p>We can now use <code class="classname">MyDbEnv</code> to open and close
both database environments and databases from the appropriate place in
our application. For example:</p>
<a id="je_db14"></a>
<pre class="programlisting">package je.gettingStarted;
import com.sleepycat.je.DatabaseException;
<strong class="userinput"><code>import com.sleepycat.je.Database;</code></strong>
import java.io.File;
...
MyDbEnv exampleDbEnv = new MyDbEnv();
try {
exampleDbEnv.setup(new File("/directory/currently/exists"), true);
<strong class="userinput"><code>Database vendorDb = exampleDbEnv.getVendorDB();
Database inventoryDB = exampleDbEnv.getInventoryDB();</code></strong>
...
} catch(DatabaseException dbe) {
// Error code goes here
} finally {
exampleDbEnv.close();
} </pre>
</div>
</div>
<br class="example-break" />
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="DBAdmin.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="databases.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="DBEntry.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Administrative Methods </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Chapter 8. Database Records</td>
</tr>
</table>
</div>
</body>
</html>