javaDB笔记

想写个测试小东西,能够保存数据,但数据库又想太重量级,只在程序启动的时候用一下,退出时数据库也跟着一样关闭,但,当很多数据库都可以做到这样,不过目前首选择的应该就是mysql跟javadb了

    在用javadb之前,还是得先了解一下它的背景

1.是在jdk1.6才开始出现

2.它是一个纯java实现,开源的数据库管理系统,程序程序于JVM之上

3.起源于apache名下的derby

4.只有2MB大小

 

如果你只是想用于一些基本的开发测试的话,可以在初次使用时创建它,它会根据你的dbname在当前目录下建立一个以dbname为文件夹名的数据库,第二次使用的话,通过getDBConnection()获得Connection,也可以在此基础上建立多个连接做连接池。

/**
     * it maybe the first time for you to create a new db
     * the connection spcifies create=true to cause the database to be created.
     * to remove the database,remove the direcotry derbyDB and its contents.the directory
     * derbyDB will be created under the directory that the system property derby.system.home
     * point to ,or the current diryctory if derby.sytem.home is not set.
     * @throws InstantiationException
     */
    public Connection createDB() throws InstantiationException, SQLException, ClassNotFoundException, IllegalAccessException {
        Class.forName(driver).newInstance();
        System.out.println("Load the instance driver.");
        con = DriverManager.getConnection(protocol + message.getDbName() + ";create=true", message.getUsername(), message.getPassword());
        return con;
    }

    /**
     * get db connection
     * @return
     */
    public Connection getDBConection() throws Exception {
        Class.forName(driver).newInstance();
        System.out.println("Load the instance driver.");
        con = DriverManager.getConnection(protocol + message.getDbName(), message.getUsername(), message.getPassword());
        return con;
    }

    /**
     * shutdown  db 
     */
    public void shutdownDB() {
        if (framework.equals("embedded")) {
            boolean shutdown = false;
            try {
                DriverManager.getConnection("jdbc:derby:"+message.getDbName()+";shutdown=true");
            } catch (SQLException se) {
                shutdown = true;
            }

            if (!shutdown) {
                System.out.println("Database did not shut down normally.");
            } else {
                System.out.println("Database shut down normally.");
            }
        }
    }

 另注意程序退出时记得shutdown退出数据库

如果是在网络模式下可以通过下以下方法调用

  if (args[index].equalsIgnoreCase("jccjdbcclient")) {
                framework = "jccjdbc";
                driver = "com.ibm.db2.jcc.DB2Driver";
                protocol = "jdbc:derby:net://localhost:1527/";
            }
            if (args[index].equalsIgnoreCase("derbyclient")) {
                framework = "derbyclient";
                driver = "org.apache.derby.jdbc.ClientDriver";
                protocol = "jdbc:derby://localhost:1527/";
            }

 

derby附带小工具(位于%db_home%/lib/derbytools.jar):

  ij:可以选择SQL脚本

  dblook:为derby数据库作模式提取,并生成dll

  sysinfo:显示系统以及derby信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值