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