学习derby数据库

<!-- by admin -->


今天玩了会 derby数据库,找了点资料。转载一下,大家一起学习。转载http://www.diybl.com/course/3_program/java/javaxl/2008821/136732.html

本文是对Apache Derby官方文档的翻译与学习的总结,如有转载,请声明出处!

1.下载

首先到Apache Derby网站下载Derby最新的release版本。

笔者使用的版本为10.4.1.3。

建议将可以下载的发布包全部下载,因为文件都不是很大。以备后用。笔者下载了:

db-derby-10.4.1.3-bin.zip [PGP] [MD5]

db-derby-10.4.1.3-lib.zip [PGP] [MD5]

db-derby-10.4.1.3-lib-debug.zip [PGP] [MD5]

db-derby-10.4.1.3-src.zip [PGP] [MD5]

derby_core_plugin_10.4.1.648739.zip [PGP] [MD5]

derby_ui_plugin_1.1.2.zip [PGP] [MD5]

本文中只使用db-derby-10.4.1.3-bin.zip文件。

2.Derby简介

Derby是一个开源的基于Java和SQL的关系数据库管理系统(RDBMS)。Derby是使用纯Java编写和实现的。Derby为用户提供 了轻量的标准数据库引擎,它可以紧密的嵌入到任何基于Java的解决方案中。Derby确保数据完整性,并提供负责的事务处理操作。默认的配置中不包含任 何独立的数据库服务器。

Derby可以有两种部署方式:

嵌入式:Derby被单个用户的Java应用程序使用,在这种模式下,Derby和应用程序运行在同一个JVM中。Derby对于终端用户几乎是透明的,因为它的启动和终止都是由应用程序来控制,而不需要任何的管理。

服务器模式:Derby由应用程序启动,此时通过用户可以通过网络连接。在这种模式下,Derby运行在服务器主机的JVM中。其他JVM的应用程序通过连接服务器来访问数据库。

Derby的运行需要Java 1.4+版本的支持。

3.安装

解压缩db-derby-10.4.1.3-bin.zip文件到本地系统目录,例如C:\。(Apache的帮助文档中提到了Windows和Linux两种操作系统,笔者只介绍Windows操作系统的情况)

将该目录记为DERBY_HOME。

解压缩后的目录中包含了样例程序,运行脚本,javadoc,derby doc,运行时类库,derby测试程序等。

设置环境

将DERBY_HOME设置为系统变量。

将DERBY_HOME\bin添加到系统path环境变量中。

设置完成以后,运行derby的命令如下:

java -jar %DERBY_HOME%\lib\derbyrun.jar ij [-p propertiesfile] [sql_script]

java -jar %DERBY_HOME%\lib\derbyrun.jar sysinfo [-cp ...] [-cp help]

java -jar %DERBY_HOME%\lib\derbyrun.jar dblook [arg]* (or no arguments for usage)

java -jar %DERBY_HOME%\lib\derbyrun.jar server [arg]* (or no arguments for usage)

在DERBY_HOME\bin目录下,有一些设置CLASSPATH的脚本,命名为setXxxCP。

4.运行Derby工具

在Windows命令行提示符窗口中执行sysinfo、ij、dblook命令,对应着DERBY_HOME\bin目录中的脚本,可以运行Derby的工具。

sysinfo:显示Java环境的信息以及Derby的版本。

ij:它是一个JDBC工具,可以用来运行脚本或者交互查询。

dblook:它是一个数据定义语言生成工具。

5.自学

本部分包括了四方面的内容:

1> 使用ij工具来加载Derby嵌入式驱动,并启动Derby数据库引擎。创建firstdb数据库以及FIRSTTABLE表。使用基本的SQL语句进行插入和选择数据。Derby的日志信息保存在derby.log文件中。

2> 使用Derby的客户端/服务器配置。启动Derby网络服务器,在单独的进程中使用ij工具加载Derby客户端驱动,并连接到网络服务器,创建seconddb数据库和SECONDTABLE表,并使用基本的SQL语句插入和选择数据。

3> 在简单的Java JDBC程序中加载Derby数据库引擎,使用嵌入式驱动来创建jdbcDemoDB,以及WISH_LIST表。使用键盘输入的内容作为数据库表的数 据。然后查看数据库中的数据。通过查看代码来了解基本的JDBC访问Derby数据库的方式。介绍了CLASSPATH和连接URL属 性,shutdown=true.

4> 将3>中的应用程序修改加载客户端驱动来连接Derby网络服务器。

Part1

创建derbytutor目录

D:\common\apache-db-derby-10.4.1.3-bin>md derbytutor

D:\common\apache-db-derby-10.4.1.3-bin>cd derbytutor

将样例sql脚本拷贝到当前目录

D:\common\apache-db-derby-10.4.1.3-bin\derbytutor>copy %DERBY_HOME%\demo\programs\toursdb\*.sql .

D:\common\apache-db-derby-10.4.1.3-bin\demo\programs\toursdb\ToursDB_schema.sql

……

已复制        10 个文件。

D:\common\apache-db-derby-10.4.1.3-bin\derbytutor>ij

ij 版本 10.4

连接到嵌入式的Derby数据库,如果数据库不存在,就创建

connect为ij的命令,用于创建数据库连接

jdbc:derby是Derby驱动的JDBC协议

firstdb是数据库的名称。由于没有指定文件路径,所以数据库就在当前的目录中创建,数据库的内容存储在子目录firstdb中。

ij> connect ‘jdbc:derby:firstdb;create=true’;

create=true’表示创建一个新的数据库,在使用中多为create=false

创建第一张表

ij> create table firsttable(id int primary key,name varchar(12));

已插入/更新/删除 0 行

ij> insert into firsttable values(10,’TEN’);

已插入/更新/删除 1 行

ij> select * from firsttable;

ID         |NAME

————————

10         |TEN

已选择 1 行

加载脚本

ij> run ‘ToursDB_schema.sql’;

ij> — Licensed to the Apache Software Foundation (ASF) under one or more

– contributor license agreements. See the NOTICE file distributed with

– this work for additional information regarding copyright ownership.

– The ASF licenses this file to You under the Apache License, Version 2.0

– (the “License”); you may not use this file except in compliance with

– the License. You may obtain a copy of the License at

–     http://www.apache.org/licenses/LICENSE-2.0

– Unless required by applicable law or agreed to in writing, software

– distributed under the License is distributed on an “AS IS” BASIS,

– WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

– See the License for the specific language governing permissions and

– limitations under the License.

AUTOCOMMIT OFF;

ij> CREATE TABLE AIRLINES

(

AIRLINE CHAR(2) NOT NULL ,

AIRLINE_FULL VARCHAR(24),

BASIC_RATE DOUBLE PRECISION,

DISTANCE_DISCOUNT DOUBLE PRECISION,

BUSINESS_LEVEL_FACTOR DOUBLE PRECISION,

FIRSTCLASS_LEVEL_FACTOR DOUBLE PRECISION,

ECONOMY_SEATS INTEGER,

BUSINESS_SEATS INTEGER,

FIRSTCLASS_SEATS INTEGER

);

已插入/更新/删除 0 行

……

ij> COMMIT;

ij> exit;

Part2

这里我们需要启动两个windows命令行提示符,一个用于运行Derby网络服务器,医用用于运行Derby客户端。

首先打开一个windows命令行提示符窗口,运行如下的命令:

D:\common\apache-db-derby-10.4.1.3-bin\derbytutor>java -jar %derby_home%/lib/derbyrun.jar server start

已使用基本服务器安全策略安装了安全管理程序。

Apache Derby Network Server - 10.4.1.3 - (648739) 已启动并且已准备好 2008-08-20

07:19:10.375 GMT 时在端口 1527 上接受连接

此时就已经启动了Derby网络服务器。

然后启动第二个windows命令行提示符窗口,进行如下的操作:

D:\common\apache-db-derby-10.4.1.3-bin\derbytutor>ij

ij 版本 10.4

连接到服务器,客户端连接的URL包含了主机名和端口号。

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

ij> exit;

其他创建表等操作都是一样的。

Part3

笔者使用了Eclipse创建了Java工程,将

%DERBY_HOME%\demo\programs\workingwithderby下面的Java源代码拷贝到了工程下,并将derby.jar添加到工程的CLASSPATH中。

使用如下的方式创建数据库连接:

// ## DEFINE VARIABLES SECTION ##

// define the driver to use

String driver = “org.apache.derby.jdbc.EmbeddedDriver”;

// the database name

String dbName = “jdbcDemoDB”;

// define the Derby connection URL to use

String connectionURL = “jdbc:derby:” + dbName + “;create=true”;

Connection conn = null;

// Beginning of JDBC code sections

// ## LOAD DRIVER SECTION ##

try {

/*

* Load the Derby driver. When the embedded Driver is used this

* action start the Derby engine. Catch an error and suggest a

* CLASSPATH problem

*/

Class.forName(driver);

System.out.println(driver + ” loaded. “);

} catch (java.lang.ClassNotFoundException e) {

}

// Beginning of Primary DB access section

// ## BOOT DATABASE SECTION ##

try {

// Create (if needed) and connect to the database

conn = DriverManager.getConnection(connectionURL);

至此获取到了连接到Derby数据库的Connection对象,其他的操作就是JDBC的基本操作了。

Part4

使用JDBC连接Derby服务器模式的数据库与连接嵌入式模式的数据库类似,只是驱动程序和连接的URL发生了变化:

String driver = “org.apache.derby.jdbc.ClientDriver”;

// the database name

String dbName = “jdbcDemoDB”;

// define the Derby connection URL to use

String connectionURL = “jdbc:derby://localhost:1527/” + dbName

+ “;create=true”;

其他的操作完全一致。

6.Derby提供的类库

引擎类库

derby.jar:用于嵌入式数据库。

对于嵌入式环境,改类库总是必须的。对于客户端/服务器环境,只需要将该类库放在服务器上。

工具

对于嵌入式环境,需要在classpath中追加类库来使用工具,对于客户端/服务器环境,只需要在客户端追加该类库。

derbytools.jar:运行所有的Derby工具(ij,dblook,import/export)都需要改类库。

derbyrun.jar:用于启动Derby工具的可运行jar文件。

网络服务器类库

derbynet.jar:用于启动Derby网络服务器。

网络客户端类库

derbyclient.jar:作为Derby网络客户端驱动程序。

本地化类库

* derbyLocale_cs.jar

* derbyLocale_de_DE.jar

* derbyLocale_es.jar

* derbyLocale_fr.jar

* derbyLocale_it.jar

* derbyLocale_hu.jar

* derbyLocale_ja_JP.jar

* derbyLocale_ko_KR.jar

* derbyLocale_pl.jar

* derbyLocale_pt_BR.jar

* derbyLocale_ru.jar

* derbyLocale_zh_CN.jar

* derbyLocale_zh_TW.jar

未指定的区域提供特定语言的消息。

分页解决办法如下 5.2. Does Derby support a LIMIT command? Starting with the 10.4.1.3 release Derby also supports limiting the number of rows using the ROW_NUMBER function. For example, to fetch the first 5 rows of a large table: SELECT * FROM ( SELECT ROW_NUMBER() OVER() AS rownum, myLargeTable.* FROM myLargeTable ) AS tmp WHERE rownum <= 5; The ROW_NUMBER function can also be used to select a limited number of rows starting with an offset, for example: SELECT * FROM ( SELECT ROW_NUMBER() OVER() AS rownum, myLargeTable.* FROM myLargeTable ) AS tmp WHERE rownum > 200000 AND rownum <= 200005; The LIMIT keyword is not defined in the SQL standard, and is currently not supported.

参考:

http://hi.baidu.com/myweb2/blog/item/abeb34468b61790a6b63e5c7.html/cmtid/30dea7257bea936935a80f62

http://www.builder.com.cn/2007/1109/619578.shtml (性能优化)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值