`

bdb数据库java操作手册

    博客分类:
  • bdb
阅读更多
     package demos.bdb;
	 
	import java.io.File;
	import com.sleepycat.je.Cursor;
	import com.sleepycat.je.Database;
	import com.sleepycat.je.DatabaseConfig;
	import com.sleepycat.je.DatabaseEntry;
	import com.sleepycat.je.Environment;
	import com.sleepycat.je.EnvironmentConfig;
	import com.sleepycat.je.LockMode;
	import com.sleepycat.je.OperationStatus;
	import com.sleepycat.je.Transaction;
	import com.sleepycat.je.TransactionConfig;
	 
	/*
	 * 数据库操作类
	 */
	public class BDB {
	 
	    private static Database bdb; // 数据源
	    private static Environment exampleEnv;// 环境对象
	    private static boolean isrunning = false;// 判断是否运行
	 
	    /**
	     * 打开数据库方法
	     */
	    public static void start(String path) {
	        if (isrunning) {
	            return;
	        }
	        /******************** 文件处理 ***********************/
	        File envDir = new File(path);// 操作文件
	        if (!envDir.exists())// 判断文件路径是否存在,不存在则创建
	        {
	            envDir.mkdir();// 创建
	        }
	 
	        /******************** 环境配置 ***********************/
	        EnvironmentConfig envConfig = new EnvironmentConfig();
	        envConfig.setTransactional(false); // 不进行事务处理
	        envConfig.setAllowCreate(true); // 如果不存在则创建一个
	        exampleEnv = new Environment(envDir, envConfig);// 通过路径,设置属性进行创建
	 
	        /******************* 创建适配器对象 ******************/
	        DatabaseConfig dbConfig = new DatabaseConfig();
	        dbConfig.setTransactional(false); // 不进行事务处理
	        dbConfig.setAllowCreate(true);// 如果不存在则创建一个
	        dbConfig.setSortedDuplicates(true);// 数据分类
	 
	        bdb = exampleEnv.openDatabase(null, "simpleDb", dbConfig); // 使用适配器打开数据库
	        isrunning = true; // 设定是否运行
	    }
	 
	    /**
	     * 关闭数据库方法
	     */
	    public static void stop() {
	        if (isrunning) {
	            isrunning = false;
	            bdb.close();
	            exampleEnv.close();
	        }
	    }
	 
	    public static boolean isrunning() {
	        return isrunning;
	    }
	 
	    /**
	     * 数据存储方法 set(Here describes this method function with a few words)
	     *
	     * TODO(Here describes this method to be suitable the condition - to be
	     * possible to elect)
	     *
	     * @param key
	     * @param data
	     *
	     *            void
	     */
	    public static void set(byte[] key, byte[] data) {
	        DatabaseEntry keyEntry = new DatabaseEntry();
	        DatabaseEntry dataEntry = new DatabaseEntry();
	        keyEntry.setData(key); // 存储数据
	        dataEntry.setData(data);
	 
	        OperationStatus status = bdb.put(null, keyEntry, dataEntry);// 持久化数据
	 
	        if (status != OperationStatus.SUCCESS) {
	            throw new RuntimeException("Data insertion got status " + status);
	        }
	    }
	 
	    /*
	     * 执行获取,根据key值获取
	     */
	    public static void selectByKey(String aKey) {
	        DatabaseEntry theKey =null;
	        DatabaseEntry theData = new DatabaseEntry();
	        try {
	             theKey = new DatabaseEntry(aKey.getBytes("utf-8"));
	        } catch (Exception e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	     
	         
	        if (bdb.get(null,theKey, theData,
	                LockMode.DEFAULT) == OperationStatus.SUCCESS) { //根据key值,进行数据查询
	            // Recreate the data String.
	            byte[] retData = theData.getData();
	            String foundData = new String(retData);
	            System.out.println("For key: '" + aKey + "' found data: '"
	                    + foundData + "'.");
	        }
	         
	    }
	     
	     
	    /**
	     * 查询所有,可遍历数据
	     * selectAll(Here describes this method function with a few words)  
	     *
	     * TODO(Here describes this method to be suitable the condition - to be possible to elect)
	     * 
	     *
	     * void
	     */
	    public static void selectAll() {
	        Cursor cursor = null;
	        cursor=bdb.openCursor(null, null);
	        DatabaseEntry theKey=null;
	        DatabaseEntry theData=null;    
	        theKey = new DatabaseEntry();
	        theData = new DatabaseEntry();
	         
	        while (cursor.getNext(theKey, theData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
	            System.out.println(new String(theData.getData()));
	        }
	        cursor.close();
	         
	    }
	     
	     
	    /**
	     *  删除方法
	     * delete(Here describes this method function with a few words)  
	     *
	     * TODO(Here describes this method to be suitable the condition - to be possible to elect)
	     *
	     * @param key
	     *
	     * void
	     */
	    public static void delete(String key) {
	        DatabaseEntry keyEntry =null;
	        try {
	            keyEntry = new DatabaseEntry(key.getBytes("utf-8"));
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        bdb.delete(null, keyEntry);
	    }
	     
	     
	}
分享到:
评论

相关推荐

    mysql官方中文参考手册

    MySQL 5.1参考手册 目录 前言 1. 一般信息 1.1. 关于本手册 1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. ...

    MySQL 5.1参考手册 (中文版)

    1.1. 关于本手册 1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年兼容性 1.5...

    MySQL 5.1参考手册中文版

    1.1. 关于本手册 1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年...

    MySQL 5.1官方简体中文参考手册

    1.1. 关于本手册 1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年兼容性 1.5...

    mysql5.1中文手册

    关于本手册 1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 ...

    MySQL 5.1参考手册

    原始参考手册为英文版,与英文版参考手册相比,本翻译版可能不是最新的。 目录 前言 1. 一般信息 1.1. 关于本手册 1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 ...

    MYSQL中文手册

    1.1. 关于本手册 1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年...

    MySQL5.1参考手册官方简体中文版

    MySQL 5.1参考手册 这是MySQL参考手册的翻译版本,关于MySQL参考手册,请访问dev.mysql.com。 原始参考手册为英文版,与英文版参考手册相比,本翻译版可能不是最新的。 This translation was done by MySQL ...

    MySQL 5.1中文手冊

    1.1. 关于本手册 1.2. 本手册采用的惯例 1.3. MySQL AB概述 1.4. MySQL数据库管理系统概述 1.4.1. MySQL的历史 1.4.2. MySQL的的主要特性 1.4.3. MySQL稳定性 1.4.4. MySQL表最大能达到多少 1.4.5. 2000年兼容性 1.5...

Global site tag (gtag.js) - Google Analytics