java内嵌数据库

来源:互联网 发布:网络技术员求职信 编辑:程序博客网 时间:2024/06/11 16:17
   新安装了 JDK 6 的程序员们也许会发现,除了传统的 bin、jre 等目录,JDK 6 新增了一个名为 db 的目录。这便是 Java 6 的新成员:Java DB。这是一个纯 Java 实现、开源的数据库管理系统(DBMS),源于 Apache 软件基金会(ASF)名下的项目 Derby。它只有 2MB 大小,对比动辄上 G 的数据库来说可谓袖珍。但这并不妨碍 Derby 功能齐备,支持几乎大部分的数据库应用所需要的特性。更难能可贵的是,依托于 ASF 强大的社区力量,Derby 得到了包括 IBM 和 Sun 等大公司以及全世界优秀程序员们的支持。这也难怪 Sun 公司会选择其 10.2.2 版本纳入到 JDK 6 中,作为内嵌的数据库。这就好像为 JDK 注入了一股全新的活力:Java 程序员不再需要耗费大量精力安装和配置数据库,就能进行安全、易用、标准、并且免费的数据库编程。在这一章中,我们将初窥 Java DB 的世界,来探究如何使用它编写出功能丰富的程序。Hello, Java DB:内嵌模式的 Derby既然有了内嵌(embedded)的数据库,就让我们从一个简单的范例开始,试着使用它吧。这个程序做了大多数数据库应用都可能会做的操作:在 DBMS 中创建了一个名为 helloDB 的数据库;创建了一张数据表,取名为 hellotable;向表内插入了两条数据;然后,查询数据并将结果打印在控制台上;最后,删除表和数据库,释放资源。public class HelloJavaDB {public static void main(String[] args) {try { // load the driverClass.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();System.out.println("Load the embedded driver");Connection conn = null;Properties props = new Properties();props.put("user", "user1"); props.put("password", "user1");//create and connect the database named helloDBconn=DriverManager.getConnection("jdbc:derby:helloDB;create=true", props);System.out.println("create and connect to helloDB");conn.setAutoCommit(false);// create a table and insert two recordsStatement s = conn.createStatement();s.execute("create table hellotable(name varchar(40), score int)");System.out.println("Created table hellotable");s.execute("insert into hellotable values('Ruth Cao', 86)");s.execute("insert into hellotable values ('Flora Shi', 92)");// list the two recordsResultSet rs = s.executeQuery("SELECT name, score FROM hellotable ORDER BY score");System.out.println("namettscore");while(rs.next()) {StringBuilder builder = new StringBuilder(rs.getString(1));builder.append("t");builder.append(rs.getInt(2));System.out.println(builder.toString());}// delete the tables.execute("drop table hellotable");System.out.println("Dropped table hellotable");rs.close();s.close();System.out.println("Closed result set and statement");conn.commit();conn.close();System.out.println("Committed transaction and closed connection");try { // perform a clean shutdownDriverManager.getConnection("jdbc:derby:;shutdown=true");} catch (SQLException se) {System.out.println("Database shut down normally");}} catch (Throwable e) {// handle the exception}System.out.println("SimpleApp finished");}}随后,我们在命令行(本例为 Windows 平台,当然,其它系统下稍作改动即可)下键入以下命令:清单 2. 运行 HelloJavaDB 命令java –cp .;%JAVA_HOME%dblibderby.jar HelloJavaDB图 1. HelloJavaDB 程序的执行结果上述的程序和以往没什么区别。不同的是我们不需要再为 DBMS 的配置而劳神,因为 Derby 已经自动地在当前目录下新建了一个名为 helloDB 的目录,来物理地存储数据和日志。需要做的只是注意命名问题:在内嵌模式下驱动的名字应为 org.apache.derby.jdbc.EmbeddedDriver;创建一个新数据库时需要在协议后加入 create=true。另外,关闭所有数据库以及 Derby 的引擎可以使用以下代码:清单 3. 关闭所有数据库及 Derby 引擎DriverManager.getConnection("jdbc:derby:;shutdown=true");如果只想关闭一个数据库,那么则可以调用:清单 4. 关闭一个数据库DriverManager.getConnection("jdbc:derby:helloDB;shutdown=true ");这样,使用嵌入模式的 Derby 维护和管理数据库的成本接近于 0。这对于希望专心写代码的人来说不失为一个好消息。然而有人不禁要问:既然有了内嵌模式,为什么大多数的 DBMS 都没有采取这样的模式呢?不妨做一个小实验。当我们同时在两个命令行窗口下运行 HelloJavaDB 程序。
0 0
原创粉丝点击