本文参考资料:
http://www.cecs.csulb.edu/~monge/classes/share/usingEclipseDerby.html
http://www.ibm.com/developerworks/opensource/library/os-ad-eclipse/
http://db.apache.org/derby/integrate/plugin_howto.html
http://db.apache.org/derby/papers/DerbyTut/ij_intro.html
安装Derby Plugin到Eclipse:
首先从Derby下载页面 查找最新官方发布的版本(或者需要的版本),下载derby_core_plugin
和 derby_ui_plugin这两个必须的Eclipse插件下载到计算,并解压缩到Eclipse目录下的plugins文 件夹下。注意,压缩包中所有文件可能都默认在plugin文件夹下,所以解压缩後,將得到plugin文件夹(也可能会叫plugin(1))下的所有文 件拷贝到eclipse安装目录下的plugin文件夹中。
被拷贝的文件应该是org.apache.derby.core_10.6.2
, org.apache.derby.ui_1.1.2
, 和 org.apache.derby.plugin.doc_1.1.2这样3个名称的(根据下载选择,版本好会有不同)。
使用Derby同Eclipse:
首 先,添加DerbyJava项目。运行Eclipse(如果Eclipse在安装Derby前就开着,请重新启动Eclipse)。在Package Explorer中选择Java项目,右键,依次选择Apache Derby,Add Apache Derby nature.到此,添加Derby完成。
你可以再次在项目上点击右键,Apache Derby,选择sysInfo(Derby System Information),便能在Eclipse的控制台输出中看到Derby系统信息。如下:
Java Version: 1.6.0_18
Java Vendor: Sun Microsystems Inc.
Java home: /usr/local/jdk1.6.0_18/jre
Java classpath: /windows/E/Computer Study/Software Study/Programming/Eclipse Workspace On Ubuntu 10.10/AccountManagement/bin:/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbytools.jar:/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derby.jar:/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbynet.jar:/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbyclient.jar
OS name: Linux
OS architecture: i386
OS version: 2.6.35-22-generic
Java user name: knowyourself1010
Java user home: /home/knowyourself1010
Java user dir: /windows/E/Computer Study/Software Study/Programming/Eclipse Workspace On Ubuntu 10.10/AccountManagement
java.specification.name: Java Platform API Specification
java.specification.version: 1.6
java.runtime.version: 1.6.0_18-b07
--------- Derby Information --------
JRE - JDBC: Java SE 6 - JDBC 4.0
[/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derby.jar] 10.6.2.1 - (999685)
[/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbytools.jar] 10.6.2.1 - (999685)
[/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbynet.jar] 10.6.2.1 - (999685)
[/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbyclient.jar] 10.6.2.1 - (999685)
------------------------------------------------------
----------------- Locale Information -----------------
------------------------------------------------------
ij>
首先,按照http://db.apache.org/derby/papers/DerbyTut/ij_intro.html#ij_create 创建一个数据库命令如下:
如果执行成功,刷新PackageExplorer中的项目目录,便能看到一个名为MyDbTest的文件夹和一个derby.log文件。可以打开derby.log文件瞧瞧。
在ij中输入help;可以查看帮助:
Supported commands include:
PROTOCOL 'JDBC protocol' [ AS ident ];
-- sets a default or named protocol
DRIVER 'class for driver'; -- loads the named class
CONNECT 'url for database' [ PROTOCOL namedProtocol ] [ AS connectionName ];
-- connects to database URL
-- and may assign identifier
SET CONNECTION connectionName; -- switches to the specified connection
SHOW CONNECTIONS; -- lists all connections
AUTOCOMMIT [ ON | OFF ]; -- sets autocommit mode for the connection
DISCONNECT [ CURRENT | connectionName | ALL ];
-- drop current, named, or all connections;
-- the default is CURRENT
SHOW SCHEMAS; -- lists all schemas in the current database
SHOW [ TABLES | VIEWS | PROCEDURES | FUNCTIONS | SYNONYMS ] { IN schema };
-- lists tables, views, procedures, functions or synonyms
SHOW INDEXES { IN schema | FROM table };
-- lists indexes in a schema, or for a table
SHOW ROLES; -- lists all defined roles in the database,
-- sorted
SHOW ENABLED_ROLES; -- lists the enabled roles for the current
-- connection (to see current role use
-- VALUES CURRENT_ROLE), sorted
SHOW SETTABLE_ROLES; -- lists the roles which can be set for the
-- current connection, sorted
DESCRIBE name; -- lists columns in the named table
COMMIT; -- commits the current transaction
ROLLBACK; -- rolls back the current transaction
PREPARE name AS 'SQL-J text'; -- prepares the SQL-J text
EXECUTE { name | 'SQL-J text' } [ USING { name | 'SQL-J text' } ] ;
-- executes the statement with parameter
-- values from the USING result set row
REMOVE name; -- removes the named previously prepared statement
RUN 'filename'; -- run commands from the named file
ELAPSEDTIME [ ON | OFF ]; -- sets elapsed time mode for ij
MAXIMUMDISPLAYWIDTH integerValue;
-- sets the maximum display width for
-- each column to integerValue
ASYNC name 'SQL-J text'; -- run the command in another thread
WAIT FOR name; -- wait for result of ASYNC'd command
GET [SCROLL INSENSITIVE] [WITH {HOLD | NOHOLD}] CURSOR name AS 'SQL-J query';
-- gets a cursor (JDBC result set) on the query
-- the default is a forward-only cursor with holdability
NEXT name; -- gets the next row from the named cursor
FIRST name; -- gets the first row from the named scroll cursor
LAST name; -- gets the last row from the named scroll cursor
PREVIOUS name; -- gets the previous row from the named scroll cursor
ABSOLUTE integer name; -- positions the named scroll cursor at the absolute row number
-- (A negative number denotes position from the last row.)
RELATIVE integer name; -- positions the named scroll cursor relative to the current row
-- (integer is number of rows)
AFTER LAST name; -- positions the named scroll cursor after the last row
BEFORE FIRST name; -- positions the named scroll cursor before the first row
GETCURRENTROWNUMBER name; -- returns the row number for the current position of the named scroll cursor
-- (0 is returned when the cursor is not positioned on a row.)
CLOSE name; -- closes the named cursor
LOCALIZEDDISPLAY [ ON | OFF ];
-- controls locale sensitive data representation
EXIT; -- exits ij
HELP; -- shows this message
Any unrecognized commands are treated as potential SQL-J commands and executed directly.
输入show connections;可以看到当前链接,如下:
CONNECTION0* - jdbc:derby:MyDbTest
* = current connection
接在来根据ij介绍,在当前数据库(MyDbTest)中创建表derbyDB,并增加修改数据,控制台输入输出如下:
0 rows inserted/updated/deleted
ij> insert into derbyDB values (1956,'Webster St.');
1 row inserted/updated/deleted
ij> insert into derbyDB values (1910,'Union St.');
1 row inserted/updated/deleted
ij> select * from derbyDB;
NUM |ADDR
----------------------------------------------------
1956 |Webster St.
1910 |Union St.
2 rows selected
ij> update derbyDB set num=180,addr='Grand Ave.' where num=1956;
1 row inserted/updated/deleted
ij> select * from derbyDB;
NUM |ADDR
----------------------------------------------------
180 |Grand Ave.
1910 |Union St.
2 rows selected
ij> show connections;
No connections available.
ij> exit;
在程序中链接MyDbTest数据库的命令有:
Connection conn =DriverManager.getConnection("jdbc:derby:MyDbTest");
或者:
jdbc:derby:../otherDirectory/MyDbTest
jdbc:derby:directory:MyDbTest
jdbc:derby:jar:(c:/derby/lib/jar2.jar)MyDbTest
更多链接方式请见:http://db.apache.org/derby/docs/dev/devguide/cdevdvlp34964.html