Apache Derby

What is Apache Derby?

 

Apache Derby, an Apache DB subproject, is an open source relational database implemented entirely in Java and available under the Apache License, Version 2.0. Some key advantages include:

  • Derby has a small footprint -- about 2 megabytes for the base engine and embedded JDBC driver.
  • Derby is based on the Java, JDBC, and SQL standards.
  • Derby provides an embedded JDBC driver that lets you embed Derby in any Java-based solution.
  • Derby also supports the more familiar client/server mode with the Derby Network Client JDBC driver and Derby Network Server.
  • Derby is easy to install, deploy, and use.

最新的MyEclipse中带有Derby,运行完了可以看到:

Apache Derby Network Server - 10.2.2.0 - (485682) 已启动并且已准备好 2008-12-09 12:51:04.296 GMT 时在端口 1527 上接受连接

最新的版本可以到apache上下载:http://db.apache.org/derby/derby_downloads.html

假设你的机子上装了JVM。

解压文件到c盘,进入目录:cd C:/db-derby-10.4.2.0-bin/bin

Derby提供了三种Tool。

The tools that are included with Derby are dblook, ij, and sysinfo. To run these tools see:

  1. Running sysinfo
  2. Running ij
  3. Running dblook

    下面参考官方文档来研究数据的操作。

    在进入的bin目录下执行命令:ij,系统会提示数据的版本信息。

    (1)连接数据库或创建一个新的DB(如果数据库不存在的时候就会新建)

         ij>CONNECT 'jdbc:derby:firstdb;create=true';

    (2)创建一张表

         ij>CREATE TABLE FIRSTTABLE
            (ID INT PRIMARY KEY,
             NAME VARCHAR(12));

    (3)插入记录

         ij>INSERT INTO FIRSTTABLE VALUES
            (10,'TEN'),(20,'TWENTY'),(30,'THIRTY');

    (4)查询

         ij>SELECT * FROM FIRSTTABLE;

    其实该CONNECT连接的是本地数据库,在bin目录下会看到该数据库firstdb。

    如果你启动了Derby的网络数据库服务,那么可以通过另一种方式进行连接:

    ij>connect 'jdbc:derby://localhost:1527/seconddb;create=true'

    其余的操作方式一致。

    在JAVA代码中如何进行连接呢?

    1. // String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    2.         String driver = "org.apache.derby.jdbc.ClientDriver";
    3.         // String dbName = "seconddb";
    4.         // String connectionURL = "jdbc:derby:" + dbName + ";create=true";
    5.         String connectionURL = "jdbc:derby://localhost:1527/seconddb;create=true";
    6.         Connection conn = null;
    7.         try {
    8.             Class.forName(driver);
    9.             conn = DriverManager.getConnection(connectionURL);
    10.             Statement stmt = conn.createStatement();
    11.             ResultSet myWishes = stmt.executeQuery("SELECT * FROM SECONDTABLE");
    12.             while (myWishes.next()) {
    13.                 System.out.println(myWishes.getString(1));
    14.             }
    15.         } catch (Throwable e) {
    16.             e.printStackTrace();
    17.         } finally {
    18.             try {
    19.                 if (conn != null)
    20.                     conn.close();
    21.             } catch (SQLException e) {
    22.                 e.printStackTrace();
    23.             }
    24.         }

     

    一点说明:注意两种驱动程序的写法,另两种工具有待试用。

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值