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>Replacing Records Using Cursors</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="Cursors.html" title="Chapter��9.��Using Cursors" /> 11 <link rel="prev" href="DeleteEntryWCursor.html" title="Deleting Records Using Cursors" /> 12 <link rel="next" href="cursorJavaUsage.html" title="Cursor Example" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Replacing Records Using Cursors</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="DeleteEntryWCursor.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��9.��Using Cursors</th> 23 <td width="20%" align="right">��<a accesskey="n" href="cursorJavaUsage.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="ReplacingEntryWCursor"></a>Replacing Records Using Cursors</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 You replace the data for a database record by using 38 39 40 <span> 41 <code class="methodname">Cursor.putCurrent()</code>. 42 </span> 43 44 45 46 </p> 47 <a id="java_cursor9"></a> 48 <pre class="programlisting">import com.sleepycat.db.Cursor; 49import com.sleepycat.db.Database; 50import com.sleepycat.db.DatabaseEntry; 51import com.sleepycat.db.LockMode; 52import com.sleepycat.db.OperationStatus; 53 54... 55Cursor cursor = null; 56Database myDatabase = null; 57try { 58 ... 59 // Database open omitted for brevity 60 ... 61 // Create DatabaseEntry objects 62 // searchKey is some String. 63 DatabaseEntry theKey = new DatabaseEntry(searchKey.getBytes("UTF-8")); 64 DatabaseEntry theData = new DatabaseEntry(); 65 66 // Open a cursor using a database handle 67 cursor = myDatabase.openCursor(null, null); 68 69 // Position the cursor. Ignoring the return value for clarity 70 OperationStatus retVal = cursor.getSearchKey(theKey, theData, 71 LockMode.DEFAULT); 72 73 // Replacement data 74 String replaceStr = "My replacement string"; 75 DatabaseEntry replacementData = 76 new DatabaseEntry(replaceStr.getBytes("UTF-8")); 77 cursor.putCurrent(replacementData); 78} catch (Exception e) { 79 // Exception handling goes here 80} finally { 81 // Make sure to close the cursor 82 cursor.close(); 83}</pre> 84 <p> 85 Note that you cannot change a record's key using this method; the key 86 parameter is always ignored when you replace a record. 87 </p> 88 <p> 89 When replacing the data portion of a record, if you are replacing a 90 record that is a member of a sorted duplicates set, then the replacement 91 will be successful only if the new record sorts identically to the old 92 record. This means that if you are replacing a record that is a member 93 of a sorted duplicates set, and if you are using the default 94 lexicographic sort, then the replacement will fail due to violating the 95 sort order. However, if you 96 provide a custom sort routine that, for example, sorts based on just a 97 few bytes out of the data item, then potentially you can perform 98 a direct replacement and still not violate the restrictions described 99 here. 100 </p> 101 <p> 102 <span>Under these circumstances, if</span> 103 104 you want to replace the data contained by a duplicate record, 105 <span> 106 and you are not using a custom sort routine, then 107 </span> 108 delete the record and create a new record with the desired key and data. 109 </p> 110 </div> 111 <div class="navfooter"> 112 <hr /> 113 <table width="100%" summary="Navigation footer"> 114 <tr> 115 <td width="40%" align="left"><a accesskey="p" href="DeleteEntryWCursor.html">Prev</a>��</td> 116 <td width="20%" align="center"> 117 <a accesskey="u" href="Cursors.html">Up</a> 118 </td> 119 <td width="40%" align="right">��<a accesskey="n" href="cursorJavaUsage.html">Next</a></td> 120 </tr> 121 <tr> 122 <td width="40%" align="left" valign="top">Deleting Records Using Cursors��</td> 123 <td width="20%" align="center"> 124 <a accesskey="h" href="index.html">Home</a> 125 </td> 126 <td width="40%" align="right" valign="top">��Cursor Example</td> 127 </tr> 128 </table> 129 </div> 130 </body> 131</html> 132