Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

SequenceExample.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: SequenceExample.java,v 12.2 2005/06/16 20:22:47 bostic Exp $
00008  */
00009 
00010 package db;
00011 
00012 import com.sleepycat.db.*;
00013 import java.io.File;
00014 import java.io.FileNotFoundException;
00015 import java.io.IOException;
00016 import java.io.PrintStream;
00017 
00018 class SequenceExample {
00019     private static final int EXIT_SUCCESS = 0;
00020     private static final int EXIT_FAILURE = 1;
00021 
00022     public SequenceExample() {
00023     }
00024 
00025     public static void usage() {
00026         System.out.println("usage: java " +
00027                "db.SequenceExample [-r] [database]\n");
00028         System.exit(EXIT_FAILURE);
00029     }
00030 
00031     public static void main(String[] argv) {
00032         boolean removeExistingDatabase = false;
00033         String databaseName = "access.db";
00034 
00035         for (int i = 0; i < argv.length; i++) {
00036             if (argv[i].equals("-r"))
00037                 removeExistingDatabase = true;
00038             else if (argv[i].equals("-?"))
00039                 usage();
00040             else if (argv[i].startsWith("-"))
00041                 usage();
00042             else {
00043                 if ((argv.length - i) != 1)
00044                     usage();
00045                 databaseName = argv[i];
00046                 break;
00047             }
00048         }
00049 
00050         try {
00051             SequenceExample app = new SequenceExample();
00052             app.run(removeExistingDatabase, databaseName);
00053         } catch (DatabaseException dbe) {
00054             System.err.println("SequenceExample: " + dbe.toString());
00055             System.exit(EXIT_FAILURE);
00056         } catch (FileNotFoundException fnfe) {
00057             System.err.println("SequenceExample: " + fnfe.toString());
00058             System.exit(EXIT_FAILURE);
00059         }
00060         System.exit(EXIT_SUCCESS);
00061     }
00062 
00063     public void run(boolean removeExistingDatabase, String databaseName)
00064         throws DatabaseException, FileNotFoundException {
00065 
00066         // Remove the previous database.
00067         if (removeExistingDatabase)
00068             new File(databaseName).delete();
00069 
00070         // Create the database object.
00071         // There is no environment for this simple example.
00072         DatabaseConfig dbConfig = new DatabaseConfig();
00073         dbConfig.setErrorStream(System.err);
00074         dbConfig.setErrorPrefix("SequenceExample");
00075         dbConfig.setType(DatabaseType.BTREE);
00076         dbConfig.setAllowCreate(true);
00077         Database table = new Database(databaseName, null, dbConfig);
00078 
00079         SequenceConfig config = new SequenceConfig();
00080         config.setAllowCreate(true);
00081         DatabaseEntry key =
00082             new DatabaseEntry("my_sequence".getBytes());
00083         Sequence sequence = table.openSequence(null, key, config);
00084 
00085         for (int i = 0; i < 10; i++) {
00086             long seqnum = sequence.get(null, 1);
00087             System.out.println("Got sequence number: " + seqnum);
00088         }
00089 
00090         sequence.close();
00091         table.close();
00092     }
00093 }

Generated on Sun Dec 25 12:14:27 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2