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