1. package test; 
  2.  
  3. import java.io.File; 
  4.  
  5. import com.sleepycat.je.Database; 
  6. import com.sleepycat.je.DatabaseConfig; 
  7. import com.sleepycat.je.DatabaseEntry; 
  8. import com.sleepycat.je.DatabaseException; 
  9. import com.sleepycat.je.Environment; 
  10. import com.sleepycat.je.EnvironmentConfig; 
  11. import com.sleepycat.je.LockMode; 
  12. import com.sleepycat.je.OperationStatus; 
  13.  
  14. public class MyBerkeleyDB { 
  15.  private Environment env; 
  16.  public Environment getEnv() { 
  17.   return env; 
  18.  } 
  19.  
  20.  private Database db; 
  21.  
  22.  public MyBerkeleyDB() { 
  23.  } 
  24.  
  25.  /** 
  26.   * 构建数据库的开发环境 
  27.   *  
  28.   * @param path   数据库开发环境的目录 
  29.   * @param cacheSize  配置缓存大小 
  30.   */ 
  31.  public void setUp(String path, long cacheSize) { 
  32.   EnvironmentConfig envConfig = new EnvironmentConfig(); 
  33.   //当设置为true时,说明若没有数据库的环境时,可以打开。否则就不能打开 
  34.   envConfig.setAllowCreate(true); 
  35.   //envConfig.setReadOnly(true); 
  36.   envConfig.setCacheSize(cacheSize); 
  37.   //设置事务 
  38.   //envConfig.setTransactional(true); 
  39.   //当提交事务的时候是否把缓存中的内容同步到磁盘中去。true 表示不同步,也就是说不写磁盘 
  40.   //envConfig.setTxnNoSync(true); 
  41.   //当提交事务的时候,是否把缓冲的log写到磁盘上,true 表示不同步,也就是说不写磁盘 
  42.   //envConfig.setTxnWriteNoSync(true); 
  43.   try { 
  44.    env = new Environment(new File(path), envConfig); 
  45.   } catch (DatabaseException e) { 
  46.    e.printStackTrace(); 
  47.   } 
  48.  } 
  49.  
  50.  /** 
  51.   * 构建数据库 
  52.   *  
  53.   * @param dbName 数据库的名称 
  54.   */ 
  55.  public void open(String dbName) { 
  56.   DatabaseConfig dbConfig = new DatabaseConfig(); 
  57.   //设置数据的是否可以创建的属性 
  58.   dbConfig.setAllowCreate(true); 
  59.   try { 
  60.    db = env.openDatabase(null, dbName, dbConfig); 
  61.   } catch (DatabaseException e) { 
  62.    e.printStackTrace(); 
  63.   } 
  64.  } 
  65.  
  66.  /** 
  67.   * 关闭berkeley db 
  68.   */ 
  69.  public void close() { 
  70.   try { 
  71.    if (db != null) { 
  72.     db.close(); 
  73.    } 
  74.    if (env != null) { 
  75.     env.cleanLog(); 
  76.     env.close(); 
  77.    } 
  78.   } catch (DatabaseException e) { 
  79.    e.printStackTrace(); 
  80.   } 
  81.  } 
  82.  
  83.  /** 
  84.   * 通过key得到数据 
  85.   * @param key   berkeley db中的key 
  86.   * @return 
  87.   * @throws Exception 
  88.   */ 
  89.  public String get(String key) throws Exception { 
  90.   DatabaseEntry queryKey = new DatabaseEntry(); 
  91.   DatabaseEntry value = new DatabaseEntry(); 
  92.   queryKey.setData(key.getBytes("UTF-8")); 
  93.   OperationStatus status = db 
  94.     .get(null, queryKey, value, LockMode.DEFAULT); 
  95.   if (status == OperationStatus.SUCCESS) { 
  96.    return new String(value.getData(),"utf-8"); 
  97.   } 
  98.   return null
  99.  } 
  100.  
  101.  /** 
  102.   * 向berkeley db中存入数据 
  103.   * @param key  存入berkeley db时的键 
  104.   * @param value  存入berkeley db时的值 
  105.   * @return 
  106.   * @throws Exception 
  107.   */ 
  108.  public boolean put(String key, String value) throws Exception { 
  109.   byte[] theKey = key.getBytes("UTF-8"); 
  110.   byte[] theValue = value.getBytes("UTF-8"); 
  111.   /** 
  112.    * Berkeley DB中的记录包括两个字段,就是键和值, 
  113.    * 并且这些键和值都必须是com.sleepycat.je.DatabaseEntry类的实例。 
  114.    */ 
  115.   OperationStatus status = db.put(nullnew DatabaseEntry(theKey), 
  116.     new DatabaseEntry(theValue)); 
  117.   if (status == OperationStatus.SUCCESS) { 
  118.    return true
  119.   } 
  120.   return false
  121.  } 
  122.  /** 
  123.   * 通过 
  124.   * @param key 
  125.   * @return 
  126.   * @throws Exception 
  127.   */ 
  128.  public boolean del(String key) throws Exception{ 
  129.   DatabaseEntry queryKey = new DatabaseEntry(); 
  130.   queryKey.setData(key.getBytes("UTF-8")); 
  131.   OperationStatus status=db.delete(null, queryKey); 
  132.   if(status==OperationStatus.SUCCESS){ 
  133.    return true
  134.   } 
  135.   return false
  136.  } 
  137.  public static void main(String[] args) { 
  138.   MyBerkeleyDB mbdb = new MyBerkeleyDB(); 
  139.   //必须先在你的C盘中创建文件夹bdb 
  140.   mbdb.setUp("C://bdb"1000000); 
  141.   mbdb.open("myDB"); 
  142.   try { 
  143.    System.out.println(mbdb.getEnv().getConfig()); 
  144.   } catch (DatabaseException e) { 
  145.    e.printStackTrace(); 
  146.   } 
  147.   System.out.println("开始向Berkeley db 中输入数据..."); 
  148.   for (int i = 0; i < 20; i++) { 
  149.    try { 
  150.     String key = "myKey" + i; 
  151.     String value = "myValue" + i; 
  152.     //System.out.println("[" + key + ":" + value + "]"); 
  153.     mbdb.put(key, value); 
  154.    } catch (Exception e) { 
  155.     e.printStackTrace(); 
  156.    } 
  157.   } 
  158.   try { 
  159.    System.out.println(mbdb.get("myKey2")); 
  160.   } catch (Exception e) { 
  161.    e.printStackTrace(); 
  162.   } 
  163.   try { 
  164.    if(mbdb.del("myKey2")){ 
  165.     System.out.println("ture"); 
  166.    }else
  167.     System.out.println("false"); 
  168.    } 
  169.   } catch (Exception e) { 
  170.    e.printStackTrace(); 
  171.   } 
  172.    mbdb.close(); 
  173.  
  174.  }