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