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>Using the BIND APIs</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="DBEntry.html" title="Chapter 8. Database Records" />
11    <link rel="previous" href="usingDbt.html" title="Reading and Writing Database Records" />
12    <link rel="next" href="dbtJavaUsage.html" title="Database Usage Example" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Using the BIND APIs</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="usingDbt.html">Prev</a> </td>
22          <th width="60%" align="center">Chapter 8. Database Records</th>
23          <td width="20%" align="right"> <a accesskey="n" href="dbtJavaUsage.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="bindAPI"></a>Using the BIND APIs</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <p>Except for Java String and boolean types, efficiently moving data in
38    and out of Java byte arrays for storage in a database can be a nontrivial
39    operation. To help you with this problem, DB provides the Bind APIs. 
40    While these APIs are described in detail in the
41    
42
43    <span>
44    <i class="citetitle">Berkeley DB Collections Tutorial</i>,
45    </span>
46
47    this section provides a brief introduction to using the Bind APIs with:</p>
48      <div class="itemizedlist">
49        <ul type="disc">
50          <li>
51            <p>Single field numerical and string objects</p>
52            <p>Use this if you want to store a single numerical or string object,
53        such as <tt class="classname">Long</tt>, <tt class="classname">Double</tt>, or
54        <tt class="classname">String</tt>.</p>
55          </li>
56          <li>
57            <p>Complex objects that implement Java serialization.</p>
58            <p>Use this if you are storing objects that implement
59            <tt class="classname">Serializable</tt> and if you do not need to sort them.
60        </p>
61          </li>
62          <li>
63            <p>Non-serialized complex objects.</p>
64            <p>If you are storing objects that do not implement serialization,
65        you can create your own custom tuple bindings. Note that you should
66        use custom tuple bindings even if your objects are serializable if
67        you want to sort on that data.</p>
68          </li>
69        </ul>
70      </div>
71      <div class="sect2" lang="en" xml:lang="en">
72        <div class="titlepage">
73          <div>
74            <div>
75              <h3 class="title"><a id="bindPrimitive"></a>Numerical and String Objects</h3>
76            </div>
77          </div>
78          <div></div>
79        </div>
80        <p>You can use the Bind APIs to store primitive data in a <tt class="classname">DatabaseEntry</tt>
81      object. That is, you can store a single field containing one of the following types:</p>
82        <div class="itemizedlist">
83          <ul type="disc">
84            <li>
85              <p>
86                <tt class="classname">String</tt>
87              </p>
88            </li>
89            <li>
90              <p>
91                <tt class="classname">Character</tt>
92              </p>
93            </li>
94            <li>
95              <p>
96                <tt class="classname">Boolean</tt>
97              </p>
98            </li>
99            <li>
100              <p>
101                <tt class="classname">Byte</tt>
102              </p>
103            </li>
104            <li>
105              <p>
106                <tt class="classname">Short</tt>
107              </p>
108            </li>
109            <li>
110              <p>
111                <tt class="classname">Integer</tt>
112              </p>
113            </li>
114            <li>
115              <p>
116                <tt class="classname">Long</tt>
117              </p>
118            </li>
119            <li>
120              <p>
121                <tt class="classname">Float</tt>
122              </p>
123            </li>
124            <li>
125              <p>
126                <tt class="classname">Double</tt>
127              </p>
128            </li>
129          </ul>
130        </div>
131        <p>
132          To store primitive data using the Bind APIs:
133      </p>
134        <div class="orderedlist">
135          <ol type="1">
136            <li>
137              <p>Create an <tt class="classname">EntryBinding</tt> object.</p>
138              <p>When you do this, you use <tt class="classname">TupleBinding.getPrimitiveBinding()</tt>
139          to return an appropriate binding for the conversion.</p>
140            </li>
141            <li>
142              <p>Use the <tt class="classname">EntryBinding</tt> object to place
143          the numerical object on the <tt class="classname">DatabaseEntry</tt>.</p>
144            </li>
145          </ol>
146        </div>
147        <p>Once the data is stored in the DatabaseEntry, you can put it to
148      the database in whatever manner you wish. For example:</p>
149        <a id="java_dbt6"></a>
150        <pre class="programlisting">package db.GettingStarted;
151
152import com.sleepycat.bind.EntryBinding;
153import com.sleepycat.bind.tuple.TupleBinding;
154import com.sleepycat.db.Database;
155import com.sleepycat.db.DatabaseEntry;
156
157...
158
159Database myDatabase = null;
160// Database open omitted for clarity.
161
162// Need a key for the put.
163try {
164    String aKey = "myLong";
165    DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));    
166
167    // Now build the DatabaseEntry using a TupleBinding
168    Long myLong = new Long(123456789l);
169    DatabaseEntry theData = new DatabaseEntry();
170    EntryBinding myBinding = TupleBinding.getPrimitiveBinding(Long.class);
171    myBinding.objectToEntry(myLong, theData);
172
173    // Now store it
174    myDatabase.put(null, theKey, theData);
175} catch (Exception e) {
176    // Exception handling goes here
177}</pre>
178        <p>Retrieval from the <tt class="classname">DatabaseEntry</tt> object is
179      performed in much the same way:</p>
180        <a id="java_dbt7"></a>
181        <pre class="programlisting">package db.GettingStarted;
182      
183import com.sleepycat.bind.EntryBinding;
184import com.sleepycat.bind.tuple.TupleBinding;
185import com.sleepycat.db.Database;
186import com.sleepycat.db.DatabaseEntry;
187import com.sleepycat.db.LockMode;
188import com.sleepycat.db.OperationStatus;
189
190...
191
192Database myDatabase = null;
193// Database open omitted for clarity
194
195try {
196    // Need a key for the get
197    String aKey = "myLong";
198    DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));
199    
200    // Need a DatabaseEntry to hold the associated data.
201    DatabaseEntry theData = new DatabaseEntry();
202
203    // Bindings need only be created once for a given scope
204    EntryBinding myBinding = TupleBinding.getPrimitiveBinding(Long.class);
205
206    // Get it
207    OperationStatus retVal = myDatabase.get(null, theKey, theData, 
208                                            LockMode.DEFAULT);
209    String retKey = null;
210    if (retVal == OperationStatus.SUCCESS) {
211        // Recreate the data.
212        // Use the binding to convert the byte array contained in theData
213        // to a Long type.
214        Long theLong = (Long) myBinding.entryToObject(theData);
215        retKey = new String(theKey.getData(), "UTF-8");
216        System.out.println("For key: '" + retKey + "' found Long: '" + 
217                            theLong + "'.");
218    } else {
219        System.out.println("No record found for key '" + retKey + "'.");
220    }
221} catch (Exception e) {
222    // Exception handling goes here
223} </pre>
224      </div>
225      <div class="sect2" lang="en" xml:lang="en">
226        <div class="titlepage">
227          <div>
228            <div>
229              <h3 class="title"><a id="object2dbt"></a>Serializable Complex Objects</h3>
230            </div>
231          </div>
232          <div></div>
233        </div>
234        <p>Frequently your application requires you to store and manage
235      objects for your record data and/or keys. You may need to do this if you
236      are caching objects created by another process. You may also want to do
237      this if you want to store multiple data values on a record. When used
238      with just primitive data, or with objects containing a single data member,
239      DB database records effectively represent a single row in a two-column table.
240      By storing a complex object in the record, you can turn each record into
241      a single row in an <span class="emphasis"><em>n</em></span>-column table, where
242      <span class="emphasis"><em>n</em></span> is the number of data members contained by the
243      stored object(s).</p>
244        <p>In order to store objects in a 
245      DB database, you must convert them to and from a <tt class="literal">byte</tt> array.
246      The first instinct for many Java programmers is to do this using Java
247      serialization. While this is functionally a correct solution, the result
248      is poor space-performance because this causes the class information
249      to be stored on every such database record. This information can be quite large
250      and it is redundant — the class information does not vary for serialized objects of the same type.
251      </p>
252        <p>
253        In other words, directly using serialization to place your objects into byte
254        arrays means that you will be storing a great deal of unnecessary information in
255        your database, which ultimately leads to larger databases and more expensive disk
256        I/O.
257      </p>
258        <p>The easiest way for you to solve this problem is to use the Bind
259      APIs to perform the serialization for you. Doing so causes the extra
260      object information to be saved off to a unique <tt class="classname">Database</tt>
261      dedicated for that purpose. This means that you do not have to duplicate
262      that information on each record in the <tt class="classname">Database</tt>
263      that your application is using to store its information.</p>
264        <p>
265        Note that when you use the Bind APIs to perform serialization, you still
266        receive all the benefits of serialization. You can still use arbitrarily
267        complex object graphs, and you still receive built-in class evolution
268        through the serialVersionUID (SUID) scheme. All of the Java
269        serialization rules apply without modification. For example, you can
270        implement Externalizable instead of Serializable.
271      </p>
272        <div class="sect3" lang="en" xml:lang="en">
273          <div class="titlepage">
274            <div>
275              <div>
276                <h4 class="title"><a id="bindCaveats"></a>Usage Caveats</h4>
277              </div>
278            </div>
279            <div></div>
280          </div>
281          <p>Before using the Bind APIs to perform serialization, you may
282        want to consider writing your own custom tuple bindings. Specifically,
283        avoid serialization if:
284        </p>
285          <div class="itemizedlist">
286            <ul type="disc">
287              <li>
288                <p>
289                If you need to sort based on the objects your are storing. 
290                The sort order is meaningless for the byte arrays that you
291                obtain through serialization. Consequently, you should not use serialization for keys if you
292                care about their sort order. You should
293                also not use serialization for record data if your
294                <tt class="classname">Database</tt> supports duplicate records and you care about sort order.
295        </p>
296              </li>
297              <li>
298                <p>
299                You want to minimize the size of your byte arrays. Even when using the Bind APIs to perform the
300                serialization the resulting <tt class="literal">byte</tt> array may be larger than necessary. You can achieve
301                more compact results by building your own custom tuple binding.
302            </p>
303              </li>
304              <li>
305                <p>
306                You want to optimize for speed. In general, custom tuple bindings are faster than serialization at
307                moving data in and out of <tt class="literal">byte</tt> arrays.
308            </p>
309              </li>
310            </ul>
311          </div>
312          <p>
313            For information on building your own custom tuple binding, see <a href="bindAPI.html#customTuple">Custom Tuple Bindings</a>.
314        </p>
315        </div>
316        <div class="sect3" lang="en" xml:lang="en">
317          <div class="titlepage">
318            <div>
319              <div>
320                <h4 class="title"><a id="objectSerial"></a>Serializing Objects</h4>
321              </div>
322            </div>
323            <div></div>
324          </div>
325          <p>To store a serializable complex object using the
326        Bind APIs:</p>
327          <div class="orderedlist">
328            <ol type="1">
329              <li>
330                <p>
331                Implement java.io.Serializable in the class whose instances that
332                you want to store.
333            </p>
334              </li>
335              <li>
336                <p>Open (create) your databases. You need two. The first is the
337            database that you use to store your data. The second is used to
338            store the class information.</p>
339              </li>
340              <li>
341                <p>Instantiate a class catalog. You do this with
342            <tt class="classname">com.sleepycat.bind.serial.StoredClassCatalog</tt>,
343            and at that time you must provide a handle to an open database
344            that is used to store the class information.</p>
345              </li>
346              <li>
347                <p>Create an entry binding that uses <tt class="methodname">com.sleepycat.bind.serial.SerialBinding</tt>.</p>
348              </li>
349              <li>
350                <p>Instantiate an instance of the object that you want to
351            store, and place it in a <tt class="classname">DatabaseEntry</tt>
352            using the entry binding that you created in the previous step.</p>
353              </li>
354            </ol>
355          </div>
356          <p>
357            For example, suppose you want to store a long, double, and a
358            String as a record's data. Then you might create a class that
359            looks something like this:
360        </p>
361          <a id="java_dbt8"></a>
362          <pre class="programlisting">package db.GettingStarted;    
363
364import java.io.Serializable;
365
366public class MyData implements Serializable {
367    private long longData;
368    private double doubleData;
369    private String description;
370
371    MyData() {
372        longData = 0;
373        doubleData = 0.0;
374        description = null;
375    }
376
377    public void setLong(long data) {
378        longData = data;
379    }
380
381    public void setDouble(double data) {
382        doubleData = data;
383    }
384
385    public void setDescription(String data) {
386        description = data;
387    }
388
389    public long getLong() {
390        return longData;
391    }
392
393    public double getDouble() {
394        return doubleData;
395    }
396
397    public String getDescription() {
398        return description;
399    }
400}</pre>
401          <p>You can then store instances of this class as follows:</p>
402          <a id="java_dbt9"></a>
403          <pre class="programlisting">package db.GettingStarted;
404
405import com.sleepycat.bind.EntryBinding;
406import com.sleepycat.bind.serial.StoredClassCatalog;
407import com.sleepycat.bind.serial.SerialBinding;
408import com.sleepycat.db.Database;
409import com.sleepycat.db.DatabaseConfig;
410import com.sleepycat.db.DatabaseEntry;
411import com.sleepycat.db.DatabaseType;
412...
413
414// The key data.
415String aKey = "myData";
416
417// The data data
418MyData data2Store = new MyData();
419data2Store.setLong(123456789l);
420data2Store.setDouble(1234.9876543);
421data2Store.setDescription("A test instance of this class");
422
423try {
424    // Open the database that you will use to store your data
425    DatabaseConfig myDbConfig = new DatabaseConfig();
426    myDbConfig.setAllowCreate(true);
427    myDbConfig.setSortedDuplicates(true);
428    myDbConfig.setType(DatabaseType.BTREE);
429    Database myDatabase = new Database("myDb", null, myDbConfig);
430
431    // Open the database that you use to store your class information.
432    // The db used to store class information does not require duplicates
433    // support.
434    myDbConfig.setSortedDuplicates(false);
435    Database myClassDb = new Database("classDb", null, myDbConfig); 
436
437    // Instantiate the class catalog
438    StoredClassCatalog classCatalog = new StoredClassCatalog(myClassDb);
439
440    // Create the binding
441    EntryBinding dataBinding = new SerialBinding(classCatalog, 
442                                                 MyData.class);
443
444    // Create the DatabaseEntry for the key
445    DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));
446
447    // Create the DatabaseEntry for the data. Use the EntryBinding object
448    // that was just created to populate the DatabaseEntry
449    DatabaseEntry theData = new DatabaseEntry();
450    dataBinding.objectToEntry(data2Store, theData);
451
452    // Put it as normal
453    myDatabase.put(null, theKey, theData);
454    
455    // Database and environment close omitted for brevity 
456} catch (Exception e) {
457    // Exception handling goes here
458}</pre>
459        </div>
460        <div class="sect3" lang="en" xml:lang="en">
461          <div class="titlepage">
462            <div>
463              <div>
464                <h4 class="title"><a id="objectDeserial"></a>Deserializing Objects</h4>
465              </div>
466            </div>
467            <div></div>
468          </div>
469          <p>Once an object is stored in the database, you can retrieve the
470        <tt class="classname">MyData</tt> objects from the retrieved
471        <tt class="classname">DatabaseEntry</tt> using the Bind APIs in much the
472        same way as is described above. For example:</p>
473          <a id="java_dbt10"></a>
474          <pre class="programlisting">package db.GettingStarted;
475
476import com.sleepycat.bind.EntryBinding;
477import com.sleepycat.bind.serial.StoredClassCatalog;
478import com.sleepycat.bind.serial.SerialBinding;
479import com.sleepycat.db.Database;
480import com.sleepycat.db.DatabaseConfig;
481import com.sleepycat.db.DatabaseEntry;
482import com.sleepycat.db.DatabaseType;
483import com.sleepycat.db.LockMode;
484
485...
486
487// The key data.
488String aKey = "myData";
489
490try {
491    // Open the database that stores your data
492    DatabaseConfig myDbConfig = new DatabaseConfig();
493    myDbConfig.setAllowCreate(false);
494    myDbConfig.setType(DatabaseType.BTREE);
495    Database myDatabase = new Database("myDb", null, myDbConfig);
496
497    // Open the database that stores your class information.
498    Database myClassDb = new Database("classDb", null, myDbConfig); 
499
500    // Instantiate the class catalog
501    StoredClassCatalog classCatalog = new StoredClassCatalog(myClassDb);
502
503    // Create the binding
504    EntryBinding dataBinding = new SerialBinding(classCatalog, 
505                                                 MyData.class);
506
507    // Create DatabaseEntry objects for the key and data
508    DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));
509    DatabaseEntry theData = new DatabaseEntry();
510
511    // Do the get as normal
512    myDatabase.get(null, theKey, theData, LockMode.DEFAULT);
513
514    // Recreate the MyData object from the retrieved DatabaseEntry using
515    // the EntryBinding created above
516    MyData retrievedData = (MyData) dataBinding.entryToObject(theData);
517 
518    // Database and environment close omitted for brevity
519} catch (Exception e) {
520    // Exception handling goes here
521}</pre>
522        </div>
523      </div>
524      <div class="sect2" lang="en" xml:lang="en">
525        <div class="titlepage">
526          <div>
527            <div>
528              <h3 class="title"><a id="customTuple"></a>Custom Tuple Bindings</h3>
529            </div>
530          </div>
531          <div></div>
532        </div>
533        <p>
534        If you want to store complex objects in your database, then you can use
535        tuple bindings to do this. While they are more work to write and
536        maintain than if you were to use serialization, the
537        <tt class="literal">byte</tt> array conversion is faster. In addition, custom
538        tuple bindings should allow you to create <tt class="literal">byte</tt> arrays
539        that are smaller than those created by serialization. Custom tuple
540        bindings also allow you to optimize your BTree comparisons, whereas
541        serialization does not.
542      </p>
543        <p>
544        For information on using serialization to store complex objects, see
545        <a href="bindAPI.html#object2dbt">Serializable Complex Objects</a>.
546      </p>
547        <p>To store complex objects using a custom tuple binding:</p>
548        <div class="orderedlist">
549          <ol type="1">
550            <li>
551              <p>Implement the class whose instances that you want to store.
552          Note that you do not have to implement the Serializable interface.</p>
553            </li>
554            <li>
555              <p>Write a tuple binding using the <tt class="classname">com.sleepycat.bind.tuple.TupleBinding</tt>
556          class.</p>
557            </li>
558            <li>
559              <p>Open (create) your database. Unlike serialization, you only
560          need one.</p>
561            </li>
562            <li>
563              <p>Create an entry binding that uses the tuple binding that you
564          implemented in step 2.</p>
565            </li>
566            <li>
567              <p>Instantiate an instance of the object that you want to store,
568          and place it in a <tt class="classname">DatabaseEntry</tt> using the
569          entry binding that you created in the previous step.</p>
570            </li>
571          </ol>
572        </div>
573        <p>
574        For example, suppose you want to your keys to be instances of the
575        following class:
576      </p>
577        <a id="java_dbt11"></a>
578        <pre class="programlisting">package db.GettingStarted;
579
580public class MyData2 {
581    private long longData;
582    private Double doubleData;
583    private String description;
584
585    public MyData2() {
586        longData = 0;
587        doubleData = new Double(0.0);
588        description = "";
589    }
590
591    public void setLong(long data) {
592        longData = data;
593    }
594
595    public void setDouble(Double data) {
596        doubleData = data;
597    }
598
599    public void setString(String data) {
600        description = data;
601    }
602
603    public long getLong() {
604        return longData;
605    }
606
607    public Double getDouble() {
608        return doubleData;
609    }
610
611    public String getString() {
612        return description;
613    }
614} </pre>
615        <p>In this case, you need to write a tuple binding for the
616      <tt class="classname">MyData2</tt> class. When you do this, you must
617      implement the <tt class="methodname">TupleBinding.objectToEntry()</tt>
618      and <tt class="methodname">TupleBinding.entryToObject()</tt> abstract methods.
619      Remember the following as you implement these methods:</p>
620        <div class="itemizedlist">
621          <ul type="disc">
622            <li>
623              <p>You use <tt class="methodname">TupleBinding.objectToEntry()</tt> to convert
624          objects to <tt class="literal">byte</tt> arrays. You use
625          <tt class="classname">com.sleepycat.bind.tuple.TupleOutput</tt> to write
626          primitive data types to the <tt class="literal">byte</tt> array. Note that
627          <tt class="classname">TupleOutput</tt> provides methods that allows
628          you to work with numerical types (<tt class="literal">long</tt>,
629          <tt class="literal">double</tt>, <tt class="literal">int</tt>, and so forth) and
630          not the corresponding <tt class="literal">java.lang</tt> numerical
631          classes.</p>
632            </li>
633            <li>
634              <p>The order that you write data to the <tt class="literal">byte</tt>
635          array in <tt class="methodname">TupleBinding.objectToEntry()</tt> is the order that
636          it appears in the array. So given the <tt class="classname">MyData2</tt>
637          class as an example, if you write <tt class="literal">description</tt>,
638          <tt class="literal">doubleData</tt>, and then <tt class="literal">longData</tt>,
639          then the resulting byte array will contain these data elements in
640          that order. This means that your records will sort based on the
641          value of the <tt class="literal">description</tt> data member and then
642          the <tt class="literal">doubleData</tt> member, and so forth. If you
643          prefer to sort based on, say, the <tt class="literal">longData</tt> data
644          member, write it to the byte array first.</p>
645            </li>
646            <li>
647              <p>You use <tt class="methodname">TupleBinding.entryToObject()</tt> to convert
648          the <tt class="literal">byte</tt> array back into an instance of your
649          original class. You use <tt class="classname">com.sleepycat.bind.tuple.TupleInput</tt>
650          to get data from the <tt class="literal">byte</tt> array.</p>
651            </li>
652            <li>
653              <p>The order that you read data from the <tt class="literal">byte</tt>
654          array must be exactly the same as the order in which it was written.</p>
655            </li>
656          </ul>
657        </div>
658        <p>For example:</p>
659        <a id="java_dbt12"></a>
660        <pre class="programlisting">package db.GettingStarted;
661
662import com.sleepycat.bind.tuple.TupleBinding;
663import com.sleepycat.bind.tuple.TupleInput;
664import com.sleepycat.bind.tuple.TupleOutput;
665
666public class MyTupleBinding extends TupleBinding {
667
668    // Write a MyData2 object to a TupleOutput
669    public void objectToEntry(Object object, TupleOutput to) {
670
671        MyData2 myData = (MyData2)object;
672
673        // Write the data to the TupleOutput (a DatabaseEntry).
674        // Order is important. The first data written will be
675        // the first bytes used by the default comparison routines.
676        to.writeDouble(myData.getDouble().doubleValue());
677        to.writeLong(myData.getLong());
678        to.writeString(myData.getString());
679    }
680
681    // Convert a TupleInput to a MyData2 object
682    public Object entryToObject(TupleInput ti) {
683
684        // Data must be read in the same order that it was
685        // originally written.
686        Double theDouble = new Double(ti.readDouble());
687        long theLong = ti.readLong();
688        String theString = ti.readString();
689
690        MyData2 myData = new MyData2();
691        myData.setDouble(theDouble);
692        myData.setLong(theLong);
693        myData.setString(theString);
694
695        return myData;
696    }
697} </pre>
698        <p>In order to use the tuple binding, instantiate the binding and
699      then use:</p>
700        <div class="itemizedlist">
701          <ul type="disc">
702            <li>
703              <p><tt class="methodname">MyTupleBinding.objectToEntry()</tt> to
704          convert a MyData2 object to a <tt class="classname">DatabaseEntry</tt>.</p>
705            </li>
706            <li>
707              <p><tt class="methodname">MyTupleBinding.entryToObject()</tt> to convert
708          a <tt class="classname">DatabaseEntry</tt> to a <tt class="classname">MyData2</tt>
709          object.</p>
710            </li>
711          </ul>
712        </div>
713        <p>For example:</p>
714        <a id="java_dbt13"></a>
715        <pre class="programlisting">package db.GettingStarted;
716
717import com.sleepycat.bind.tuple.TupleBinding;
718import com.sleepycat.db.DatabaseEntry;
719 
720...
721
722TupleBinding keyBinding = new MyTupleBinding();
723
724MyData2 theKeyData = new MyData2();
725theKeyData.setLong(123456789l);
726theKeyData.setDouble(new Double(12345.6789));
727theKeyData.setString("My key data");
728
729DatabaseEntry myKey = new DatabaseEntry();
730
731try {
732    // Store theKeyData in the DatabaseEntry
733    keyBinding.objectToEntry(theKeyData, myKey);
734
735    ...
736    // Database put and get activity omitted for clarity
737    ...
738
739    // Retrieve the key data
740    theKeyData = (MyData2) keyBinding.entryToObject(myKey);
741} catch (Exception e) {
742    // Exception handling goes here
743}</pre>
744      </div>
745    </div>
746    <div class="navfooter">
747      <hr />
748      <table width="100%" summary="Navigation footer">
749        <tr>
750          <td width="40%" align="left"><a accesskey="p" href="usingDbt.html">Prev</a> </td>
751          <td width="20%" align="center">
752            <a accesskey="u" href="DBEntry.html">Up</a>
753          </td>
754          <td width="40%" align="right"> <a accesskey="n" href="dbtJavaUsage.html">Next</a></td>
755        </tr>
756        <tr>
757          <td width="40%" align="left" valign="top">Reading and Writing Database Records </td>
758          <td width="20%" align="center">
759            <a accesskey="h" href="index.html">Home</a>
760          </td>
761          <td width="40%" align="right" valign="top"> Database Usage Example</td>
762        </tr>
763      </table>
764    </div>
765  </body>
766</html>
767