Using Secondary Cursors

Just like cursors on a primary database, you can use secondary cursors to iterate over the records in a secondary database. Like normal cursors, you can also use secondary cursors to search for specific records in a database, to seek to the first or last record in the database, to get the next duplicate record, and so forth. For a complete description on cursors and their capabilities, see Using Cursors.

However, when you use secondary cursors:

For example, suppose you are using the databases, classes, and key creators described in Implementing Key Creators . Then the following searches for a person's name in the secondary database, and deletes all secondary and primary records that use that name.

package db.GettingStarted;

import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.LockMode;
import com.sleepycat.db.OperationStatus;
import com.sleepycat.db.SecondaryDatabase;
import com.sleepycat.db.SecondaryCursor;
try {
    SecondaryDatabase mySecondaryDatabase = null;
    // Database opens omitted for brevity

    String secondaryName = "John Doe";
    DatabaseEntry secondaryKey = 
        new DatabaseEntry(secondaryName.getBytes("UTF-8"));

    DatabaseEntry foundData = new DatabaseEntry();

    SecondaryCursor mySecCursor = 
        mySecondaryDatabase.openSecondaryCursor(null, null);

    OperationStatus retVal = mySecCursor.getSearchKey(secondaryKey, 
    while (retVal == OperationStatus.SUCCESS) {
        retVal = mySecCursor.getNextDup(secondaryKey, 
} catch (Exception e) {
    // Exception handling goes here