AccessExample.java
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*-
  2.  * See the file LICENSE for redistribution information.
  3.  *
  4.  * Copyright (c) 1997, 1998, 1999, 2000
  5.  * Sleepycat Software.  All rights reserved.
  6.  *
  7.  * $Id: AccessExample.java,v 11.5 2000/12/13 07:09:42 krinsky Exp $
  8.  */
  9. package com.sleepycat.examples;
  10. import com.sleepycat.db.*;
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.io.InputStreamReader;
  14. import java.io.IOException;
  15. import java.io.PrintStream;
  16. class AccessExample
  17. {
  18.     private static final String FileName = "access.db";
  19.     public AccessExample()
  20.     {
  21.     }
  22.     private static void usage()
  23.     {
  24.         System.err.println("usage: AccessExamplen");
  25.         System.exit(1);
  26.     }
  27.     public static void main(String argv[])
  28.     {
  29.         try
  30.         {
  31.             AccessExample app = new AccessExample();
  32.             app.run();
  33.         }
  34.         catch (DbException dbe)
  35.         {
  36.             System.err.println("AccessExample: " + dbe.toString());
  37.             System.exit(1);
  38.         }
  39.         catch (FileNotFoundException fnfe)
  40.         {
  41.             System.err.println("AccessExample: " + fnfe.toString());
  42.             System.exit(1);
  43.         }
  44.         System.exit(0);
  45.     }
  46.     // Prompts for a line, and keeps prompting until a non blank
  47.     // line is returned.  Returns null on error.
  48.     //
  49.     static public String askForLine(InputStreamReader reader,
  50.                                     PrintStream out, String prompt)
  51.     {
  52.         String result = "";
  53.         while (result != null && result.length() == 0) {
  54.             out.print(prompt);
  55.             out.flush();
  56.             result = getLine(reader);
  57.         }
  58.         return result;
  59.     }
  60.     // Not terribly efficient, but does the job.
  61.     // Works for reading a line from stdin or a file.
  62.     // Returns null on EOF.  If EOF appears in the middle
  63.     // of a line, returns that line, then null on next call.
  64.     //
  65.     static public String getLine(InputStreamReader reader)
  66.     {
  67.         StringBuffer b = new StringBuffer();
  68.         int c;
  69.         try {
  70.             while ((c = reader.read()) != -1 && c != 'n') {
  71.                 if (c != 'r')
  72.                     b.append((char)c);
  73.             }
  74.         }
  75.         catch (IOException ioe) {
  76.             c = -1;
  77.         }
  78.         if (c == -1 && b.length() == 0)
  79.             return null;
  80.         else
  81.             return b.toString();
  82.     }
  83.     public void run()
  84.          throws DbException, FileNotFoundException
  85.     {
  86.         // Remove the previous database.
  87.         new File(FileName).delete();
  88.         // Create the database object.
  89.         // There is no environment for this simple example.
  90.         Db table = new Db(null, 0);
  91.         table.set_error_stream(System.err);
  92.         table.set_errpfx("AccessExample");
  93.         table.open(FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
  94.         //
  95.         // Insert records into the database, where the key is the user
  96.         // input and the data is the user input in reverse order.
  97.         //
  98.         InputStreamReader reader = new InputStreamReader(System.in);
  99.         for (;;) {
  100.             String line = askForLine(reader, System.out, "input> ");
  101.             if (line == null)
  102.                 break;
  103.             String reversed = (new StringBuffer(line)).reverse().toString();
  104.             // See definition of StringDbt below
  105.             //
  106.             StringDbt key = new StringDbt(line);
  107.             StringDbt data = new StringDbt(reversed);
  108.             try
  109.             {
  110.                 int err;
  111.                 if ((err = table.put(null, 
  112.     key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
  113.                         System.out.println("Key " + line + " already exists.");
  114.                 }
  115.             }
  116.             catch (DbException dbe)
  117.             {
  118.                 System.out.println(dbe.toString());
  119.             }
  120.             System.out.println("");
  121.         }
  122.         // Acquire an iterator for the table.
  123.         Dbc iterator;
  124.         iterator = table.cursor(null, 0);
  125.         // Walk through the table, printing the key/data pairs.
  126.         // See class StringDbt defined below.
  127.         //
  128.         StringDbt key = new StringDbt();
  129.         StringDbt data = new StringDbt();
  130.         while (iterator.get(key, data, Db.DB_NEXT) == 0)
  131.         {
  132.             System.out.println(key.getString() + " : " + data.getString());
  133.         }
  134.         iterator.close();
  135.         table.close(0);
  136.     }
  137.     // Here's an example of how you can extend a Dbt in a straightforward
  138.     // way to allow easy storage/retrieval of strings, or whatever
  139.     // kind of data you wish.  We've declared it as a static inner
  140.     // class, but it need not be.
  141.     //
  142.     static /*inner*/
  143.     class StringDbt extends Dbt
  144.     {
  145.         StringDbt()
  146.         {
  147.             set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
  148.         }
  149.         StringDbt(String value)
  150.         {
  151.             setString(value);
  152.             set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
  153.         }
  154.         void setString(String value)
  155.         {
  156.             set_data(value.getBytes());
  157.             set_size(value.length());
  158.         }
  159.         String getString()
  160.         {
  161.             return new String(get_data(), 0, get_size());
  162.         }
  163.     }
  164. }