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>Reading Secondary Databases</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="indexes.html" title="Chapter 10. Secondary Databases" /> 11 <link rel="prev" href="secondaryProps.html" title="Secondary Database Properties" /> 12 <link rel="next" href="secondaryDelete.html" title="Deleting Secondary 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">Reading Secondary Databases</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="secondaryProps.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 10. Secondary Databases</th> 23 <td width="20%" align="right"> <a accesskey="n" href="secondaryDelete.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="readSecondary"></a>Reading Secondary Databases</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 Like a primary database, you can read records from your secondary 38 database either by using the 39 <span> 40 <code class="methodname">SecondaryDatabase.get()</code> method, 41 </span> 42 43 or by using 44 <span>a <code class="classname">SecondaryCursor</code>.</span> 45 46 47 The main difference between reading secondary and primary databases is that when 48 you read a secondary database record, the secondary record's data is not 49 returned to you. Instead, the primary key and data corresponding to the 50 secondary key are returned to you. 51 </p> 52 <p> 53 For example, assuming your secondary database contains keys related 54 to a person's full name: 55 </p> 56 <a id="java_index6"></a> 57 <pre class="programlisting">package db.GettingStarted; 58 59import com.sleepycat.db.DatabaseEntry; 60import com.sleepycat.db.LockMode; 61import com.sleepycat.db.OperationStatus; 62import com.sleepycat.db.SecondaryDatabase; 63 64... 65SecondaryDatabase mySecondaryDatabase = null; 66try { 67 // Omitting all database opens 68 ... 69 70 String searchName = "John Doe"; 71 DatabaseEntry searchKey = 72 new DatabaseEntry(searchName.getBytes("UTF-8")); 73 DatabaseEntry primaryKey = new DatabaseEntry(); 74 DatabaseEntry primaryData = new DatabaseEntry(); 75 76 // Get the primary key and data for the user 'John Doe'. 77 OperationStatus retVal = mySecondaryDatabase.get(null, searchKey, 78 primaryKey, 79 primaryData, 80 LockMode.DEFAULT); 81} catch (Exception e) { 82 // Exception handling goes here 83}</pre> 84 <p> 85 Note that, just like 86 <span><code class="methodname">Database.get()</code>, </span> 87 88 89 if your secondary database supports duplicate records then 90 <code class="methodname">SecondaryDatabase.get()</code> 91 92 only return the first record found in a matching duplicates set. If you 93 want to see all the records related to a specific secondary key, then use a 94 <span> 95 <code class="classname">SecondaryCursor</code> (described in <a class="xref" href="secondaryCursor.html" title="Using Secondary Cursors"> 96 <span>Using Secondary Cursors</span> 97 98 </a>). 99 </span> 100 101 </p> 102 </div> 103 <div class="navfooter"> 104 <hr /> 105 <table width="100%" summary="Navigation footer"> 106 <tr> 107 <td width="40%" align="left"><a accesskey="p" href="secondaryProps.html">Prev</a> </td> 108 <td width="20%" align="center"> 109 <a accesskey="u" href="indexes.html">Up</a> 110 </td> 111 <td width="40%" align="right"> <a accesskey="n" href="secondaryDelete.html">Next</a></td> 112 </tr> 113 <tr> 114 <td width="40%" align="left" valign="top">Secondary Database Properties </td> 115 <td width="20%" align="center"> 116 <a accesskey="h" href="index.html">Home</a> 117 </td> 118 <td width="40%" align="right" valign="top"> Deleting Secondary Database Records</td> 119 </tr> 120 </table> 121 </div> 122 </body> 123</html> 124