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>Retrieving Objects from an Entity Store</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="persist_access.html" title="Chapter��5.��Saving and Retrieving Objects" />
11    <link rel="previous" href="simpleput.html" title="Placing Objects in an Entity Store" />
12    <link rel="next" href="getmultiple.html" title="Retrieving Multiple Objects" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Retrieving Objects from an Entity Store</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="simpleput.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��5.��Saving and Retrieving Objects</th>
23          <td width="20%" align="right">��<a accesskey="n" href="getmultiple.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="simpleget"></a>Retrieving Objects from an Entity Store</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <p>
38                    You retrieve objects placed in an entity store by using
39                    either the object's primary index, or the appropriate
40                    secondary index if it exists. The following application
41                    illustrates this by retrieving some of the objects that
42                    we placed in an entity store in the previous section.
43            </p>
44      <p>
45                To begin, we import the Java classes that our example
46                needs. We also instantiate the private data members that we
47                require.
48           </p>
49      <pre class="programlisting">package persist.gettingStarted;
50
51import java.io.File;
52import java.io.FileNotFoundException;
53
54import com.sleepycat.db.DatabaseException;
55import com.sleepycat.db.Environment;
56import com.sleepycat.db.EnvironmentConfig;
57
58import com.sleepycat.persist.EntityStore;
59import com.sleepycat.persist.StoreConfig;
60
61public class SimpleStoreGet {
62
63    private static File envHome = new File("./JEDB");
64
65    private Environment envmnt;
66    private EntityStore store;
67    private SimpleDA sda; </pre>
68      <p>
69            Next we create a method that simply opens our database
70            environment and entity store for us. 
71    </p>
72      <pre class="programlisting">   // The setup() method opens the environment and store
73    // for us.
74    public void setup()
75        throws DatabaseException {
76
77        EnvironmentConfig envConfig = new EnvironmentConfig();
78        StoreConfig storeConfig = new StoreConfig();
79
80        envConfig.setAllowCreate(true);
81        storeConfig.setAllowCreate(true);
82
83        try {
84            // Open the environment and entity store
85            envmnt = new Environment(envHome, envConfig);
86            store = new EntityStore(envmnt, "EntityStore", storeConfig);
87        } catch (FileNotFoundException fnfe) {
88            System.err.println("setup(): " + fnfe.toString());
89            System.exit(-1);
90        }
91    } </pre>
92      <p>
93            We also need a method to close our environment and store.
94    </p>
95      <pre class="programlisting">    // Close our environment and store.
96    public void shutdown()
97        throws DatabaseException {
98
99        store.close();
100        envmnt.close();
101    } </pre>
102      <p>
103            Now we retrieve a few objects. To do this, we instantiate a
104            <tt class="classname">SimpleDA</tt> (see <a href="simpleda.html">SimpleDA.class</a>) class that we use to access
105            our primary and secondary indexes. Then we retrieve objects
106            based on a primary or secondary index value. And finally, we
107            display the retrieved objects.
108     </p>
109      <pre class="programlisting">    // Retrieve some SimpleEntityClass objects from the store.
110    private void run()
111        throws DatabaseException {
112
113        setup();
114
115        // Open the data accessor. This is used to store
116        // persistent objects.
117        sda = new SimpleDA(store);
118
119        // Instantiate and store some entity classes
120        SimpleEntityClass sec1 = sda.pIdx.get("keyone");
121        SimpleEntityClass sec2 = sda.pIdx.get("keytwo");
122
123        SimpleEntityClass sec4 = sda.sIdx.get("skeythree");
124
125        System.out.println("sec1: " + sec1.getpKey());
126        System.out.println("sec2: " + sec2.getpKey());
127        System.out.println("sec4: " + sec4.getpKey());
128
129
130        shutdown();
131    } </pre>
132      <p>
133            Finally, to complete our class, we need a
134            <tt class="methodname">main()</tt> method, which simply calls our
135            <tt class="methodname">run()</tt> method.
136    </p>
137      <pre class="programlisting">    // main
138    public static void main(String args[]) {
139        SimpleStoreGet ssg = new SimpleStoreGet();
140        try {
141            ssg.run();
142        } catch (DatabaseException dbe) {
143            System.err.println("SimpleStoreGet: " + dbe.toString());
144            dbe.printStackTrace();
145        } catch (Exception e) {
146            System.out.println("Exception: " + e.toString());
147            e.printStackTrace();
148        }
149        System.out.println("All done.");
150    }
151
152} </pre>
153    </div>
154    <div class="navfooter">
155      <hr />
156      <table width="100%" summary="Navigation footer">
157        <tr>
158          <td width="40%" align="left"><a accesskey="p" href="simpleput.html">Prev</a>��</td>
159          <td width="20%" align="center">
160            <a accesskey="u" href="persist_access.html">Up</a>
161          </td>
162          <td width="40%" align="right">��<a accesskey="n" href="getmultiple.html">Next</a></td>
163        </tr>
164        <tr>
165          <td width="40%" align="left" valign="top">Placing Objects in an Entity Store��</td>
166          <td width="20%" align="center">
167            <a accesskey="h" href="index.html">Home</a>
168          </td>
169          <td width="40%" align="right" valign="top">��Retrieving Multiple Objects</td>
170        </tr>
171      </table>
172    </div>
173  </body>
174</html>
175